You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0300-0399/0369.Plus One Linked List/README_EN.md
+50-13
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,15 @@ tags:
44
44
45
45
<!-- solution:start -->
46
46
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)$.
Copy file name to clipboardexpand all lines: solution/0300-0399/0387.First Unique Character in a String/README_EN.md
+14-8
Original file line number
Diff line number
Diff line change
@@ -64,7 +64,13 @@ tags:
64
64
65
65
<!-- solution:start -->
66
66
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$.
68
74
69
75
<!-- tabs:start -->
70
76
@@ -142,12 +148,12 @@ func firstUniqChar(s string) int {
142
148
143
149
```ts
144
150
function firstUniqChar(s:string):number {
145
-
const cnt =newArray(26).fill(0);
151
+
const cnt =newMap<string, number>();
146
152
for (const c ofs) {
147
-
cnt[c.charCodeAt(0) -97]++;
153
+
cnt.set(c, (cnt.get(c) ||0) +1);
148
154
}
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) {
151
157
returni;
152
158
}
153
159
}
@@ -163,12 +169,12 @@ function firstUniqChar(s: string): number {
163
169
* @return{number}
164
170
*/
165
171
varfirstUniqChar=function (s) {
166
-
constcnt=newArray(26).fill(0);
172
+
constcnt=newMap();
167
173
for (constcof s) {
168
-
++cnt[c.charCodeAt() -'a'.charCodeAt()];
174
+
cnt.set(c, (cnt.get(c) ||0) +1);
169
175
}
170
176
for (let i =0; i <s.length; ++i) {
171
-
if (cnt[s[i].charCodeAt() -'a'.charCodeAt()]===1) {
0 commit comments