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 72ab1fd

Browse files
authored
feat: add solutions to lc problems: No.0369,0387,0434 (doocs#4270)
1 parent 1cd3ae2 commit 72ab1fd

File tree

15 files changed

+301
-75
lines changed

15 files changed

+301
-75
lines changed

‎solution/0300-0399/0369.Plus One Linked List/README.md‎

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ tags:
5757

5858
### 方法一:链表遍历
5959

60-
我们先设置一个虚拟头节点 `dummy`,初始值为 $0,ドル指向链表头节点 `head`
60+
我们先设置一个虚拟头节点 $\textit{dummy},ドル初始时 $\textit{dummy}$ 的值为 $0,ドル并且 $\textit{dummy}$ 的后继节点为链表 $\textit{head}$
6161

62-
然后从链表头节点开始遍历,找出链表最后一个值不等于 9ドル$ 的节点`target`,将 `target` 的值加 1ドル$。接着将 `target` 之后的所有节点值置为 0ドル$。
62+
接下来,我们从虚拟头节点开始遍历链表,找到最后一个不为 9ドル$ 的节点,将其值加 1ドル$,并将该节点之后的所有节点的值置为 0ドル$。
6363

64-
需要注意的是,如果链表中所有节点值都为 9ドル,ドル那么遍历结束后,`target` 会指向空节点,这时我们需要将 `dummy` 的值加 1ドル,ドル然后返回 `dummy`,否则返回 `dummy` 的下一个节点
64+
最后,我们判断虚拟头节点的值是否为 1ドル,ドル如果为 1ドル,ドル则返回 $\textit{dummy}$,否则返回 $\textit{dummy}$ 的后继节点
6565

66-
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 为链表的长度
66+
时间复杂度 $O(n),ドル其中 $n$ 是链表的长度。空间复杂度 $O(1)$。
6767

6868
<!-- tabs:start -->
6969

@@ -76,7 +76,7 @@ tags:
7676
# self.val = val
7777
# self.next = next
7878
class Solution:
79-
def plusOne(self, head: ListNode) -> ListNode:
79+
def plusOne(self, head: Optional[ListNode]) -> Optional[ListNode]:
8080
dummy = ListNode(0, head)
8181
target = dummy
8282
while head:
@@ -143,17 +143,16 @@ public:
143143
ListNode* plusOne(ListNode* head) {
144144
ListNode* dummy = new ListNode(0, head);
145145
ListNode* target = dummy;
146-
while (head) {
147-
if (head->val != 9) target = head;
148-
head = head->next;
146+
for (; head; head = head->next) {
147+
if (head->val != 9) {
148+
target = head;
149+
}
149150
}
150-
++target->val;
151-
target = target->next;
152-
while (target) {
151+
target->val++;
152+
for (target = target->next; target; target = target->next) {
153153
target->val = 0;
154-
target = target->next;
155154
}
156-
return dummy->val == 1 ? dummy : dummy->next;
155+
return dummy->val ? dummy : dummy->next;
157156
}
158157
};
159158
```
@@ -178,10 +177,8 @@ func plusOne(head *ListNode) *ListNode {
178177
head = head.Next
179178
}
180179
target.Val++
181-
target = target.Next
182-
for target != nil {
180+
for target = target.Next; target != nil; target = target.Next {
183181
target.Val = 0
184-
target = target.Next
185182
}
186183
if dummy.Val == 1 {
187184
return dummy
@@ -190,6 +187,38 @@ func plusOne(head *ListNode) *ListNode {
190187
}
191188
```
192189

190+
#### TypeScript
191+
192+
```ts
193+
/**
194+
* Definition for singly-linked list.
195+
* class ListNode {
196+
* val: number
197+
* next: ListNode | null
198+
* constructor(val?: number, next?: ListNode | null) {
199+
* this.val = (val===undefined ? 0 : val)
200+
* this.next = (next===undefined ? null : next)
201+
* }
202+
* }
203+
*/
204+
205+
function plusOne(head: ListNode | null): ListNode | null {
206+
const dummy = new ListNode(0, head);
207+
let target = dummy;
208+
while (head) {
209+
if (head.val !== 9) {
210+
target = head;
211+
}
212+
head = head.next;
213+
}
214+
target.val++;
215+
for (target = target.next; target; target = target.next) {
216+
target.val = 0;
217+
}
218+
return dummy.val ? dummy : dummy.next;
219+
}
220+
```
221+
193222
<!-- tabs:end -->
194223

195224
<!-- solution:end -->

‎solution/0300-0399/0369.Plus One Linked List/README_EN.md‎

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,15 @@ tags:
4444

4545
<!-- solution:start -->
4646

47-
### Solution 1
47+
### Solution 1: Linked List Traversal
48+
49+
We first set a dummy head node $\textit{dummy},ドル initially with a value of 0ドル,ドル and the successor node of $\textit{dummy}$ is the linked list $\textit{head}$.
50+
51+
Next, we traverse the linked list starting from the dummy head node, find the last node that is not 9ドル,ドル increment its value by 1ドル,ドル and set the values of all nodes after this node to 0ドル$.
52+
53+
Finally, we check if the value of the dummy head node is 1ドル$. If it is 1ドル,ドル we return $\textit{dummy}$; otherwise, we return the successor node of $\textit{dummy}$.
54+
55+
The time complexity is $O(n),ドル where $n$ is the length of the linked list. The space complexity is $O(1)$.
4856

4957
<!-- tabs:start -->
5058

@@ -57,7 +65,7 @@ tags:
5765
# self.val = val
5866
# self.next = next
5967
class Solution:
60-
def plusOne(self, head: ListNode) -> ListNode:
68+
def plusOne(self, head: Optional[ListNode]) -> Optional[ListNode]:
6169
dummy = ListNode(0, head)
6270
target = dummy
6371
while head:
@@ -124,17 +132,16 @@ public:
124132
ListNode* plusOne(ListNode* head) {
125133
ListNode* dummy = new ListNode(0, head);
126134
ListNode* target = dummy;
127-
while (head) {
128-
if (head->val != 9) target = head;
129-
head = head->next;
135+
for (; head; head = head->next) {
136+
if (head->val != 9) {
137+
target = head;
138+
}
130139
}
131-
++target->val;
132-
target = target->next;
133-
while (target) {
140+
target->val++;
141+
for (target = target->next; target; target = target->next) {
134142
target->val = 0;
135-
target = target->next;
136143
}
137-
return dummy->val == 1 ? dummy : dummy->next;
144+
return dummy->val ? dummy : dummy->next;
138145
}
139146
};
140147
```
@@ -159,10 +166,8 @@ func plusOne(head *ListNode) *ListNode {
159166
head = head.Next
160167
}
161168
target.Val++
162-
target = target.Next
163-
for target != nil {
169+
for target = target.Next; target != nil; target = target.Next {
164170
target.Val = 0
165-
target = target.Next
166171
}
167172
if dummy.Val == 1 {
168173
return dummy
@@ -171,6 +176,38 @@ func plusOne(head *ListNode) *ListNode {
171176
}
172177
```
173178

179+
#### TypeScript
180+
181+
```ts
182+
/**
183+
* Definition for singly-linked list.
184+
* class ListNode {
185+
* val: number
186+
* next: ListNode | null
187+
* constructor(val?: number, next?: ListNode | null) {
188+
* this.val = (val===undefined ? 0 : val)
189+
* this.next = (next===undefined ? null : next)
190+
* }
191+
* }
192+
*/
193+
194+
function plusOne(head: ListNode | null): ListNode | null {
195+
const dummy = new ListNode(0, head);
196+
let target = dummy;
197+
while (head) {
198+
if (head.val !== 9) {
199+
target = head;
200+
}
201+
head = head.next;
202+
}
203+
target.val++;
204+
for (target = target.next; target; target = target.next) {
205+
target.val = 0;
206+
}
207+
return dummy.val ? dummy : dummy.next;
208+
}
209+
```
210+
174211
<!-- tabs:end -->
175212

176213
<!-- solution:end -->

‎solution/0300-0399/0369.Plus One Linked List/Solution.cpp‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@ class Solution {
1313
ListNode* plusOne(ListNode* head) {
1414
ListNode* dummy = new ListNode(0, head);
1515
ListNode* target = dummy;
16-
while (head) {
17-
if (head->val != 9) target = head;
18-
head = head->next;
16+
for (; head; head = head->next) {
17+
if (head->val != 9) {
18+
target = head;
19+
}
1920
}
20-
++target->val;
21-
target = target->next;
22-
while (target) {
21+
target->val++;
22+
for (target = target->next; target; target = target->next) {
2323
target->val = 0;
24-
target = target->next;
2524
}
26-
return dummy->val == 1? dummy : dummy->next;
25+
return dummy->val ? dummy : dummy->next;
2726
}
2827
};

‎solution/0300-0399/0369.Plus One Linked List/Solution.go‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ func plusOne(head *ListNode) *ListNode {
1515
head = head.Next
1616
}
1717
target.Val++
18-
target = target.Next
19-
for target != nil {
18+
for target = target.Next; target != nil; target = target.Next {
2019
target.Val = 0
21-
target = target.Next
2220
}
2321
if dummy.Val == 1 {
2422
return dummy

‎solution/0300-0399/0369.Plus One Linked List/Solution.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# self.val = val
55
# self.next = next
66
class Solution:
7-
def plusOne(self, head: ListNode) -> ListNode:
7+
def plusOne(self, head: Optional[ListNode]) -> Optional[ListNode]:
88
dummy = ListNode(0, head)
99
target = dummy
1010
while head:
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
function plusOne(head: ListNode | null): ListNode | null {
14+
const dummy = new ListNode(0, head);
15+
let target = dummy;
16+
while (head) {
17+
if (head.val !== 9) {
18+
target = head;
19+
}
20+
head = head.next;
21+
}
22+
target.val++;
23+
for (target = target.next; target; target = target.next) {
24+
target.val = 0;
25+
}
26+
return dummy.val ? dummy : dummy.next;
27+
}

‎solution/0300-0399/0387.First Unique Character in a String/README.md‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,13 @@ tags:
5959

6060
<!-- solution:start -->
6161

62-
### 方法一:数组或哈希表
62+
### 方法一:计数
6363

64-
我们可以用数组或哈希表 $cnt$ 记录字符串 $s$ 中每个字符出现的次数
64+
我们用一个哈希表或者一个长度为 26ドル$ 的数组 $\text{cnt}$ 来存储每个字符出现的次数,然后从头开始遍历每个字符 $\text{s[i]},ドル如果 $\text{cnt[s[i]]}$ 为 1ドル,ドル则返回 $i$
6565

66-
然后我们再遍历字符串 $s,ドル当遍历到某个字符 $c$ 时,如果 $cnt[c]=1,ドル则说明 $c$ 是第一个不重复的字符,返回它的索引即可
66+
遍历结束后,如果没有找到符合条件的字符,返回 $-1$
6767

68-
如果遍历完字符串 $s$ 仍然没有找到不重复的字符,返回 $-1$。
69-
70-
时间复杂度 $O(n),ドル空间复杂度 $O(\Sigma),ドル其中 $\Sigma$ 是字符集的大小。
68+
时间复杂度 $O(n),ドル其中 $n$ 是字符串的长度。空间复杂度 $O(|\Sigma|),ドル其中 $\Sigma$ 是字符集,本题中字符集为小写字母,所以 $|\Sigma|=26$。
7169

7270
<!-- tabs:start -->
7371

@@ -145,12 +143,12 @@ func firstUniqChar(s string) int {
145143

146144
```ts
147145
function firstUniqChar(s: string): number {
148-
const cnt = new Array(26).fill(0);
146+
const cnt = new Map<string, number>();
149147
for (const c of s) {
150-
cnt[c.charCodeAt(0) -97]++;
148+
cnt.set(c, (cnt.get(c) ||0) +1);
151149
}
152-
for (let i = 0; i < s.length; i++) {
153-
if (cnt[s.charCodeAt(i) -97] === 1) {
150+
for (let i = 0; i < s.length; ++i) {
151+
if (cnt.get(s[i]) === 1) {
154152
return i;
155153
}
156154
}
@@ -166,12 +164,12 @@ function firstUniqChar(s: string): number {
166164
* @return {number}
167165
*/
168166
var firstUniqChar = function (s) {
169-
const cnt = new Array(26).fill(0);
167+
const cnt = new Map();
170168
for (const c of s) {
171-
++cnt[c.charCodeAt() -'a'.charCodeAt()];
169+
cnt.set(c, (cnt.get(c) ||0) +1);
172170
}
173171
for (let i = 0; i < s.length; ++i) {
174-
if (cnt[s[i].charCodeAt() -'a'.charCodeAt()] === 1) {
172+
if (cnt.get(s[i]) === 1) {
175173
return i;
176174
}
177175
}

‎solution/0300-0399/0387.First Unique Character in a String/README_EN.md‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ tags:
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Counting
68+
69+
We use a hash table or an array of length 26ドル$ $\text{cnt}$ to store the frequency of each character. Then, we traverse each character $\text{s[i]}$ from the beginning. If $\text{cnt[s[i]]}$ is 1ドル,ドル we return $i$.
70+
71+
If no such character is found after the traversal, we return $-1$.
72+
73+
The time complexity is $O(n),ドル where $n$ is the length of the string. The space complexity is $O(|\Sigma|),ドル where $\Sigma$ is the character set. In this problem, the character set consists of lowercase letters, so $|\Sigma|=26$.
6874

6975
<!-- tabs:start -->
7076

@@ -142,12 +148,12 @@ func firstUniqChar(s string) int {
142148

143149
```ts
144150
function firstUniqChar(s: string): number {
145-
const cnt = new Array(26).fill(0);
151+
const cnt = new Map<string, number>();
146152
for (const c of s) {
147-
cnt[c.charCodeAt(0) -97]++;
153+
cnt.set(c, (cnt.get(c) ||0) +1);
148154
}
149-
for (let i = 0; i < s.length; i++) {
150-
if (cnt[s.charCodeAt(i) -97] === 1) {
155+
for (let i = 0; i < s.length; ++i) {
156+
if (cnt.get(s[i]) === 1) {
151157
return i;
152158
}
153159
}
@@ -163,12 +169,12 @@ function firstUniqChar(s: string): number {
163169
* @return {number}
164170
*/
165171
var firstUniqChar = function (s) {
166-
const cnt = new Array(26).fill(0);
172+
const cnt = new Map();
167173
for (const c of s) {
168-
++cnt[c.charCodeAt() -'a'.charCodeAt()];
174+
cnt.set(c, (cnt.get(c) ||0) +1);
169175
}
170176
for (let i = 0; i < s.length; ++i) {
171-
if (cnt[s[i].charCodeAt() -'a'.charCodeAt()] === 1) {
177+
if (cnt.get(s[i]) === 1) {
172178
return i;
173179
}
174180
}

‎solution/0300-0399/0387.First Unique Character in a String/Solution.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
* @return {number}
44
*/
55
var firstUniqChar = function (s) {
6-
const cnt = new Array(26).fill(0);
6+
const cnt = new Map();
77
for (const c of s) {
8-
++cnt[c.charCodeAt()-'a'.charCodeAt()];
8+
cnt.set(c,(cnt.get(c)||0)+1);
99
}
1010
for (let i = 0; i < s.length; ++i) {
11-
if (cnt[s[i].charCodeAt()-'a'.charCodeAt()] === 1) {
11+
if (cnt.get(s[i]) === 1) {
1212
return i;
1313
}
1414
}

0 commit comments

Comments
(0)

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