Skip to content

✨ feat: add 1106、1210、1222、1245、11465、1766、1781、235、2335、2336、583、621… #788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 55 additions & 24 deletions LeetCode/1101-1110/1106. 解析布尔表达式(困难).md
Original file line number Diff line number Diff line change
Expand Up @@ -98,33 +98,36 @@ class Solution {
}
}
```
TypeScript 代码:
```TypeScript
function parseBoolExpr(s: string): boolean {
function calc(a: string, b: string, op: string): string {
const x = a == 't', y = b == 't'
const ans = op == '|' ? x || y : x && y
return ans ? 't' : 'f'
}
const nums = new Array<string>(s.length).fill(''), ops = new Array<string>(s.length).fill('')
let idx1 = 0, idx2 = 0
for (const c of s) {
if (c == ',') continue
if (c == 't' || c == 'f') nums[idx1++] = c
if (c == '|' || c == '&' || c == '!') ops[idx2++] = c
if (c == '(') nums[idx1++] = '-'
if (c == ')') {
let op = ops[--idx2], cur = ' '
while (idx1 > 0 && nums[idx1 - 1] != '-') {
const top = nums[--idx1]
cur = cur == ' ' ? top : calc(top, cur, op)
C++ 代码:
```C++
class Solution {
public:
bool parseBoolExpr(string s) {
deque<char> nums, ops;
for (char c : s) {
if (c == ',') continue;
if (c == 't' || c == 'f') nums.push_back(c);
if (c == '|' || c == '&' || c == '!') ops.push_back(c);
if (c == '(') nums.push_back('-');
if (c == ')') {
char op = ops.back(); ops.pop_back();
char cur = ' ';
while (!nums.empty() && nums.back() != '-') {
char top = nums.back(); nums.pop_back();
cur = cur == ' ' ? top : calc(top, cur, op);
}
if (op == '!') cur = cur == 't' ? 'f' : 't';
nums.pop_back(); nums.push_back(cur);
}
if (op == '!') cur = cur == 't' ? 'f' : 't'
idx1--; nums[idx1++] = cur
}
return nums.back() == 't';
}
return nums[idx1 - 1] == 't'
}
char calc(char a, char b, char op) {
bool x = a == 't', y = b == 't';
bool ans = op == '|' ? x | y : x & y;
return ans ? 't' : 'f';
}
};
```
Python 代码:
```Python
Expand Down Expand Up @@ -155,6 +158,34 @@ class Solution:
nums.append(cur)
return nums[-1] == 't'
```
TypeScript 代码:
```TypeScript
function parseBoolExpr(s: string): boolean {
function calc(a: string, b: string, op: string): string {
const x = a == 't', y = b == 't'
const ans = op == '|' ? x || y : x && y
return ans ? 't' : 'f'
}
const nums = new Array<string>(s.length).fill(''), ops = new Array<string>(s.length).fill('')
let idx1 = 0, idx2 = 0
for (const c of s) {
if (c == ',') continue
if (c == 't' || c == 'f') nums[idx1++] = c
if (c == '|' || c == '&' || c == '!') ops[idx2++] = c
if (c == '(') nums[idx1++] = '-'
if (c == ')') {
let op = ops[--idx2], cur = ' '
while (idx1 > 0 && nums[idx1 - 1] != '-') {
const top = nums[--idx1]
cur = cur == ' ' ? top : calc(top, cur, op)
}
if (op == '!') cur = cur == 't' ? 'f' : 't'
idx1--; nums[idx1++] = cur
}
}
return nums[idx1 - 1] == 't'
}
```
* 时间复杂度:$O(n)$
* 空间复杂度:$O(n)$

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ Tag : 「BFS」

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

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

蛇需要移动到迷宫的右下角(`(n-1, n-2)` 和 `(n-1, n-1)`)。

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

Expand Down Expand Up @@ -75,8 +77,7 @@ Tag : 「BFS」

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


代码:
Java 代码:
```Java
class Solution {
int[][] dirs = new int[][]{{1,0,0},{0,1,0},{0,0,1}};
Expand Down Expand Up @@ -105,6 +106,85 @@ class Solution {
}
}
```
C++ 代码:
```C++
class Solution {
public:
int minimumMoves(vector<vector<int>>& g) {
vector<vector<int>> dirs = {{1,0,0}, {0,1,0}, {0,0,1}};
int n = g.size();
queue<vector<int>> d;
d.push({0, 0, 0, 0});
vector<vector<vector<bool>>> vis(n, vector<vector<bool>>(n, vector<bool>(2, false)));
vis[0][0][0] = true;
while (!d.empty()) {
vector<int> info = d.front();
d.pop();
int x = info[0], y = info[1], cd = info[2], step = info[3];
for (vector<int>& dir : dirs) {
int nx = x + dir[0], ny = y + dir[1], nd = cd ^ dir[2];
int tx = nd == 0 ? nx : nx + 1, ty = nd == 0 ? ny + 1 : ny;
if (nx >= n || ny >= n || tx >= n || ty >= n) continue;
if (g[nx][ny] == 1 || g[tx][ty] == 1) continue;
if (vis[nx][ny][nd]) continue;
if (cd != nd && g[x + 1][y + 1] == 1) continue;
if (nx == n - 1 && ny == n - 2 && nd == 0) return step + 1;
d.push({nx, ny, nd, step + 1});
vis[nx][ny][nd] = true;
}
}
return -1;
}
};
```
Python 代码:
```Python
class Solution:
def minimumMoves(self, g: List[List[int]]) -> int:
dirs = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]
n = len(g)
d = deque([(0,0,0,0)])
vis = [[[0]*2 for _ in range(n)] for _ in range(n)]
vis[0][0][0] = 1
while d:
x, y, cd, step = d.popleft()
for dir in dirs:
nx, ny, nd = x + dir[0], y + dir[1], cd ^ dir[2]
tx, ty = nx + (nd == 1), ny + (nd == 0)
if nx >= n or ny >= n or tx >= n or ty >= n: continue
if g[nx][ny] == 1 or g[tx][ty] == 1: continue
if vis[nx][ny][nd]: continue
if cd != nd and g[x + 1][y + 1] == 1: continue
if nx == n - 1 and ny == n - 2 and nd == 0: return step + 1
d.append((nx, ny, nd, step + 1))
vis[nx][ny][nd] = 1
return -1
```
TypeScript 代码:
```TypeScript
function minimumMoves(g: number[][]): number {
const n = g.length;
const d: [number, number, number, number][] = [[0,0,0,0]];
const vis: boolean[][][] = Array.from({ length: n }, () => Array.from({ length: n }, () => [false, false]));
vis[0][0][0] = true;
const dirs: [number, number, number][] = [[1,0,0], [0,1,0], [0,0,1]];
while (d.length > 0) {
const [x, y, cd, step] = d.shift()!;
for (const dir of dirs) {
const nx = x + dir[0], ny = y + dir[1], nd = cd ^ dir[2];
const tx = nd === 0 ? nx : nx + 1, ty = nd === 0 ? ny + 1 : ny;
if (nx >= n || ny >= n || tx >= n || ty >= n) continue;
if (g[nx][ny] === 1 || g[tx][ty] === 1) continue
if (vis[nx][ny][nd]) continue;
if (cd !== nd && g[x + 1][y + 1] === 1) continue;
if (nx === n - 1 && ny === n - 2 && nd === 0) return step + 1;
d.push([nx, ny, nd, step + 1]);
vis[nx][ny][nd] = true;
}
}
return -1;
};
```
* 时间复杂度:$O(n^2)$
* 空间复杂度:$O(n^2 \times C)$,其中 $C = 2$ 代表蛇可变状态方向

Expand Down
Loading