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 f31828a

Browse files
feat: add solutions to lc problems: No.2485~2487 (doocs#2175)
1 parent c4f60e0 commit f31828a

File tree

7 files changed

+402
-22
lines changed

7 files changed

+402
-22
lines changed

‎solution/2400-2499/2485.Find the Pivot Integer/README_EN.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,34 @@
4646

4747
## Solutions
4848

49+
**Solution 1: Enumeration**
50+
51+
We can directly enumerate $x$ in the range of $[1,..n],ドル and check whether the following equation holds. If it holds, then $x$ is the pivot integer, and we can directly return $x$.
52+
53+
$$
54+
(1 + x) \times x = (x + n) \times (n - x + 1)
55+
$$
56+
57+
The time complexity is $O(n),ドル where $n$ is the given positive integer $n$. The space complexity is $O(1)$.
58+
59+
**Solution 2: Mathematics**
60+
61+
We can transform the above equation to get:
62+
63+
$$
64+
n \times (n + 1) = 2 \times x^2
65+
$$
66+
67+
That is:
68+
69+
$$
70+
x = \sqrt{\frac{n \times (n + 1)}{2}}
71+
$$
72+
73+
If $x$ is an integer, then $x$ is the pivot integer, otherwise there is no pivot integer.
74+
75+
The time complexity is $O(1),ドル and the space complexity is $O(1)$.
76+
4977
<!-- tabs:start -->
5078

5179
### **Python3**

‎solution/2400-2499/2486.Append Characters to String to Make Subsequence/README.md‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757

5858
**方法一:双指针**
5959

60-
定义两个指针 $i$ 和 $j,ドル分别指向字符串 $s$ 和 $t$ 的首字符。遍历字符串 $t,ドル当 $s[i] \neq t[j]$ 时,指针 $i$ 后移,直到 $s[i] = t[j]$ 或者 $i$ 到达字符串 $s$ 的末尾。如果 $i$ 到达字符串 $s$ 的末尾,说明 $t$ 中的字符 $t[j]$ 无法在 $s$ 中找到对应的字符,返回 $t$ 中剩余的字符数。否则,将指针 $i$ 和 $j$ 同时后移,继续遍历字符串 $t$。
60+
我们定义两个指针 $i$ 和 $j,ドル分别指向字符串 $s$ 和 $t$ 的首字符。遍历字符串 $t,ドル当 $s[i] \neq t[j]$ 时,指针 $i$ 后移,直到 $s[i] = t[j]$ 或者 $i$ 到达字符串 $s$ 的末尾。如果 $i$ 到达字符串 $s$ 的末尾,说明 $t$ 中的字符 $t[j]$ 无法在 $s$ 中找到对应的字符,返回 $t$ 中剩余的字符数。否则,将指针 $i$ 和 $j$ 同时后移,继续遍历字符串 $t$。
6161

6262
时间复杂度 $(m + n),ドル空间复杂度 $O(1)$。其中 $m$ 和 $n$ 分别是字符串 $s$ 和 $t$ 的长度。
6363

@@ -70,13 +70,12 @@
7070
```python
7171
class Solution:
7272
def appendCharacters(self, s: str, t: str) -> int:
73-
m, n = len(s), len(t)
74-
i = 0
75-
for j in range(n):
76-
while i < m and s[i] != t[j]:
73+
i, m = 0, len(s)
74+
for j, c in enumerate(t):
75+
while i < m and s[i] != c:
7776
i += 1
7877
if i == m:
79-
return n - j
78+
return len(t) - j
8079
i += 1
8180
return 0
8281
```
@@ -139,6 +138,24 @@ func appendCharacters(s string, t string) int {
139138
}
140139
```
141140

141+
### **TypeScript**
142+
143+
```ts
144+
function appendCharacters(s: string, t: string): number {
145+
const [m, n] = [s.length, t.length];
146+
for (let i = 0, j = 0; j < n; ++j) {
147+
while (i < m && s[i] !== t[j]) {
148+
++i;
149+
}
150+
if (i === m) {
151+
return n - j;
152+
}
153+
++i;
154+
}
155+
return 0;
156+
}
157+
```
158+
142159
### **...**
143160

144161
```

‎solution/2400-2499/2486.Append Characters to String to Make Subsequence/README_EN.md‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,25 @@ It can be shown that appending any 4 characters to the end of s will never make
4949

5050
## Solutions
5151

52+
**Solution 1: Two Pointers**
53+
54+
We define two pointers $i$ and $j,ドル pointing to the first characters of strings $s$ and $t$ respectively. We traverse string $t,ドル when $s[i] \neq t[j],ドル we move pointer $i$ forward until $s[i] = t[j]$ or $i$ reaches the end of string $s$. If $i$ reaches the end of string $s,ドル it means that the character $t[j]$ in $t$ cannot find the corresponding character in $s,ドル so we return the remaining number of characters in $t$. Otherwise, we move both pointers $i$ and $j$ forward and continue to traverse string $t$.
55+
56+
The time complexity is $O(m + n),ドル and the space complexity is $O(1)$. Where $m$ and $n$ are the lengths of strings $s$ and $t$ respectively.
57+
5258
<!-- tabs:start -->
5359

5460
### **Python3**
5561

5662
```python
5763
class Solution:
5864
def appendCharacters(self, s: str, t: str) -> int:
59-
m, n = len(s), len(t)
60-
i = 0
61-
for j in range(n):
62-
while i < m and s[i] != t[j]:
65+
i, m = 0, len(s)
66+
for j, c in enumerate(t):
67+
while i < m and s[i] != c:
6368
i += 1
6469
if i == m:
65-
return n - j
70+
return len(t) - j
6671
i += 1
6772
return 0
6873
```
@@ -123,6 +128,24 @@ func appendCharacters(s string, t string) int {
123128
}
124129
```
125130

131+
### **TypeScript**
132+
133+
```ts
134+
function appendCharacters(s: string, t: string): number {
135+
const [m, n] = [s.length, t.length];
136+
for (let i = 0, j = 0; j < n; ++j) {
137+
while (i < m && s[i] !== t[j]) {
138+
++i;
139+
}
140+
if (i === m) {
141+
return n - j;
142+
}
143+
++i;
144+
}
145+
return 0;
146+
}
147+
```
148+
126149
### **...**
127150

128151
```
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
class Solution:
2-
def appendCharacters(self, s: str, t: str) -> int:
3-
m, n = len(s), len(t)
4-
i = 0
5-
for j in range(n):
6-
while i < m and s[i] != t[j]:
7-
i += 1
8-
if i == m:
9-
return n - j
10-
i += 1
11-
return 0
1+
class Solution:
2+
def appendCharacters(self, s: str, t: str) -> int:
3+
i, m = 0, len(s)
4+
for j, c in enumerate(t):
5+
while i < m and s[i] != c:
6+
i += 1
7+
if i == m:
8+
return len(t) - j
9+
i += 1
10+
return 0
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function appendCharacters(s: string, t: string): number {
2+
const [m, n] = [s.length, t.length];
3+
for (let i = 0, j = 0; j < n; ++j) {
4+
while (i < m && s[i] !== t[j]) {
5+
++i;
6+
}
7+
if (i === m) {
8+
return n - j;
9+
}
10+
++i;
11+
}
12+
return 0;
13+
}

‎solution/2400-2499/2487.Remove Nodes From Linked List/README.md‎

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@
5656

5757
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是链表的长度。
5858

59+
我们也可以不使用数组 $nums,ドル直接遍历链表,维护一个从栈底到栈顶单调递减的栈 $stk,ドル如果当前元素比栈顶元素大,则将栈顶元素出栈,直到当前元素小于等于栈顶元素。然后,如果栈不为空,则将栈顶元素的 $next$ 指针指向当前元素,否则将答案链表的虚拟头节点的 $next$ 指针指向当前元素。最后,将当前元素入栈,继续遍历链表。
60+
61+
遍历结束后,将虚拟头节点的 $next$ 指针作为答案返回。
62+
63+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是链表的长度。
64+
5965
<!-- tabs:start -->
6066

6167
### **Python3**
@@ -87,6 +93,29 @@ class Solution:
8793
return dummy.next
8894
```
8995

96+
```python
97+
# Definition for singly-linked list.
98+
# class ListNode:
99+
# def __init__(self, val=0, next=None):
100+
# self.val = val
101+
# self.next = next
102+
class Solution:
103+
def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
104+
dummy = ListNode(next=head)
105+
cur = head
106+
stk = []
107+
while cur:
108+
while stk and stk[-1].val < cur.val:
109+
stk.pop()
110+
if stk:
111+
stk[-1].next = cur
112+
else:
113+
dummy.next = cur
114+
stk.append(cur)
115+
cur = cur.next
116+
return dummy.next
117+
```
118+
90119
### **Java**
91120

92121
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -127,6 +156,37 @@ class Solution {
127156
}
128157
```
129158

159+
```java
160+
/**
161+
* Definition for singly-linked list.
162+
* public class ListNode {
163+
* int val;
164+
* ListNode next;
165+
* ListNode() {}
166+
* ListNode(int val) { this.val = val; }
167+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
168+
* }
169+
*/
170+
class Solution {
171+
public ListNode removeNodes(ListNode head) {
172+
ListNode dummy = new ListNode(0, head);
173+
Deque<ListNode> stk = new ArrayDeque<>();
174+
for (ListNode cur = head; cur != null; cur = cur.next) {
175+
while (!stk.isEmpty() && stk.peekLast().val < cur.val) {
176+
stk.pollLast();
177+
}
178+
if (!stk.isEmpty()) {
179+
stk.peekLast().next = cur;
180+
} else {
181+
dummy.next = cur;
182+
}
183+
stk.offerLast(cur);
184+
}
185+
return dummy.next;
186+
}
187+
}
188+
```
189+
130190
### **C++**
131191

132192
```cpp
@@ -166,6 +226,39 @@ public:
166226
};
167227
```
168228
229+
```cpp
230+
/**
231+
* Definition for singly-linked list.
232+
* struct ListNode {
233+
* int val;
234+
* ListNode *next;
235+
* ListNode() : val(0), next(nullptr) {}
236+
* ListNode(int x) : val(x), next(nullptr) {}
237+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
238+
* };
239+
*/
240+
class Solution {
241+
public:
242+
ListNode* removeNodes(ListNode* head) {
243+
ListNode* dummy = new ListNode(0, head);
244+
ListNode* cur = head;
245+
vector<ListNode*> stk;
246+
for (ListNode* cur = head; cur; cur = cur->next) {
247+
while (stk.size() && stk.back()->val < cur->val) {
248+
stk.pop_back();
249+
}
250+
if (stk.size()) {
251+
stk.back()->next = cur;
252+
} else {
253+
dummy->next = cur;
254+
}
255+
stk.push_back(cur);
256+
}
257+
return dummy->next;
258+
}
259+
};
260+
```
261+
169262
### **Go**
170263

171264
```go
@@ -199,6 +292,32 @@ func removeNodes(head *ListNode) *ListNode {
199292
}
200293
```
201294

295+
```go
296+
/**
297+
* Definition for singly-linked list.
298+
* type ListNode struct {
299+
* Val int
300+
* Next *ListNode
301+
* }
302+
*/
303+
func removeNodes(head *ListNode) *ListNode {
304+
dummy := &ListNode{Next: head}
305+
stk := []*ListNode{}
306+
for cur := head; cur != nil; cur = cur.Next {
307+
for len(stk) > 0 && stk[len(stk)-1].Val < cur.Val {
308+
stk = stk[:len(stk)-1]
309+
}
310+
if len(stk) > 0 {
311+
stk[len(stk)-1].Next = cur
312+
} else {
313+
dummy.Next = cur
314+
}
315+
stk = append(stk, cur)
316+
}
317+
return dummy.Next
318+
}
319+
```
320+
202321
### **TypeScript**
203322

204323
```ts
@@ -236,6 +355,37 @@ function removeNodes(head: ListNode | null): ListNode | null {
236355
}
237356
```
238357

358+
```ts
359+
/**
360+
* Definition for singly-linked list.
361+
* class ListNode {
362+
* val: number
363+
* next: ListNode | null
364+
* constructor(val?: number, next?: ListNode | null) {
365+
* this.val = (val===undefined ? 0 : val)
366+
* this.next = (next===undefined ? null : next)
367+
* }
368+
* }
369+
*/
370+
371+
function removeNodes(head: ListNode | null): ListNode | null {
372+
const dummy = new ListNode(0, head);
373+
const stk: ListNode[] = [];
374+
for (let cur = head; cur; cur = cur.next) {
375+
while (stk.length && stk.at(-1)!.val < cur.val) {
376+
stk.pop();
377+
}
378+
if (stk.length) {
379+
stk.at(-1)!.next = cur;
380+
} else {
381+
dummy.next = cur;
382+
}
383+
stk.push(cur);
384+
}
385+
return dummy.next;
386+
}
387+
```
388+
239389
### **...**
240390

241391
```

0 commit comments

Comments
(0)

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