Skip to content

Commit 24e882c

Browse files
authored
feat: add solutions to lc problems (doocs#2105)
1 parent 8b5b868 commit 24e882c

File tree

17 files changed

+444
-149
lines changed

17 files changed

+444
-149
lines changed

solution/0000-0099/0066.Plus One/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:模拟**
54+
55+
我们从数组的最后一个元素开始遍历,将当前元素加一,然后对 $10$ 取模,如果取模后的结果不为 $0$,说明当前元素没有进位,直接返回数组即可。否则,当前元素为 $0$,需要进位,继续遍历前一个元素,重复上述操作。如果遍历完数组后,仍然没有返回,说明数组中所有元素都为 $0$,需要在数组的头部插入一个 $1$。
56+
57+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**

solution/0000-0099/0066.Plus One/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ Thus, the result should be [1,0].
5050

5151
## Solutions
5252

53+
**Solution 1: Simulation**
54+
55+
We start traversing from the last element of the array, add one to the current element, and then take the modulus by $10$. If the result is not $0$, it means that there is no carry for the current element, and we can directly return the array. Otherwise, the current element is $0$ and needs to be carried over. We continue to traverse the previous element and repeat the above operation. If we still haven't returned after traversing the array, it means that all elements in the array are $0$, and we need to insert a $1$ at the beginning of the array.
56+
57+
The time complexity is $O(n)$, where $n$ is the length of the array. Ignoring the space consumption of the answer, the space complexity is $O(1)$.
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**

solution/0200-0299/0242.Valid Anagram/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ class Solution:
6969
return True
7070
```
7171

72+
```python
73+
class Solution:
74+
def isAnagram(self, s: str, t: str) -> bool:
75+
return Counter(s) == Counter(t)
76+
```
77+
7278
### **Java**
7379

7480
<!-- 这里可写当前语言的特殊实现逻辑 -->

solution/0200-0299/0242.Valid Anagram/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ class Solution:
5454
return True
5555
```
5656

57+
```python
58+
class Solution:
59+
def isAnagram(self, s: str, t: str) -> bool:
60+
return Counter(s) == Counter(t)
61+
```
62+
5763
### **Java**
5864

5965
```java

solution/0200-0299/0283.Move Zeroes/README_EN.md

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929

3030
## Solutions
3131

32+
**Solution 1: Two Pointers**
33+
34+
We use two pointers $i$ and $j$, where pointer $i$ points to the end of the sequence that has been processed, and pointer $j$ points to the head of the sequence to be processed. Initially, $i=-1$.
35+
36+
Next, we traverse $j \in [0,n)$, if $nums[j] \neq 0$, then we swap the next number pointed by pointer $i$ with $nums[j]$, and move $i$ forward. Continue to traverse until $j$ reaches the end of the array, all non-zero elements of the array are moved to the front of the array in the original order, and all zero elements are moved to the end of the array.
37+
38+
The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
39+
3240
<!-- tabs:start -->
3341

3442
### **Python3**

solution/0300-0399/0389.Find the Difference/README.md

+68-59
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,15 @@
4545

4646
**方法一:计数**
4747

48-
使用数组(`cnt`)统计 `s``t` 当中字符出现的次数:`s[i]` 进行 `cnt[s[i] - 'a']++``t[i]` 进行 `cnt[t[i] - 'a']--`
48+
我们可以用一个哈希表或数组 $cnt$ 统计字符串 $s$ 中每个字符出现的次数,再遍历字符串 $t$,对于每个字符,我们在 $cnt$ 中减去一次出现的次数,如果对应次数为负数,则说明该字符在 $t$ 中出现的次数大于在 $s$ 中出现的次数,因此该字符为被添加的字符
4949

50-
完成统计后,找到符合 `cnt[i] == -1``i`,返回即可(`return 'a' + i`)。
51-
52-
时间复杂度 $O(n)$,空间复杂度 $O(C)$。本题中 $C=26$。
50+
时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$,其中 $n$ 为字符串的长度,而 $\Sigma$ 表示字符集,这里字符集为所有小写字母,所以 $|\Sigma|=26$。
5351

5452
**方法二:求和**
5553

56-
由于 `s``t` 只存在一个不同元素,可以统计两者所有字符 ASCII 码之和,再进行相减(`sum(t) - sum(s)`),即可得到 `t` 中那一个额外字符的 ASCII
54+
我们可以将字符串 $t$ 中每个字符的 ASCII 码的值求和,再减去字符串 $s$ 中每个字符的 ASCII 码的值求和,最后的结果即为被添加的字符的 ASCII 码对应的值
5755

58-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
56+
时间复杂度 $O(n)$,其中 $n$ 为字符串的长度,空间复杂度 $O(1)$。
5957

6058
<!-- tabs:start -->
6159

@@ -122,10 +120,15 @@ class Solution {
122120
class Solution {
123121
public:
124122
char findTheDifference(string s, string t) {
125-
int cnt[26] = {0};
126-
for (char& c : s) ++cnt[c - 'a'];
127-
for (char& c : t)
128-
if (--cnt[c - 'a'] < 0) return c;
123+
int cnt[26]{};
124+
for (char& c : s) {
125+
++cnt[c - 'a'];
126+
}
127+
for (char& c : t) {
128+
if (--cnt[c - 'a'] < 0) {
129+
return c;
130+
}
131+
}
129132
return ' ';
130133
}
131134
};
@@ -136,25 +139,64 @@ class Solution {
136139
public:
137140
char findTheDifference(string s, string t) {
138141
int a = 0, b = 0;
139-
for (char& c : s) a += c;
140-
for (char& c : t) b += c;
142+
for (char& c : s) {
143+
a += c;
144+
}
145+
for (char& c : t) {
146+
b += c;
147+
}
141148
return b - a;
142149
}
143150
};
144151
```
145152

153+
### **Go**
154+
155+
```go
156+
func findTheDifference(s, t string) byte {
157+
cnt := [26]int{}
158+
for _, ch := range s {
159+
cnt[ch-'a']++
160+
}
161+
for i := 0; ; i++ {
162+
ch := t[i]
163+
cnt[ch-'a']--
164+
if cnt[ch-'a'] < 0 {
165+
return ch
166+
}
167+
}
168+
}
169+
```
170+
171+
```go
172+
func findTheDifference(s string, t string) byte {
173+
ss := 0
174+
for _, c := range s {
175+
ss -= int(c)
176+
}
177+
for _, c := range t {
178+
ss += int(c)
179+
}
180+
return byte(ss)
181+
}
182+
```
183+
146184
### **TypeScript**
147185

148186
```ts
149187
function findTheDifference(s: string, t: string): string {
150-
const n = s.length;
151-
const count = new Array(26).fill(0);
152-
for (let i = 0; i < n; i++) {
153-
count[s.charCodeAt(i) - 'a'.charCodeAt(0)]++;
154-
count[t.charCodeAt(i) - 'a'.charCodeAt(0)]--;
188+
const cnt: number[] = Array(26).fill(0);
189+
for (const c of s) {
190+
++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
191+
}
192+
for (const c of t) {
193+
--cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
194+
}
195+
for (let i = 0; ; ++i) {
196+
if (cnt[i] < 0) {
197+
return String.fromCharCode(i + 'a'.charCodeAt(0));
198+
}
155199
}
156-
count[t.charCodeAt(n) - 'a'.charCodeAt(0)]--;
157-
return String.fromCharCode('a'.charCodeAt(0) + count.findIndex(v => v !== 0));
158200
}
159201
```
160202

@@ -214,19 +256,17 @@ impl Solution {
214256
```c
215257
char findTheDifference(char* s, char* t) {
216258
int n = strlen(s);
217-
int count[26] = {0};
259+
int cnt[26] = {0};
218260
for (int i = 0; i < n; i++) {
219-
count[s[i] - 'a']++;
220-
count[t[i] - 'a']--;
261+
cnt[s[i] - 'a']++;
262+
cnt[t[i] - 'a']--;
221263
}
222-
count[t[n] - 'a']--;
223-
int i;
224-
for (i = 0; i < 26; i++) {
225-
if (count[i]) {
226-
break;
264+
cnt[t[n] - 'a']--;
265+
for (int i = 0;; i++) {
266+
if (cnt[i]) {
267+
return 'a' + i;
227268
}
228269
}
229-
return 'a' + i;
230270
}
231271
```
232272
@@ -243,37 +283,6 @@ char findTheDifference(char* s, char* t) {
243283
}
244284
```
245285

246-
### **Go**
247-
248-
```go
249-
func findTheDifference(s, t string) byte {
250-
cnt := [26]int{}
251-
for _, ch := range s {
252-
cnt[ch-'a']++
253-
}
254-
for i := 0; ; i++ {
255-
ch := t[i]
256-
cnt[ch-'a']--
257-
if cnt[ch-'a'] < 0 {
258-
return ch
259-
}
260-
}
261-
}
262-
```
263-
264-
```go
265-
func findTheDifference(s string, t string) byte {
266-
ss := 0
267-
for _, c := range s {
268-
ss -= int(c)
269-
}
270-
for _, c := range t {
271-
ss += int(c)
272-
}
273-
return byte(ss)
274-
}
275-
```
276-
277286
### **...**
278287

279288
```

0 commit comments

Comments
 (0)