Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.0369,0387,0434 #4270

Merged
merged 1 commit into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions solution/0300-0399/0369.Plus One Linked List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)$。

<!-- tabs:start -->

Expand All @@ -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:
Expand Down Expand Up @@ -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;
}
};
```
Expand All @@ -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
Expand All @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
63 changes: 50 additions & 13 deletions solution/0300-0399/0369.Plus One Linked List/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ tags:

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

Expand All @@ -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:
Expand Down Expand Up @@ -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;
}
};
```
Expand All @@ -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
Expand All @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
15 changes: 7 additions & 8 deletions solution/0300-0399/0369.Plus One Linked List/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
4 changes: 1 addition & 3 deletions solution/0300-0399/0369.Plus One Linked List/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion solution/0300-0399/0369.Plus One Linked List/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
27 changes: 27 additions & 0 deletions solution/0300-0399/0369.Plus One Linked List/Solution.ts
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,13 @@ tags:

<!-- solution:start -->

### 方法一:数组或哈希表
### 方法一:计数

我们可以用数组或哈希表 $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$。

<!-- tabs:start -->

Expand Down Expand Up @@ -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<string, number>();
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;
}
}
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ tags:

<!-- solution:start -->

### 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$.

<!-- tabs:start -->

Expand Down Expand Up @@ -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<string, number>();
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;
}
}
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Loading