Skip to content

Commit 0974ade

Browse files
authored
feat: update solutions to lc problem: No.2326 (#2787)
1 parent 0378ee4 commit 0974ade

File tree

20 files changed

+167
-159
lines changed

20 files changed

+167
-159
lines changed

lcci/16.26.Calculator/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class Solution {
207207
var sign: Character = "+"
208208
var stk = [Int]()
209209
let sArray = Array(s)
210-
210+
211211
for i in 0..<n {
212212
let c = sArray[i]
213213
if c.isNumber {
@@ -234,7 +234,7 @@ class Solution {
234234
sign = c
235235
}
236236
}
237-
237+
238238
return stk.reduce(0, +)
239239
}
240240
}

lcci/16.26.Calculator/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class Solution {
217217
var sign: Character = "+"
218218
var stk = [Int]()
219219
let sArray = Array(s)
220-
220+
221221
for i in 0..<n {
222222
let c = sArray[i]
223223
if c.isNumber {
@@ -244,7 +244,7 @@ class Solution {
244244
sign = c
245245
}
246246
}
247-
247+
248248
return stk.reduce(0, +)
249249
}
250250
}

lcci/17.01.Add Without Plus/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ class Solution {
5050
var b = b
5151
var sum = 0
5252
var carry = 0
53-
53+
5454
while b != 0 {
5555
sum = a ^ b
5656
carry = (a & b) << 1
5757
a = sum
5858
b = carry
5959
}
60-
60+
6161
return a
6262
}
6363
}

lcci/17.01.Add Without Plus/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ class Solution {
5151
var b = b
5252
var sum = 0
5353
var carry = 0
54-
54+
5555
while b != 0 {
5656
sum = a ^ b
5757
carry = (a & b) << 1
5858
a = sum
5959
b = carry
6060
}
61-
61+
6262
return a
6363
}
6464
}

lcci/17.05.Find Longest Subarray/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class Solution {
163163
func findLongestSubarray(_ array: [String]) -> [String] {
164164
var vis: [Int: Int] = [0: -1]
165165
var s = 0, mx = 0, k = 0
166-
166+
167167
for i in 0..<array.count {
168168
s += array[i].first!.isLetter ? 1 : -1
169169
if let j = vis[s] {
@@ -175,7 +175,7 @@ class Solution {
175175
vis[s] = i
176176
}
177177
}
178-
178+
179179
return Array(array[k..<(k + mx)])
180180
}
181181
}

lcci/17.05.Find Longest Subarray/README_EN.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class Solution {
172172
func findLongestSubarray(_ array: [String]) -> [String] {
173173
var vis: [Int: Int] = [0: -1]
174174
var s = 0, mx = 0, k = 0
175-
175+
176176
for i in 0..<array.count {
177177
s += array[i].first!.isLetter ? 1 : -1
178178
if let j = vis[s] {
@@ -184,7 +184,7 @@ class Solution {
184184
vis[s] = i
185185
}
186186
}
187-
187+
188188
return Array(array[k..<(k + mx)])
189189
}
190190
}

solution/2300-2399/2326.Spiral Matrix IV/README.md

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,17 @@
4646

4747
## 解法
4848

49-
### 方法一
49+
### 方法一:模拟
50+
51+
我们定义一个二维数组 $\text{ans}$,用来存放链表中的元素,初始时全部填充为 $-1$。定义三个变量 $i, j, k$,分别表示当前的行、列和方向。定义一个数组 $\text{dirs}$,表示四个方向的偏移量。
52+
53+
然后我们开始遍历链表,每次遍历一个节点,就将当前节点的值填充到 $\text{ans}[i][j]$ 中,然后更新链表的指针,如果链表为空,说明所有的元素都已经填充完毕,退出循环。
54+
55+
否则,我们需要找到下一个元素的位置,我们可以通过当前位置 $(i, j)$ 和当前方向 $k$ 来计算下一个位置 $(x, y)$,如果 $(x, y)$ 在矩阵的范围内,并且 $\text{ans}[x][y]$ 为 $-1$,说明 $(x, y)$ 还没有被填充过,我们就将 $(x, y)$ 作为下一个位置,否则我们需要更换方向。
56+
57+
遍历完链表之后,我们就得到了一个螺旋矩阵,返回即可。
58+
59+
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别表示矩阵的行数和列数。
5060

5161
<!-- tabs:start -->
5262

@@ -59,20 +69,19 @@
5969
class Solution:
6070
def spiralMatrix(self, m: int, n: int, head: Optional[ListNode]) -> List[List[int]]:
6171
ans = [[-1] * n for _ in range(m)]
62-
i = j = p = 0
63-
dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]]
72+
i = j = k = 0
73+
dirs = (0, 1, 0, -1, 0)
6474
while 1:
6575
ans[i][j] = head.val
6676
head = head.next
67-
if not head:
77+
if head is None:
6878
break
6979
while 1:
70-
x, y = i + dirs[p][0], j + dirs[p][1]
71-
if x < 0 or y < 0 or x >= m or y >= n or ~ans[x][y]:
72-
p = (p + 1) % 4
73-
else:
80+
x, y = i + dirs[k], j + dirs[k + 1]
81+
if 0 <= x < m and 0 <= y < n and ans[x][y] == -1:
7482
i, j = x, y
7583
break
84+
k = (k + 1) % 4
7685
return ans
7786
```
7887

@@ -90,26 +99,25 @@ class Solution:
9099
class Solution {
91100
public int[][] spiralMatrix(int m, int n, ListNode head) {
92101
int[][] ans = new int[m][n];
93-
for (int[] row : ans) {
102+
for (var row : ans) {
94103
Arrays.fill(row, -1);
95104
}
96-
int i = 0, j = 0, p = 0;
97-
int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
105+
int i = 0, j = 0, k = 0;
106+
final int[] dirs = {0, 1, 0, -1, 0};
98107
while (true) {
99108
ans[i][j] = head.val;
100109
head = head.next;
101110
if (head == null) {
102111
break;
103112
}
104113
while (true) {
105-
int x = i + dirs[p][0], y = j + dirs[p][1];
106-
if (x < 0 || y < 0 || x >= m || y >= n || ans[x][y] >= 0) {
107-
p = (p + 1) % 4;
108-
} else {
114+
int x = i + dirs[k], y = j + dirs[k + 1];
115+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
109116
i = x;
110117
j = y;
111118
break;
112119
}
120+
k = (k + 1) % 4;
113121
}
114122
}
115123
return ans;
@@ -132,20 +140,22 @@ class Solution {
132140
public:
133141
vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
134142
vector<vector<int>> ans(m, vector<int>(n, -1));
135-
int i = 0, j = 0, p = 0;
136-
vector<vector<int>> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
143+
int i = 0, j = 0, k = 0;
144+
const int dirs[5] = {0, 1, 0, -1, 0};
137145
while (1) {
138146
ans[i][j] = head->val;
139147
head = head->next;
140-
if (!head) break;
148+
if (!head) {
149+
break;
150+
}
141151
while (1) {
142-
int x = i + dirs[p][0], y = j + dirs[p][1];
143-
if (x < 0 || y < 0 || x >= m || y >= n || ans[x][y] >= 0)
144-
p = (p + 1) % 4;
145-
else {
146-
i = x, j = y;
152+
int x = i + dirs[k], y = j + dirs[k + 1];
153+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) {
154+
i = x;
155+
j = y;
147156
break;
148157
}
158+
k = (k + 1) % 4;
149159
}
150160
}
151161
return ans;
@@ -169,22 +179,20 @@ func spiralMatrix(m int, n int, head *ListNode) [][]int {
169179
ans[i][j] = -1
170180
}
171181
}
172-
i, j, p := 0, 0, 0
173-
dirs := [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}
182+
i, j, k := 0, 0, 0
183+
dirs := [5]int{0, 1, 0, -1, 0}
174184
for {
175185
ans[i][j] = head.Val
176-
head = head.Next
177-
if head == nil {
186+
if head = head.Next; head == nil {
178187
break
179188
}
180189
for {
181-
x, y := i+dirs[p][0], j+dirs[p][1]
182-
if x < 0 || y < 0 || x >= m || y >= n || ans[x][y] >= 0 {
183-
p = (p + 1) % 4
184-
} else {
190+
x, y := i+dirs[k], j+dirs[k+1]
191+
if x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1 {
185192
i, j = x, y
186193
break
187194
}
195+
k = (k + 1) % 4
188196
}
189197
}
190198
return ans
@@ -205,26 +213,24 @@ func spiralMatrix(m int, n int, head *ListNode) [][]int {
205213
*/
206214

207215
function spiralMatrix(m: number, n: number, head: ListNode | null): number[][] {
208-
const dirs = [
209-
[0, 1],
210-
[1, 0],
211-
[0, -1],
212-
[-1, 0],
213-
];
214-
let ans = Array.from({ length: m }, v => new Array(n).fill(-1));
215-
let i = 0,
216-
j = 0,
217-
k = 0;
218-
while (head) {
216+
const ans: number[][] = Array.from({ length: m }, () => Array(n).fill(-1));
217+
const dirs: number[] = [0, 1, 0, -1, 0];
218+
let [i, j, k] = [0, 0, 0];
219+
while (1) {
219220
ans[i][j] = head.val;
220221
head = head.next;
221-
let x = i + dirs[k][0];
222-
let y = j + dirs[k][1];
223-
if (x < 0 || x > m - 1 || y < 0 || y > n - 1 || ans[x][y] != -1) {
222+
if (!head) {
223+
break;
224+
}
225+
while (1) {
226+
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
227+
if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] === -1) {
228+
i = x;
229+
j = y;
230+
break;
231+
}
224232
k = (k + 1) % 4;
225233
}
226-
i = i + dirs[k][0];
227-
j = j + dirs[k][1];
228234
}
229235
return ans;
230236
}

0 commit comments

Comments
 (0)