Skip to content

Commit e7f7d81

Browse files
committed
add leetcode: offer-29-31
1 parent 072cb70 commit e7f7d81

3 files changed

+87
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-17-23:43
6+
* @Function Description :29.顺时针打印矩阵
7+
*/
8+
public class _29PrintMatrixClockwise {
9+
//==========================覆盖原数组的============================
10+
class Solution {
11+
public int[] spiralOrder(int[][] matrix) {
12+
if( matrix.length == 0 ) return new int[0];
13+
int n = matrix.length , m = matrix[0].length;
14+
int[] res = new int[ n * m ];
15+
int x = 0 , y = 0 , d = 0 ;
16+
int[] dy = {1 , 0 , -1 , 0} , dx = {0 , 1 , 0 , -1};
17+
for( int i = 0 ; i < n * m ; i++ ){
18+
res[i] = matrix[x][y];
19+
matrix[x][y] = Integer.MAX_VALUE;
20+
int a = x + dx[d] , b = y + dy[d];
21+
if( a < 0 || a >= n || b < 0 || b >= m || matrix[a][b] == Integer.MAX_VALUE ){
22+
d = (d + 1) % 4;
23+
a = x + dx[d];
24+
b = y + dy[d];
25+
}
26+
x = a ;
27+
y = b;
28+
}
29+
return res;
30+
}
31+
}
32+
33+
//==========================开一个标志数组的============================
34+
class Solution2 {
35+
public int[] spiralOrder(int[][] matrix) {
36+
if( matrix.length == 0 ) return new int[0];
37+
int x = 0 , y = 0 , d = 1;
38+
int[] dx = {-1 , 0 , 1 , 0};
39+
int[] dy = {0 , 1 , 0 , -1};
40+
41+
int n = matrix.length ;int m = matrix[0].length;
42+
int[] ans = new int[n * m];
43+
44+
boolean[][] flag = new boolean[n][m];
45+
for( int i = 0 ; i < n * m ; i++ ){
46+
flag[x][y] = true;
47+
ans[i] = matrix[x][y];
48+
int a = x + dx[d] , b = y + dy[d];
49+
if( a < 0 || a >= n || b < 0 || b >= m || flag[a][b] == true ){
50+
d = (d + 1) % 4;
51+
a = x + dx[d] ; b = y + dy[d];
52+
}
53+
x = a ; y = b;
54+
}
55+
return ans;
56+
}
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package offer;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* @author : CodeWater
7+
* @create :2022-07-17-23:52
8+
* @Function Description :31.栈的压入、弹出序列
9+
*/
10+
public class _31PressingAndPopUpSequencesOfTheStack {
11+
class Solution {
12+
// 开一个辅助栈,
13+
public boolean validateStackSequences(int[] pushed, int[] popped) {
14+
Stack<Integer> stack = new Stack<>();
15+
int i = 0;
16+
for( int num : pushed ){
17+
stack.push(num);
18+
// 栈不空,栈顶跟当前弹出顺序值一致就出栈
19+
while( !stack.isEmpty() && stack.peek() == popped[i] ){
20+
stack.pop();
21+
i++;
22+
}
23+
}
24+
// 遍历结束,栈中还有元素,那么就不是正确的出栈顺序
25+
return stack.isEmpty();
26+
}
27+
}
28+
}

Algorithm/src/offer/_57_2AndTheContinuousPositiveNumberSequenceOfSAndS.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public int[][] findContinuousSequence(int target) {
3131
}
3232

3333
//==========================双指针============================
34-
class Solution {
34+
class Solution2 {
3535
public int[][] findContinuousSequence(int target) {
3636
int i = 1 , j = 2 , s = 3;
3737
List<int[]> res = new ArrayList<>();

0 commit comments

Comments
 (0)