Skip to content

Commit eec6c20

Browse files
committed
✨ feat: add 1106、1210、1222、1245、11465、1766、1781、235、2335、2336、583、621、649、792、809、816、915、934
1 parent 53402bd commit eec6c20

19 files changed

+1027
-306
lines changed

LeetCode/1101-1110/1106. 解析布尔表达式(困难).md

+55-24
Original file line numberDiff line numberDiff line change
@@ -98,33 +98,36 @@ class Solution {
9898
}
9999
}
100100
```
101-
TypeScript 代码:
102-
```TypeScript
103-
function parseBoolExpr(s: string): boolean {
104-
function calc(a: string, b: string, op: string): string {
105-
const x = a == 't', y = b == 't'
106-
const ans = op == '|' ? x || y : x && y
107-
return ans ? 't' : 'f'
108-
}
109-
const nums = new Array<string>(s.length).fill(''), ops = new Array<string>(s.length).fill('')
110-
let idx1 = 0, idx2 = 0
111-
for (const c of s) {
112-
if (c == ',') continue
113-
if (c == 't' || c == 'f') nums[idx1++] = c
114-
if (c == '|' || c == '&' || c == '!') ops[idx2++] = c
115-
if (c == '(') nums[idx1++] = '-'
116-
if (c == ')') {
117-
let op = ops[--idx2], cur = ' '
118-
while (idx1 > 0 && nums[idx1 - 1] != '-') {
119-
const top = nums[--idx1]
120-
cur = cur == ' ' ? top : calc(top, cur, op)
101+
C++ 代码:
102+
```C++
103+
class Solution {
104+
public:
105+
bool parseBoolExpr(string s) {
106+
deque<char> nums, ops;
107+
for (char c : s) {
108+
if (c == ',') continue;
109+
if (c == 't' || c == 'f') nums.push_back(c);
110+
if (c == '|' || c == '&' || c == '!') ops.push_back(c);
111+
if (c == '(') nums.push_back('-');
112+
if (c == ')') {
113+
char op = ops.back(); ops.pop_back();
114+
char cur = ' ';
115+
while (!nums.empty() && nums.back() != '-') {
116+
char top = nums.back(); nums.pop_back();
117+
cur = cur == ' ' ? top : calc(top, cur, op);
118+
}
119+
if (op == '!') cur = cur == 't' ? 'f' : 't';
120+
nums.pop_back(); nums.push_back(cur);
121121
}
122-
if (op == '!') cur = cur == 't' ? 'f' : 't'
123-
idx1--; nums[idx1++] = cur
124122
}
123+
return nums.back() == 't';
125124
}
126-
return nums[idx1 - 1] == 't'
127-
}
125+
char calc(char a, char b, char op) {
126+
bool x = a == 't', y = b == 't';
127+
bool ans = op == '|' ? x | y : x & y;
128+
return ans ? 't' : 'f';
129+
}
130+
};
128131
```
129132
Python 代码:
130133
```Python
@@ -155,6 +158,34 @@ class Solution:
155158
nums.append(cur)
156159
return nums[-1] == 't'
157160
```
161+
TypeScript 代码:
162+
```TypeScript
163+
function parseBoolExpr(s: string): boolean {
164+
function calc(a: string, b: string, op: string): string {
165+
const x = a == 't', y = b == 't'
166+
const ans = op == '|' ? x || y : x && y
167+
return ans ? 't' : 'f'
168+
}
169+
const nums = new Array<string>(s.length).fill(''), ops = new Array<string>(s.length).fill('')
170+
let idx1 = 0, idx2 = 0
171+
for (const c of s) {
172+
if (c == ',') continue
173+
if (c == 't' || c == 'f') nums[idx1++] = c
174+
if (c == '|' || c == '&' || c == '!') ops[idx2++] = c
175+
if (c == '(') nums[idx1++] = '-'
176+
if (c == ')') {
177+
let op = ops[--idx2], cur = ' '
178+
while (idx1 > 0 && nums[idx1 - 1] != '-') {
179+
const top = nums[--idx1]
180+
cur = cur == ' ' ? top : calc(top, cur, op)
181+
}
182+
if (op == '!') cur = cur == 't' ? 'f' : 't'
183+
idx1--; nums[idx1++] = cur
184+
}
185+
}
186+
return nums[idx1 - 1] == 't'
187+
}
188+
```
158189
* 时间复杂度:$O(n)$
159190
* 空间复杂度:$O(n)$
160191

LeetCode/1201-1210/1210. 穿过迷宫的最少移动次数(困难).md

+83-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ Tag : 「BFS」
88

99
你还记得那条风靡全球的贪吃蛇吗?
1010

11-
我们在一个 `n*n` 的网格上构建了新的迷宫地图,蛇的长度为 `2`,也就是说它会占去两个单元格。蛇会从左上角(`(0, 0)` 和 `(0, 1)`)开始移动。我们用 `0` 表示空单元格,用 `1` 表示障碍物。蛇需要移动到迷宫的右下角(`(n-1, n-2)` 和 `(n-1, n-1)`)。
11+
我们在一个 `n*n` 的网格上构建了新的迷宫地图,蛇的长度为 `2`,也就是说它会占去两个单元格。蛇会从左上角(`(0, 0)` 和 `(0, 1)`)开始移动。我们用 `0` 表示空单元格,用 `1` 表示障碍物。
12+
13+
蛇需要移动到迷宫的右下角(`(n-1, n-2)` 和 `(n-1, n-1)`)。
1214

1315
每次移动,蛇可以这样走:
1416

@@ -75,8 +77,7 @@ Tag : 「BFS」
7577

7678
在得到新蛇尾位置 $(nx, ny)$ 之后,计算新蛇头的位置 $(tx, ty)$。需要确保整条蛇没有越界,没有碰到障碍物,并且旋转转移时,额外检查 $(x + 1, y + 1)$ 位置是否合法。
7779

78-
79-
代码:
80+
Java 代码:
8081
```Java
8182
class Solution {
8283
int[][] dirs = new int[][]{{1,0,0},{0,1,0},{0,0,1}};
@@ -105,6 +106,85 @@ class Solution {
105106
}
106107
}
107108
```
109+
C++ 代码:
110+
```C++
111+
class Solution {
112+
public:
113+
int minimumMoves(vector<vector<int>>& g) {
114+
vector<vector<int>> dirs = {{1,0,0}, {0,1,0}, {0,0,1}};
115+
int n = g.size();
116+
queue<vector<int>> d;
117+
d.push({0, 0, 0, 0});
118+
vector<vector<vector<bool>>> vis(n, vector<vector<bool>>(n, vector<bool>(2, false)));
119+
vis[0][0][0] = true;
120+
while (!d.empty()) {
121+
vector<int> info = d.front();
122+
d.pop();
123+
int x = info[0], y = info[1], cd = info[2], step = info[3];
124+
for (vector<int>& dir : dirs) {
125+
int nx = x + dir[0], ny = y + dir[1], nd = cd ^ dir[2];
126+
int tx = nd == 0 ? nx : nx + 1, ty = nd == 0 ? ny + 1 : ny;
127+
if (nx >= n || ny >= n || tx >= n || ty >= n) continue;
128+
if (g[nx][ny] == 1 || g[tx][ty] == 1) continue;
129+
if (vis[nx][ny][nd]) continue;
130+
if (cd != nd && g[x + 1][y + 1] == 1) continue;
131+
if (nx == n - 1 && ny == n - 2 && nd == 0) return step + 1;
132+
d.push({nx, ny, nd, step + 1});
133+
vis[nx][ny][nd] = true;
134+
}
135+
}
136+
return -1;
137+
}
138+
};
139+
```
140+
Python 代码:
141+
```Python
142+
class Solution:
143+
def minimumMoves(self, g: List[List[int]]) -> int:
144+
dirs = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
145+
n = len(g)
146+
d = deque([(0,0,0,0)])
147+
vis = [[[0]*2 for _ in range(n)] for _ in range(n)]
148+
vis[0][0][0] = 1
149+
while d:
150+
x, y, cd, step = d.popleft()
151+
for dir in dirs:
152+
nx, ny, nd = x + dir[0], y + dir[1], cd ^ dir[2]
153+
tx, ty = nx + (nd == 1), ny + (nd == 0)
154+
if nx >= n or ny >= n or tx >= n or ty >= n: continue
155+
if g[nx][ny] == 1 or g[tx][ty] == 1: continue
156+
if vis[nx][ny][nd]: continue
157+
if cd != nd and g[x + 1][y + 1] == 1: continue
158+
if nx == n - 1 and ny == n - 2 and nd == 0: return step + 1
159+
d.append((nx, ny, nd, step + 1))
160+
vis[nx][ny][nd] = 1
161+
return -1
162+
```
163+
TypeScript 代码:
164+
```TypeScript
165+
function minimumMoves(g: number[][]): number {
166+
const n = g.length;
167+
const d: [number, number, number, number][] = [[0,0,0,0]];
168+
const vis: boolean[][][] = Array.from({ length: n }, () => Array.from({ length: n }, () => [false, false]));
169+
vis[0][0][0] = true;
170+
const dirs: [number, number, number][] = [[1,0,0], [0,1,0], [0,0,1]];
171+
while (d.length > 0) {
172+
const [x, y, cd, step] = d.shift()!;
173+
for (const dir of dirs) {
174+
const nx = x + dir[0], ny = y + dir[1], nd = cd ^ dir[2];
175+
const tx = nd === 0 ? nx : nx + 1, ty = nd === 0 ? ny + 1 : ny;
176+
if (nx >= n || ny >= n || tx >= n || ty >= n) continue;
177+
if (g[nx][ny] === 1 || g[tx][ty] === 1) continue
178+
if (vis[nx][ny][nd]) continue;
179+
if (cd !== nd && g[x + 1][y + 1] === 1) continue;
180+
if (nx === n - 1 && ny === n - 2 && nd === 0) return step + 1;
181+
d.push([nx, ny, nd, step + 1]);
182+
vis[nx][ny][nd] = true;
183+
}
184+
}
185+
return -1;
186+
};
187+
```
108188
* 时间复杂度:$O(n^2)$
109189
* 空间复杂度:$O(n^2 \times C)$,其中 $C = 2$ 代表蛇可变状态方向
110190

0 commit comments

Comments
 (0)