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 cd9e2fb

Browse files
✨feat: add 921
1 parent 67e327f commit cd9e2fb

File tree

4 files changed

+182
-9
lines changed

4 files changed

+182
-9
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
| [896. 单调数列](https://leetcode-cn.com/problems/monotonic-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/monotonic-array/solution/wei-shi-yao-yi-ci-bian-li-yao-bi-liang-c-uglp/) | 简单 | 🤩🤩🤩🤩 |
121121
| [905. 按奇偶排序数组](https://leetcode-cn.com/problems/sort-array-by-parity/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-array-by-parity/solution/by-ac_oier-nuz7/) | 简单 | 🤩🤩🤩 |
122122
| [919. 完全二叉树插入器](https://leetcode.cn/problems/complete-binary-tree-inserter/) | [LeetCode 题解链接](https://leetcode.cn/problems/complete-binary-tree-inserter/solution/by-ac_oier-t9dh/) | 中等 | 🤩🤩🤩🤩 |
123+
| [921. 使括号有效的最少添加](https://leetcode.cn/problems/minimum-add-to-make-parentheses-valid/) | [LeetCode 题解链接](https://leetcode.cn/problems/minimum-add-to-make-parentheses-valid/solution/by-ac_oier-9tn1/) | 中等 | 🤩🤩🤩🤩 |
123124
| [929. 独特的电子邮件地址](https://leetcode.cn/problems/unique-email-addresses/) | [LeetCode 题解链接](https://leetcode.cn/problems/unique-email-addresses/solution/by-ac_oier-d3zu/) | 简单 | 🤩🤩🤩 |
124125
| [944. 删列造序](https://leetcode.cn/problems/delete-columns-to-make-sorted/) | [LeetCode 题解链接](https://leetcode.cn/problems/delete-columns-to-make-sorted/solution/by-ac_oier-smoz/) | 简单 | 🤩🤩🤩 |
125126
| [946. 验证栈序列](https://leetcode.cn/problems/validate-stack-sequences/) | [LeetCode 题解链接](https://leetcode.cn/problems/validate-stack-sequences/solution/by-ac_oier-84qd/) | 中等 | 🤩🤩🤩🤩 |

‎LeetCode/31-40/31. 下一个排列(中等).md‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ Tag : 「模拟」、「数学」
4444

4545
---
4646

47-
### 朴素解法
47+
### 模拟
4848

49-
找下一个排列的数大家可以先想想大脑来是怎么完成这个找数的过程的。
49+
找下一个排列的数,大家可以先想想大脑来是怎么完成这个找数的过程的。
5050

5151
**我们会尽可能的将低位的数字变大,这样才符合「下一个排列」的定义。**
5252

@@ -80,8 +80,7 @@ Tag : 「模拟」、「数学」
8080
```Java
8181
class Solution {
8282
public void nextPermutation(int[] nums) {
83-
int n = nums.length;
84-
int k = n - 1;
83+
int n = nums.length, k = n - 1;
8584
while (k - 1 >= 0 && nums[k - 1] >= nums[k]) k--;
8685
if (k == 0) {
8786
reverse(nums, 0, n - 1);
@@ -94,9 +93,7 @@ class Solution {
9493
}
9594
void reverse(int[] nums, int a, int b) {
9695
int l = a, r = b;
97-
while (l < r) {
98-
swap(nums, l++, r--);
99-
}
96+
while (l < r) swap(nums, l++, r--);
10097
}
10198
void swap(int[] nums, int a, int b) {
10299
int c = nums[a];
@@ -105,7 +102,7 @@ class Solution {
105102
}
106103
}
107104
```
108-
* 时间复杂度:对数组线性遍历。复杂度为 $O(n)$
105+
* 时间复杂度:$O(n)$
109106
* 空间复杂度:$O(1)$
110107

111108
---
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[921. 使括号有效的最少添加](https://leetcode.cn/problems/minimum-add-to-make-parentheses-valid/solution/by-ac_oier-9tn1/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
只有满足下面几点之一,括号字符串才是有效的:
10+
11+
* 它是一个空字符串,或者
12+
* 它可以被写成 `AB` (`A``B` 连接), 其中 `A``B` 都是有效字符串,或者
13+
* 它可以被写作 (`A`),其中 `A` 是有效字符串。
14+
15+
给定一个括号字符串 `s` ,移动 `N` 次,你就可以在字符串的任何位置插入一个括号。
16+
17+
* 例如,如果 `s = "()))"` ,你可以插入一个开始括号为 `"(()))"` 或结束括号为 `"())))"`
18+
19+
返回 为使结果字符串 `s` 有效而必须添加的最少括号数。
20+
21+
22+
示例 1:
23+
```
24+
输入:s = "())"
25+
26+
输出:1
27+
```
28+
示例 2:
29+
```
30+
输入:s = "((("
31+
32+
输出:3
33+
```
34+
35+
提示:
36+
* 1ドル <= s.length <= 1000$
37+
* `s` 只包含 `'('``')'` 字符
38+
39+
---
40+
41+
### 模拟
42+
43+
一个介绍过很多次的做法 : 将「有效括号问题」转化为「分值有效性」的数学判定。
44+
45+
使用 `score` 代指处理过程中的得分,将 `(` 记为 `+1`,将 `)` 记为 `-1`
46+
47+
一个有效的括号应当在整个过程中不出现负数,因此一旦 `score` 出现负数,我们需要马上增加 `(` 来确保合法性;当整个 `s` 处理完后,还需要添加 `socre` 等同的 `)` 来确保合法性。
48+
49+
Java 代码:
50+
```Java
51+
class Solution {
52+
public int minAddToMakeValid(String s) {
53+
int score = 0, ans = 0;
54+
for (char c : s.toCharArray()) {
55+
score += c == '(' ? 1 : -1;
56+
if (score < 0) {
57+
score = 0; ans++;
58+
}
59+
}
60+
return ans + score;
61+
}
62+
}
63+
```
64+
TypeScript 代码:
65+
```TypeScript
66+
function minAddToMakeValid(s: string): number {
67+
let scroe = 0, ans = 0
68+
for (const c of s) {
69+
scroe += c == '(' ? 1 : -1
70+
if (scroe < 0) {
71+
scroe = 0; ans++
72+
}
73+
}
74+
return ans + scroe
75+
};
76+
```
77+
Python 代码:
78+
```Python
79+
class Solution:
80+
def minAddToMakeValid(self, s: str) -> int:
81+
score, ans = 0, 0
82+
for c in s:
83+
score += 1 if c == '(' else -1
84+
if score < 0:
85+
score = 0
86+
ans += 1
87+
return ans + score
88+
```
89+
* 时间复杂度:$O(n)$
90+
* 空间复杂度:使用 `charAt` 代替 `toCharArray` 操作,复杂度为 $O(1)$
91+
92+
---
93+
94+
### 最后
95+
96+
这是我们「刷穿 LeetCode」系列文章的第 `No.921` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
97+
98+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
99+
100+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
101+
102+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
103+

‎LeetCode/面试题/面试题 17.19. 消失的两个数字(困难).md‎

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
这是 LeetCode 上的 **[面试题 17.19. 消失的两个数字](https://leetcode.cn/problems/missing-two-lcci/solution/by-ac_oier-pgeh/)** ,难度为 **困难**
44

5-
Tag : 「数学」
5+
Tag : 「数学」、「位运算」
66

77

88

@@ -78,6 +78,78 @@ class Solution:
7878

7979
---
8080

81+
### 异或
82+
83+
另外一类求解方法是利用「异或」+「lowbit」。
84+
85+
由于我们明确了是在 $[1, n + 2]$ 中缺失了两个数,我们可以先通过异或 $[1, n + 2]$ 以及所有的 $nums[i]$ 来得到缺失两个数值异或和 `t`
86+
87+
我们知道异或结果二进制表示为 1ドル$ 代表了两缺失值该位置数值不同(一个该位置 0ドル,ドル另一个该位置为 1ドル$),我们可以根据异或和 `t` 中任意一位为 1ドル$ 的位置来将两个缺失值划分到两组中。
88+
89+
更加优雅的方式是使用 `lowbit` 操作:`d = t & -t` 可快速得到只保留 `t` 中最低位 `1` 的对应数值。
90+
91+
随后将 $[1, n + 2]$ 中满足 `i & d != 0` 的所有 `i`(含义为对应位置为 1ドル$ 的数值)与 $nums[i]$ 中满足 `nums[i] & d != 0` 的所有 $nums[i]$(含义为对应位置为 1ドル$ 的数值) 进行异或,最终能够确定其中一个缺失值,再结合最开始的异或和 `t` 即可确定另外一个缺失值。
92+
93+
Java 代码:
94+
```Java
95+
class Solution {
96+
public int[] missingTwo(int[] nums) {
97+
int n = nums.length + 2, cur = 0;
98+
for (int i = 1; i <= n; i++) cur ^= i;
99+
for (int x : nums) cur ^= x;
100+
int t = cur, d = cur & -cur;
101+
cur = 0;
102+
for (int i = 1; i <= n; i++) {
103+
if ((d & i) != 0) cur ^= i;
104+
}
105+
for (int x : nums) {
106+
if ((d & x) != 0) cur ^= x;
107+
}
108+
return new int[]{cur, t ^ cur};
109+
}
110+
}
111+
```
112+
TypeScript 代码:
113+
```TypeScript
114+
function missingTwo(nums: number[]): number[] {
115+
let n = nums.length + 2, cur = 0
116+
for (let i = 1; i <= n; i++) cur ^= i
117+
for (let x of nums) cur ^= x
118+
const t = cur, d = cur & -cur
119+
cur = 0
120+
for (let i = 1; i <= n; i++) {
121+
if ((d & i) != 0) cur ^= i
122+
}
123+
for (let x of nums) {
124+
if ((d & x) != 0) cur ^= x
125+
}
126+
return [cur, t ^ cur]
127+
};
128+
```
129+
Python 代码:
130+
```Python
131+
class Solution:
132+
def missingTwo(self, nums: List[int]) -> List[int]:
133+
n, cur = len(nums) + 2, 0
134+
for i in range(1, n + 1):
135+
cur ^= i
136+
for x in nums:
137+
cur ^= x
138+
t, d = cur, cur & -cur
139+
cur = 0
140+
for i in range(1, n + 1):
141+
if d & i:
142+
cur ^= i
143+
for x in nums:
144+
if d & x:
145+
cur ^= x
146+
return [cur, t ^ cur]
147+
```
148+
* 时间复杂度:$O(n)$
149+
* 空间复杂度:$O(1)$
150+
151+
---
152+
81153
### 最后
82154

83155
这是我们「刷穿 LeetCode」系列文章的第 `No.面试题 17.19` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

0 commit comments

Comments
(0)

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