Movatterモバイル変換


[0]ホーム

URL:


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

Commitaefa2ea

Browse files
authored
Merge branch 'main' into main
2 parents8ccced9 +2a30677 commitaefa2ea

File tree

282 files changed

+11531
-133
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

282 files changed

+11531
-133
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

‎130-Surrounded-Regions.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
classSolution {
2+
publicvoidsolve(char[][]board) {
3+
intnRows =board.length;
4+
intnCols =board[0].length;
5+
6+
// 1a) Capture unsurrounded regions - top and bottom row (O -> T)
7+
for (inti =0;i <nCols;i++) {
8+
if (board[0][i] =='O')dfs(board,0,i);
9+
if (board[nRows-1][i] =='O')dfs(board,nRows-1,i);
10+
}
11+
12+
// 1b) Capture unsurrounded regions - Left and right columns (O -> T)
13+
for (inti =0;i <nRows;i++) {
14+
if (board[i][0] =='O')dfs(board,i,0);
15+
if (board[i][nCols-1] =='O')dfs(board,i,nCols-1);
16+
}
17+
18+
for (intr =0;r <nRows;r++) {
19+
for (intc =0;c <nCols;c++) {
20+
if (board[r][c] =='O')board[r][c] ='X';// 2) Capture surrounded regions (O -> X)
21+
if (board[r][c] =='T')board[r][c] ='O';// 3) Uncapture unsurrounded regions (T- O)
22+
}
23+
}
24+
}
25+
26+
privatevoiddfs(char[][]board,intr,intc) {
27+
intnRows =board.length;
28+
intnCols =board[0].length;
29+
if (r <0 ||c <0 ||r >=nRows ||c >=nCols ||board[r][c] !='O')return;
30+
31+
board[r][c] ='T';
32+
dfs(board,r+1,c);
33+
dfs(board,r-1,c);
34+
dfs(board,r,c+1);
35+
dfs(board,r,c-1);
36+
}
37+
}

‎200-Number-of-Islands.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ def numIslands(self, grid: List[List[str]]) -> int:
44
return0
55

66
islands=0
7-
q=collections.deque()
87
visit=set()
98
rows,cols=len(grid),len(grid[0])
109

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
classSolution(object):
2+
defremoveDuplicates(self,nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
k=0
8+
i=0
9+
whilei<len(nums):
10+
val=nums[i]
11+
whilei+1<len(nums)andnums[i+1]==val:
12+
nums.remove(val)
13+
k+=1
14+
i+=1
15+
returnlen(nums)
16+

‎27-Remove-Element.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
classSolution(object):
2+
defremoveElement(self,nums,val):
3+
"""
4+
:type nums: List[int]
5+
:type val: int
6+
:rtype: int
7+
"""
8+
foriinrange(nums.count(val)):# loop how many val is in the list
9+
nums.remove(val)# remove each val one by one
10+
returnlen(nums)# return len of nums

‎286-Walls-and-Gates.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ def addRooms(r, c):
1414
return
1515
visit.add((r,c))
1616
q.append([r,c])
17-
17+
18+
forrinrange(ROWS):
19+
forcinrange(COLS):
20+
ifrooms[r][c]==0:
21+
q.append([r,c])
22+
visit.add((r,c))
23+
1824
dist=0
1925
whileq:
2026
foriinrange(len(q)):

‎371-Sum-of-Two-Integers.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ public int getSum(int a, int b) {
77
}
88
returna;
99
}
10-
}
10+
}

‎74-Search-a-2D-Matrix.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
1414

1515
ifnot (top<=bot):
1616
returnFalse
17-
row= (top+bot)//2
17+
1818
l,r=0,COLS-1
1919
whilel<=r:
2020
m= (l+r)//2
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Given int array, return true if any value appears at least twice
3+
Ex. nums = [1,2,3,1] -> true, nums = [1,2,3,4] -> false
4+
5+
If seen num previously then has dupe, else insert into hash set
6+
7+
Time: O(n)
8+
Space: O(n)
9+
*/
10+
11+
classSolution {
12+
public:
13+
boolcontainsDuplicate(vector<int>& nums) {
14+
unordered_set<int> s;
15+
16+
for (int i =0; i < nums.size(); i++) {
17+
if (s.find(nums[i]) != s.end()) {
18+
returntrue;
19+
}
20+
s.insert(nums[i]);
21+
}
22+
23+
returnfalse;
24+
}
25+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Design algorithm to encode/decode: list of strings <-> string
3+
4+
Encode/decode w/ non-ASCII delimiter: {len of str, "#", str}
5+
6+
Time: O(n)
7+
Space: O(1)
8+
*/
9+
10+
classCodec {
11+
public:
12+
13+
// Encodes a list of strings to a single string.
14+
stringencode(vector<string>& strs) {
15+
string result ="";
16+
17+
for (int i =0; i < strs.size(); i++) {
18+
string str = strs[i];
19+
result +=to_string(str.size()) +"#" + str;
20+
}
21+
22+
return result;
23+
}
24+
25+
// Decodes a single string to a list of strings.
26+
vector<string>decode(string s) {
27+
vector<string> result;
28+
29+
int i =0;
30+
while (i < s.size()) {
31+
int j = i;
32+
while (s[j] !='#') {
33+
j++;
34+
}
35+
int length =stoi(s.substr(i, j - i));
36+
string str = s.substr(j +1, length);
37+
result.push_back(str);
38+
i = j +1 + length;
39+
}
40+
41+
return result;
42+
}
43+
private:
44+
};
45+
46+
// Your Codec object will be instantiated and called as such:
47+
// Codec codec;
48+
// codec.decode(codec.encode(strs));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp