Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8fecd51

Browse files
add 289. 1472
1 parent 389d606 commit 8fecd51

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

‎0289-game-of-life.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
289. Game of Life
3+
4+
Submitted: March 17, 2025
5+
6+
Runtime: 2 ms (beats 23.06%)
7+
Memory: 17.70 MB (beats 94.61%)
8+
"""
9+
10+
class Solution:
11+
def gameOfLife(self, board: List[List[int]]) -> None:
12+
"""
13+
Do not return anything, modify board in-place instead.
14+
"""
15+
board_copy = [row.copy() for row in board]
16+
17+
for i in range(len(board)):
18+
for j in range(len(board[0])):
19+
n = self.sumOfNeighbors(board_copy, i, j)
20+
if board_copy[i][j]:
21+
if n < 2: board[i][j] = 0
22+
elif n > 3: board[i][j] = 0
23+
elif n == 3:
24+
board[i][j] = 1
25+
26+
27+
def sumOfNeighbors(self, board, i, j):
28+
return sum(
29+
sum(
30+
board[x][y] for y in range(max(0, j - 1), min(len(board[0]), j + 2))
31+
if (x, y) != (i, j)
32+
)
33+
for x in range(max(0, i - 1), min(len(board), i + 2))
34+
)

‎1472-design-browser-history.cpp‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
1472. Design Browser History
3+
4+
Submitted: March 19, 2025
5+
6+
Runtime: 20 ms (beats 77.32%)
7+
Memory: 62.60 MB (beats 87.07%)
8+
*/
9+
10+
class BrowserHistory {
11+
public:
12+
typedef vector<string> container_type;
13+
private:
14+
container_type container;
15+
container_type::iterator pos;
16+
17+
public:
18+
BrowserHistory(string homepage) {
19+
container.push_back(homepage);
20+
pos = container.begin();
21+
}
22+
23+
void visit(string url) {
24+
container.erase(pos + 1, container.end());
25+
container.push_back(url);
26+
pos = container.end() - 1;
27+
}
28+
29+
string back(int steps) {
30+
return *(pos = max(container.begin(), pos - steps));
31+
}
32+
33+
string forward(int steps) {
34+
return *(pos = min(container.end() - 1, pos + steps));
35+
}
36+
};
37+
38+
/**
39+
* Your BrowserHistory object will be instantiated and called as such:
40+
* BrowserHistory* obj = new BrowserHistory(homepage);
41+
* obj->visit(url);
42+
* string param_2 = obj->back(steps);
43+
* string param_3 = obj->forward(steps);
44+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /