diff --git a/solution/0300-0399/0369.Plus One Linked List/README.md b/solution/0300-0399/0369.Plus One Linked List/README.md index 95eebe6928d74..078c4033d9a67 100644 --- a/solution/0300-0399/0369.Plus One Linked List/README.md +++ b/solution/0300-0399/0369.Plus One Linked List/README.md @@ -57,13 +57,13 @@ tags: ### 方法一:链表遍历 -我们先设置一个虚拟头节点 `dummy`,初始值为 $0$,指向链表头节点 `head`。 +我们先设置一个虚拟头节点 $\textit{dummy}$,初始时 $\textit{dummy}$ 的值为 $0$,并且 $\textit{dummy}$ 的后继节点为链表 $\textit{head}$。 -然后从链表头节点开始遍历,找出链表最后一个值不等于 $9$ 的节点 `target`,将 `target` 的值加 $1$。接着将 `target` 之后的所有节点值置为 $0$。 +接下来,我们从虚拟头节点开始遍历链表,找到最后一个不为 $9$ 的节点,将其值加 $1$,并将该节点之后的所有节点的值置为 $0$。 -需要注意的是,如果链表中所有节点值都为 $9$,那么遍历结束后,`target` 会指向空节点,这时我们需要将 `dummy` 的值加 $1$,然后返回 `dummy`,否则返回 `dummy` 的下一个节点。 +最后,我们判断虚拟头节点的值是否为 $1$,如果为 $1$,则返回 $\textit{dummy}$,否则返回 $\textit{dummy}$ 的后继节点。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为链表的长度。 +时间复杂度 $O(n)$,其中 $n$ 是链表的长度。空间复杂度 $O(1)$。 @@ -76,7 +76,7 @@ tags: # self.val = val # self.next = next class Solution: - def plusOne(self, head: ListNode) -> ListNode: + def plusOne(self, head: Optional[ListNode]) -> Optional[ListNode]: dummy = ListNode(0, head) target = dummy while head: @@ -143,17 +143,16 @@ public: ListNode* plusOne(ListNode* head) { ListNode* dummy = new ListNode(0, head); ListNode* target = dummy; - while (head) { - if (head->val != 9) target = head; - head = head->next; + for (; head; head = head->next) { + if (head->val != 9) { + target = head; + } } - ++target->val; - target = target->next; - while (target) { + target->val++; + for (target = target->next; target; target = target->next) { target->val = 0; - target = target->next; } - return dummy->val == 1 ? dummy : dummy->next; + return dummy->val ? dummy : dummy->next; } }; ``` @@ -178,10 +177,8 @@ func plusOne(head *ListNode) *ListNode { head = head.Next } target.Val++ - target = target.Next - for target != nil { + for target = target.Next; target != nil; target = target.Next { target.Val = 0 - target = target.Next } if dummy.Val == 1 { return dummy @@ -190,6 +187,38 @@ func plusOne(head *ListNode) *ListNode { } ``` +#### TypeScript + +```ts +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +function plusOne(head: ListNode | null): ListNode | null { + const dummy = new ListNode(0, head); + let target = dummy; + while (head) { + if (head.val !== 9) { + target = head; + } + head = head.next; + } + target.val++; + for (target = target.next; target; target = target.next) { + target.val = 0; + } + return dummy.val ? dummy : dummy.next; +} +``` + diff --git a/solution/0300-0399/0369.Plus One Linked List/README_EN.md b/solution/0300-0399/0369.Plus One Linked List/README_EN.md index e56316d0e35b6..f34920e3a5e8a 100644 --- a/solution/0300-0399/0369.Plus One Linked List/README_EN.md +++ b/solution/0300-0399/0369.Plus One Linked List/README_EN.md @@ -44,7 +44,15 @@ tags: -### Solution 1 +### Solution 1: Linked List Traversal + +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}$. + +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$. + +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}$. + +The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$. @@ -57,7 +65,7 @@ tags: # self.val = val # self.next = next class Solution: - def plusOne(self, head: ListNode) -> ListNode: + def plusOne(self, head: Optional[ListNode]) -> Optional[ListNode]: dummy = ListNode(0, head) target = dummy while head: @@ -124,17 +132,16 @@ public: ListNode* plusOne(ListNode* head) { ListNode* dummy = new ListNode(0, head); ListNode* target = dummy; - while (head) { - if (head->val != 9) target = head; - head = head->next; + for (; head; head = head->next) { + if (head->val != 9) { + target = head; + } } - ++target->val; - target = target->next; - while (target) { + target->val++; + for (target = target->next; target; target = target->next) { target->val = 0; - target = target->next; } - return dummy->val == 1 ? dummy : dummy->next; + return dummy->val ? dummy : dummy->next; } }; ``` @@ -159,10 +166,8 @@ func plusOne(head *ListNode) *ListNode { head = head.Next } target.Val++ - target = target.Next - for target != nil { + for target = target.Next; target != nil; target = target.Next { target.Val = 0 - target = target.Next } if dummy.Val == 1 { return dummy @@ -171,6 +176,38 @@ func plusOne(head *ListNode) *ListNode { } ``` +#### TypeScript + +```ts +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +function plusOne(head: ListNode | null): ListNode | null { + const dummy = new ListNode(0, head); + let target = dummy; + while (head) { + if (head.val !== 9) { + target = head; + } + head = head.next; + } + target.val++; + for (target = target.next; target; target = target.next) { + target.val = 0; + } + return dummy.val ? dummy : dummy.next; +} +``` + diff --git a/solution/0300-0399/0369.Plus One Linked List/Solution.cpp b/solution/0300-0399/0369.Plus One Linked List/Solution.cpp index d608df47aa497..4039ca6ce9ba2 100644 --- a/solution/0300-0399/0369.Plus One Linked List/Solution.cpp +++ b/solution/0300-0399/0369.Plus One Linked List/Solution.cpp @@ -13,16 +13,15 @@ class Solution { ListNode* plusOne(ListNode* head) { ListNode* dummy = new ListNode(0, head); ListNode* target = dummy; - while (head) { - if (head->val != 9) target = head; - head = head->next; + for (; head; head = head->next) { + if (head->val != 9) { + target = head; + } } - ++target->val; - target = target->next; - while (target) { + target->val++; + for (target = target->next; target; target = target->next) { target->val = 0; - target = target->next; } - return dummy->val == 1 ? dummy : dummy->next; + return dummy->val ? dummy : dummy->next; } }; \ No newline at end of file diff --git a/solution/0300-0399/0369.Plus One Linked List/Solution.go b/solution/0300-0399/0369.Plus One Linked List/Solution.go index 2cc181b636ae9..e391dafc75f0b 100644 --- a/solution/0300-0399/0369.Plus One Linked List/Solution.go +++ b/solution/0300-0399/0369.Plus One Linked List/Solution.go @@ -15,10 +15,8 @@ func plusOne(head *ListNode) *ListNode { head = head.Next } target.Val++ - target = target.Next - for target != nil { + for target = target.Next; target != nil; target = target.Next { target.Val = 0 - target = target.Next } if dummy.Val == 1 { return dummy diff --git a/solution/0300-0399/0369.Plus One Linked List/Solution.py b/solution/0300-0399/0369.Plus One Linked List/Solution.py index 39f094896af21..ee84db74fc160 100644 --- a/solution/0300-0399/0369.Plus One Linked List/Solution.py +++ b/solution/0300-0399/0369.Plus One Linked List/Solution.py @@ -4,7 +4,7 @@ # self.val = val # self.next = next class Solution: - def plusOne(self, head: ListNode) -> ListNode: + def plusOne(self, head: Optional[ListNode]) -> Optional[ListNode]: dummy = ListNode(0, head) target = dummy while head: diff --git a/solution/0300-0399/0369.Plus One Linked List/Solution.ts b/solution/0300-0399/0369.Plus One Linked List/Solution.ts new file mode 100644 index 0000000000000..81a3c64edb53c --- /dev/null +++ b/solution/0300-0399/0369.Plus One Linked List/Solution.ts @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +function plusOne(head: ListNode | null): ListNode | null { + const dummy = new ListNode(0, head); + let target = dummy; + while (head) { + if (head.val !== 9) { + target = head; + } + head = head.next; + } + target.val++; + for (target = target.next; target; target = target.next) { + target.val = 0; + } + return dummy.val ? dummy : dummy.next; +} diff --git a/solution/0300-0399/0387.First Unique Character in a String/README.md b/solution/0300-0399/0387.First Unique Character in a String/README.md index eb945630e5bfe..146df01e483fd 100644 --- a/solution/0300-0399/0387.First Unique Character in a String/README.md +++ b/solution/0300-0399/0387.First Unique Character in a String/README.md @@ -59,15 +59,13 @@ tags: -### 方法一:数组或哈希表 +### 方法一:计数 -我们可以用数组或哈希表 $cnt$ 记录字符串 $s$ 中每个字符出现的次数。 +我们用一个哈希表或者一个长度为 $26$ 的数组 $\text{cnt}$ 来存储每个字符出现的次数,然后从头开始遍历每个字符 $\text{s[i]}$,如果 $\text{cnt[s[i]]}$ 为 $1$,则返回 $i$。 -然后我们再遍历字符串 $s$,当遍历到某个字符 $c$ 时,如果 $cnt[c]=1$,则说明 $c$ 是第一个不重复的字符,返回它的索引即可。 +遍历结束后,如果没有找到符合条件的字符,返回 $-1$。 -如果遍历完字符串 $s$ 仍然没有找到不重复的字符,返回 $-1$。 - -时间复杂度 $O(n)$,空间复杂度 $O(\Sigma)$,其中 $\Sigma$ 是字符集的大小。 +时间复杂度 $O(n)$,其中 $n$ 是字符串的长度。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 是字符集,本题中字符集为小写字母,所以 $|\Sigma|=26$。 @@ -145,12 +143,12 @@ func firstUniqChar(s string) int { ```ts function firstUniqChar(s: string): number { - const cnt = new Array(26).fill(0); + const cnt = new Map(); for (const c of s) { - cnt[c.charCodeAt(0) - 97]++; + cnt.set(c, (cnt.get(c) || 0) + 1); } - for (let i = 0; i < s.length; i++) { - if (cnt[s.charCodeAt(i) - 97] === 1) { + for (let i = 0; i < s.length; ++i) { + if (cnt.get(s[i]) === 1) { return i; } } @@ -166,12 +164,12 @@ function firstUniqChar(s: string): number { * @return {number} */ var firstUniqChar = function (s) { - const cnt = new Array(26).fill(0); + const cnt = new Map(); for (const c of s) { - ++cnt[c.charCodeAt() - 'a'.charCodeAt()]; + cnt.set(c, (cnt.get(c) || 0) + 1); } for (let i = 0; i < s.length; ++i) { - if (cnt[s[i].charCodeAt() - 'a'.charCodeAt()] === 1) { + if (cnt.get(s[i]) === 1) { return i; } } diff --git a/solution/0300-0399/0387.First Unique Character in a String/README_EN.md b/solution/0300-0399/0387.First Unique Character in a String/README_EN.md index a86551f835c7c..a063045a0d345 100644 --- a/solution/0300-0399/0387.First Unique Character in a String/README_EN.md +++ b/solution/0300-0399/0387.First Unique Character in a String/README_EN.md @@ -64,7 +64,13 @@ tags: -### Solution 1 +### Solution 1: Counting + +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$. + +If no such character is found after the traversal, we return $-1$. + +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$. @@ -142,12 +148,12 @@ func firstUniqChar(s string) int { ```ts function firstUniqChar(s: string): number { - const cnt = new Array(26).fill(0); + const cnt = new Map(); for (const c of s) { - cnt[c.charCodeAt(0) - 97]++; + cnt.set(c, (cnt.get(c) || 0) + 1); } - for (let i = 0; i < s.length; i++) { - if (cnt[s.charCodeAt(i) - 97] === 1) { + for (let i = 0; i < s.length; ++i) { + if (cnt.get(s[i]) === 1) { return i; } } @@ -163,12 +169,12 @@ function firstUniqChar(s: string): number { * @return {number} */ var firstUniqChar = function (s) { - const cnt = new Array(26).fill(0); + const cnt = new Map(); for (const c of s) { - ++cnt[c.charCodeAt() - 'a'.charCodeAt()]; + cnt.set(c, (cnt.get(c) || 0) + 1); } for (let i = 0; i < s.length; ++i) { - if (cnt[s[i].charCodeAt() - 'a'.charCodeAt()] === 1) { + if (cnt.get(s[i]) === 1) { return i; } } diff --git a/solution/0300-0399/0387.First Unique Character in a String/Solution.js b/solution/0300-0399/0387.First Unique Character in a String/Solution.js index 9e12bbbdd828e..5ffe59f022aa8 100644 --- a/solution/0300-0399/0387.First Unique Character in a String/Solution.js +++ b/solution/0300-0399/0387.First Unique Character in a String/Solution.js @@ -3,12 +3,12 @@ * @return {number} */ var firstUniqChar = function (s) { - const cnt = new Array(26).fill(0); + const cnt = new Map(); for (const c of s) { - ++cnt[c.charCodeAt() - 'a'.charCodeAt()]; + cnt.set(c, (cnt.get(c) || 0) + 1); } for (let i = 0; i < s.length; ++i) { - if (cnt[s[i].charCodeAt() - 'a'.charCodeAt()] === 1) { + if (cnt.get(s[i]) === 1) { return i; } } diff --git a/solution/0300-0399/0387.First Unique Character in a String/Solution.ts b/solution/0300-0399/0387.First Unique Character in a String/Solution.ts index 78be86adcfab1..8fb60cff78022 100644 --- a/solution/0300-0399/0387.First Unique Character in a String/Solution.ts +++ b/solution/0300-0399/0387.First Unique Character in a String/Solution.ts @@ -1,10 +1,10 @@ function firstUniqChar(s: string): number { - const cnt = new Array(26).fill(0); + const cnt = new Map(); for (const c of s) { - cnt[c.charCodeAt(0) - 97]++; + cnt.set(c, (cnt.get(c) || 0) + 1); } - for (let i = 0; i < s.length; i++) { - if (cnt[s.charCodeAt(i) - 97] === 1) { + for (let i = 0; i < s.length; ++i) { + if (cnt.get(s[i]) === 1) { return i; } } diff --git a/solution/0400-0499/0434.Number of Segments in a String/README.md b/solution/0400-0499/0434.Number of Segments in a String/README.md index bb68680de4334..97b8cf15f9b26 100644 --- a/solution/0400-0499/0434.Number of Segments in a String/README.md +++ b/solution/0400-0499/0434.Number of Segments in a String/README.md @@ -35,9 +35,9 @@ tags: ### 方法一:字符串分割 -将字符串 `s` 按照空格进行分割,然后统计不为空的单词个数。 +我们将字符串 $\textit{s}$ 按照空格进行分割,然后统计不为空的单词个数。 -时间复杂度 $O(n)$,空间复杂度 $O(n)$。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $\textit{s}$ 的长度。 @@ -93,6 +93,14 @@ func countSegments(s string) int { } ``` +#### TypeScript + +```ts +function countSegments(s: string): number { + return s.split(/\s+/).filter(Boolean).length; +} +``` + #### PHP ```php @@ -122,9 +130,11 @@ class Solution { ### 方法二:模拟 -直接模拟,遍历字符串,检测每个字符,统计个数。 +我们也可以直接遍历字符串的每个字符 $\text{s[i]}$,如果 $\text{s[i]}$ 不是空格且 $\text{s[i-1]}$ 是空格或者 $i = 0$,那么就说明 $\text{s[i]}$ 是一个新的单词的开始,我们就将答案加一。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。 +遍历结束后,返回答案即可。 + +时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{s}$ 的长度。空间复杂度 $O(1)$。 @@ -187,6 +197,43 @@ func countSegments(s string) int { } ``` +#### TypeScript + +```ts +function countSegments(s: string): number { + let ans = 0; + for (let i = 0; i < s.length; i++) { + let c = s[i]; + if (c !== ' ' && (i === 0 || s[i - 1] === ' ')) { + ans++; + } + } + return ans; +} +``` + +#### PHP + +```php +class Solution { + /** + * @param String $s + * @return Integer + */ + function countSegments($s) { + $ans = 0; + $n = strlen($s); + for ($i = 0; $i < $n; $i++) { + $c = $s[$i]; + if ($c !== ' ' && ($i === 0 || $s[$i - 1] === ' ')) { + $ans++; + } + } + return $ans; + } +} +``` + diff --git a/solution/0400-0499/0434.Number of Segments in a String/README_EN.md b/solution/0400-0499/0434.Number of Segments in a String/README_EN.md index becc602f25288..de310aced515b 100644 --- a/solution/0400-0499/0434.Number of Segments in a String/README_EN.md +++ b/solution/0400-0499/0434.Number of Segments in a String/README_EN.md @@ -51,7 +51,11 @@ tags: -### Solution 1 +### Solution 1: String Splitting + +We split the string $\textit{s}$ by spaces and then count the number of non-empty words. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the length of the string $\textit{s}$. @@ -107,6 +111,14 @@ func countSegments(s string) int { } ``` +#### TypeScript + +```ts +function countSegments(s: string): number { + return s.split(/\s+/).filter(Boolean).length; +} +``` + #### PHP ```php @@ -134,7 +146,13 @@ class Solution { -### Solution 2 +### Solution 2: Simulation + +We can also directly traverse each character $\text{s[i]}$ in the string. If $\text{s[i]}$ is not a space and $\text{s[i-1]}$ is a space or $i = 0$, then $\text{s[i]}$ marks the beginning of a new word, and we increment the answer by one. + +After the traversal, we return the answer. + +The time complexity is $O(n)$, where $n$ is the length of the string $\textit{s}$. The space complexity is $O(1)$. @@ -197,6 +215,43 @@ func countSegments(s string) int { } ``` +#### TypeScript + +```ts +function countSegments(s: string): number { + let ans = 0; + for (let i = 0; i < s.length; i++) { + let c = s[i]; + if (c !== ' ' && (i === 0 || s[i - 1] === ' ')) { + ans++; + } + } + return ans; +} +``` + +#### PHP + +```php +class Solution { + /** + * @param String $s + * @return Integer + */ + function countSegments($s) { + $ans = 0; + $n = strlen($s); + for ($i = 0; $i < $n; $i++) { + $c = $s[$i]; + if ($c !== ' ' && ($i === 0 || $s[$i - 1] === ' ')) { + $ans++; + } + } + return $ans; + } +} +``` + diff --git a/solution/0400-0499/0434.Number of Segments in a String/Solution.ts b/solution/0400-0499/0434.Number of Segments in a String/Solution.ts new file mode 100644 index 0000000000000..e31e039bb1416 --- /dev/null +++ b/solution/0400-0499/0434.Number of Segments in a String/Solution.ts @@ -0,0 +1,3 @@ +function countSegments(s: string): number { + return s.split(/\s+/).filter(Boolean).length; +} diff --git a/solution/0400-0499/0434.Number of Segments in a String/Solution2.php b/solution/0400-0499/0434.Number of Segments in a String/Solution2.php new file mode 100644 index 0000000000000..70de51e4e781f --- /dev/null +++ b/solution/0400-0499/0434.Number of Segments in a String/Solution2.php @@ -0,0 +1,17 @@ +class Solution { + /** + * @param String $s + * @return Integer + */ + function countSegments($s) { + $ans = 0; + $n = strlen($s); + for ($i = 0; $i < $n; $i++) { + $c = $s[$i]; + if ($c !== ' ' && ($i === 0 || $s[$i - 1] === ' ')) { + $ans++; + } + } + return $ans; + } +} \ No newline at end of file diff --git a/solution/0400-0499/0434.Number of Segments in a String/Solution2.ts b/solution/0400-0499/0434.Number of Segments in a String/Solution2.ts new file mode 100644 index 0000000000000..27613c80065e9 --- /dev/null +++ b/solution/0400-0499/0434.Number of Segments in a String/Solution2.ts @@ -0,0 +1,10 @@ +function countSegments(s: string): number { + let ans = 0; + for (let i = 0; i < s.length; i++) { + let c = s[i]; + if (c !== ' ' && (i === 0 || s[i - 1] === ' ')) { + ans++; + } + } + return ans; +}