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 46245d9

Browse files
add python solutions of 54 & 498
1 parent 4fe92d0 commit 46245d9

File tree

5 files changed

+159
-18
lines changed

5 files changed

+159
-18
lines changed

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ You can also ask for problem solving ideas and discuss in GitHub issues directly
5353
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_26.java) & Python|![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Conclusion
5454
|27|[Remove Element](https://leetcode.com/problems/implement-strstr/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_27.java) & Python|![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Two-Pointer Technique
5555
|28|[Implement strStr()](https://leetcode.com/problems/implement-strstr/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_28.java) & Python|![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Introduction to String
56-
|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_54.java) & Python|![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Introduction to 2D Array
56+
|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_54.java) & [Python](https://github.com/guobinhit/myleetcode/blob/master/codes/python/leetcodes/src/main/python/com/hit/basmath/learn/array_and_string/_54.py)|![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Introduction to 2D Array
5757
|66|[Plus One](https://leetcode.com/problems/plus-one/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_66.java) & [Python](https://github.com/guobinhit/myleetcode/blob/master/codes/python/leetcodes/src/main/python/com/hit/basmath/learn/array_and_string/_66.py)|![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Introduction to Array
5858
|67|[Add Binary](https://leetcode.com/problems/add-binary/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_67.java) & Python|![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Introduction to String
5959
|118|[Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_118.java) & Python| ![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Introduction to 2D Array
@@ -65,7 +65,7 @@ You can also ask for problem solving ideas and discuss in GitHub issues directly
6565
|283|[Move Zeroes](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_283.java) & Python| ![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Conclusion
6666
|344|[Reverse String](https://leetcode.com/problems/reverse-string/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_344.java) & Python| ![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Two-Pointer Technique
6767
|485|[Max Consecutive Ones](https://leetcode.com/problems/diagonal-traverse/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_485.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Two-Pointer Technique
68-
|498|[Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_498.java) & Python |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Introduction to 2D Array
68+
|498|[Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_498.java) & [Python](https://github.com/guobinhit/myleetcode/blob/master/codes/python/leetcodes/src/main/python/com/hit/basmath/learn/array_and_string/_498.py) |![medium](https://github.com/guobinhit/myleetcode/blob/master/images/medium.png)| Introduction to 2D Array
6969
|557|[Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_557.java) & Python |![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Conclusion
7070
|561|[Array Partition I](https://leetcode.com/problems/array-partition-i/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_561.java) & Python|![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png)| Two-Pointer Technique
7171
|724|[Find Pivot Index](https://leetcode.com/problems/find-pivot-index/description/)|[Java](https://github.com/guobinhit/myleetcode/blob/master/codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_724.java) & [Python](https://github.com/guobinhit/myleetcode/blob/master/codes/python/leetcodes/src/main/python/com/hit/basmath/learn/array_and_string/_724.py)| ![easy](https://github.com/guobinhit/myleetcode/blob/master/images/easy.png) | Introduction to Array

‎codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_498.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
/**
44
* 498. Diagonal Traverse
55
* <p>
6-
* Given a matrix of m x N elements (m rows, N columns), return all elements of the matrix in diagonal order
7-
* as shown in the below image.
6+
* Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.
87
* <p>
98
* Input:
109
* [
@@ -36,7 +35,8 @@ public int[] findDiagonalOrder(int[][] matrix) {
3635
* For example, the sum of all moving up trajectory element can be divided by 2,
3736
* and the sum of all moving down trajectory element can't be divided by 2
3837
*/
39-
if ((row + col) % 2 == 0) { // moving up
38+
if ((row + col) % 2 == 0) {
39+
// moving up
4040
if (col == n - 1) {
4141
/**
4242
* If col == n - 1, it's mean this position is last column,
@@ -51,13 +51,14 @@ public int[] findDiagonalOrder(int[][] matrix) {
5151
col++;
5252
} else {
5353
/**
54-
* In this condition, in order to moving down,
54+
* In this condition, in order to moving up,
5555
* column index must be increased and row index must be decreased
5656
*/
5757
row--;
5858
col++;
5959
}
60-
} else { // moving down
60+
} else {
61+
// moving down
6162
if (row == m - 1) {
6263
/**
6364
* If row == m - 1, it's mean this position is last row,

‎codes/java/leetcodes/src/main/java/com/hit/basmath/learn/array_and_string/_54.java‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
*/
2121
public class _54 {
2222
public List<Integer> spiralOrder(int[][] matrix) {
23-
List<Integer> res = new ArrayList<Integer>();
23+
List<Integer> targetList = new ArrayList<Integer>();
2424

25-
// If matrix.length == 0, return res immediately
25+
// If matrix.length == 0, return targetList immediately
2626
if (matrix.length == 0) {
27-
return res;
27+
return targetList;
2828
}
2929

3030
// Initial variables
@@ -36,8 +36,8 @@ public List<Integer> spiralOrder(int[][] matrix) {
3636
// Iterate all element of matrix,condition is rowBegin <= rowEnd && colBegin <= colEnd
3737
while (rowBegin <= rowEnd && colBegin <= colEnd) {
3838
// Traverse Right
39-
for (int j = colBegin; j <= colEnd; j++) {
40-
res.add(matrix[rowBegin][j]);
39+
for (int i = colBegin; i <= colEnd; i++) {
40+
targetList.add(matrix[rowBegin][i]);
4141
}
4242

4343
/**
@@ -48,7 +48,7 @@ public List<Integer> spiralOrder(int[][] matrix) {
4848

4949
// Traverse Down
5050
for (int j = rowBegin; j <= rowEnd; j++) {
51-
res.add(matrix[j][colEnd]);
51+
targetList.add(matrix[j][colEnd]);
5252
}
5353

5454
/**
@@ -59,8 +59,8 @@ public List<Integer> spiralOrder(int[][] matrix) {
5959

6060
if (rowBegin <= rowEnd) {
6161
// Traverse Left
62-
for (int j = colEnd; j >= colBegin; j--) {
63-
res.add(matrix[rowEnd][j]);
62+
for (int k = colEnd; k >= colBegin; k--) {
63+
targetList.add(matrix[rowEnd][k]);
6464
}
6565
}
6666

@@ -72,8 +72,8 @@ public List<Integer> spiralOrder(int[][] matrix) {
7272

7373
if (colBegin <= colEnd) {
7474
// Traver Up
75-
for (int j = rowEnd; j >= rowBegin; j--) {
76-
res.add(matrix[j][colBegin]);
75+
for (int m = rowEnd; m >= rowBegin; m--) {
76+
targetList.add(matrix[m][colBegin]);
7777
}
7878
}
7979

@@ -83,6 +83,6 @@ public List<Integer> spiralOrder(int[][] matrix) {
8383
*/
8484
colBegin++;
8585
}
86-
return res;
86+
return targetList;
8787
}
8888
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
498. Diagonal Traverse
3+
4+
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.
5+
6+
Example:
7+
8+
Input:
9+
[
10+
[ 1, 2, 3 ],
11+
[ 4, 5, 6 ],
12+
[ 7, 8, 9 ]
13+
]
14+
15+
Output: [1,2,4,7,5,3,6,8,9]
16+
17+
Note:
18+
19+
The total number of elements of the given matrix will not exceed 10,000.
20+
"""
21+
22+
23+
class Solution(object):
24+
def findDiagonalOrder(self, matrix):
25+
"""
26+
:type matrix: List[List[int]]
27+
:rtype: List[int]
28+
"""
29+
if len(matrix) == 0:
30+
return []
31+
32+
row = 0
33+
col = 0
34+
m = len(matrix)
35+
n = len(matrix[0])
36+
37+
aimArr = []
38+
39+
i = 0
40+
while i < m * n:
41+
aimArr.append(matrix[row][col])
42+
if (row + col) % 2 == 0:
43+
if col == n - 1:
44+
row += 1
45+
elif row == 0:
46+
col += 1
47+
else:
48+
row -= 1
49+
col += 1
50+
else:
51+
if row == m - 1:
52+
col += 1
53+
elif col == 0:
54+
row += 1
55+
else:
56+
row += 1
57+
col -= 1
58+
59+
i += 1
60+
61+
return aimArr
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
54. Spiral Matrix
3+
4+
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
5+
6+
Example 1:
7+
8+
Input:
9+
[
10+
[ 1, 2, 3 ],
11+
[ 4, 5, 6 ],
12+
[ 7, 8, 9 ]
13+
]
14+
Output: [1,2,3,6,9,8,7,4,5]
15+
16+
Example 2:
17+
18+
Input:
19+
[
20+
[1, 2, 3, 4],
21+
[5, 6, 7, 8],
22+
[9,10,11,12]
23+
]
24+
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
25+
"""
26+
27+
28+
class Solution(object):
29+
def spiralOrder(self, matrix):
30+
"""
31+
:type matrix: List[List[int]]
32+
:rtype: List[int]
33+
"""
34+
targetList = []
35+
36+
if len(matrix) == 0:
37+
return targetList
38+
39+
rowBegin = 0
40+
rowEnd = len(matrix) - 1
41+
colBegin = 0
42+
colEnd = len(matrix[0]) - 1
43+
44+
while rowBegin <= rowEnd and colBegin <= colEnd:
45+
i = colBegin
46+
while i <= colEnd:
47+
targetList.append(matrix[rowBegin][i])
48+
i += 1
49+
50+
rowBegin += 1
51+
52+
j = rowBegin
53+
while j <= rowEnd:
54+
targetList.append(matrix[j][colEnd])
55+
j += 1
56+
57+
colEnd -= 1
58+
59+
if rowBegin <= rowEnd:
60+
k = colEnd
61+
while k >= colBegin:
62+
targetList.append(matrix[rowEnd][k])
63+
k -= 1
64+
65+
rowEnd -= 1
66+
67+
if colBegin <= colEnd:
68+
m = rowEnd
69+
while m >= rowBegin:
70+
targetList.append(matrix[m][colBegin])
71+
m -= 1
72+
73+
colBegin += 1
74+
75+
return targetList
76+
77+
78+
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
79+
print Solution().spiralOrder(matrix)

0 commit comments

Comments
(0)

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