Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: add solutions to lc problems: No.2485~2487 #2175

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

Merged
merged 1 commit into from
Jan 3, 2024
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
28 changes: 28 additions & 0 deletions solution/2400-2499/2485.Find the Pivot Integer/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@

## Solutions

**Solution 1: Enumeration**

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

$$
(1 + x) \times x = (x + n) \times (n - x + 1)
$$

The time complexity is $O(n)$, where $n$ is the given positive integer $n$. The space complexity is $O(1)$.

**Solution 2: Mathematics**

We can transform the above equation to get:

$$
n \times (n + 1) = 2 \times x^2
$$

That is:

$$
x = \sqrt{\frac{n \times (n + 1)}{2}}
$$

If $x$ is an integer, then $x$ is the pivot integer, otherwise there is no pivot integer.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@

**方法一:双指针**

定义两个指针 $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$。
我们定义两个指针 $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$。

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

Expand All @@ -70,13 +70,12 @@
```python
class Solution:
def appendCharacters(self, s: str, t: str) -> int:
m, n = len(s), len(t)
i = 0
for j in range(n):
while i < m and s[i] != t[j]:
i, m = 0, len(s)
for j, c in enumerate(t):
while i < m and s[i] != c:
i += 1
if i == m:
return n - j
return len(t) - j
i += 1
return 0
```
Expand Down Expand Up @@ -139,6 +138,24 @@ func appendCharacters(s string, t string) int {
}
```

### **TypeScript**

```ts
function appendCharacters(s: string, t: string): number {
const [m, n] = [s.length, t.length];
for (let i = 0, j = 0; j < n; ++j) {
while (i < m && s[i] !== t[j]) {
++i;
}
if (i === m) {
return n - j;
}
++i;
}
return 0;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,25 @@ It can be shown that appending any 4 characters to the end of s will never make

## Solutions

**Solution 1: Two Pointers**

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

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.

<!-- tabs:start -->

### **Python3**

```python
class Solution:
def appendCharacters(self, s: str, t: str) -> int:
m, n = len(s), len(t)
i = 0
for j in range(n):
while i < m and s[i] != t[j]:
i, m = 0, len(s)
for j, c in enumerate(t):
while i < m and s[i] != c:
i += 1
if i == m:
return n - j
return len(t) - j
i += 1
return 0
```
Expand Down Expand Up @@ -123,6 +128,24 @@ func appendCharacters(s string, t string) int {
}
```

### **TypeScript**

```ts
function appendCharacters(s: string, t: string): number {
const [m, n] = [s.length, t.length];
for (let i = 0, j = 0; j < n; ++j) {
while (i < m && s[i] !== t[j]) {
++i;
}
if (i === m) {
return n - j;
}
++i;
}
return 0;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
class Solution:
def appendCharacters(self, s: str, t: str) -> int:
m, n = len(s), len(t)
i = 0
for j in range(n):
while i < m and s[i] != t[j]:
i += 1
if i == m:
return n - j
i += 1
return 0
class Solution:
def appendCharacters(self, s: str, t: str) -> int:
i, m = 0, len(s)
for j, c in enumerate(t):
while i < m and s[i] != c:
i += 1
if i == m:
return len(t) - j
i += 1
return 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function appendCharacters(s: string, t: string): number {
const [m, n] = [s.length, t.length];
for (let i = 0, j = 0; j < n; ++j) {
while (i < m && s[i] !== t[j]) {
++i;
}
if (i === m) {
return n - j;
}
++i;
}
return 0;
}
150 changes: 150 additions & 0 deletions solution/2400-2499/2487.Remove Nodes From Linked List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@

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

我们也可以不使用数组 $nums$,直接遍历链表,维护一个从栈底到栈顶单调递减的栈 $stk$,如果当前元素比栈顶元素大,则将栈顶元素出栈,直到当前元素小于等于栈顶元素。然后,如果栈不为空,则将栈顶元素的 $next$ 指针指向当前元素,否则将答案链表的虚拟头节点的 $next$ 指针指向当前元素。最后,将当前元素入栈,继续遍历链表。

遍历结束后,将虚拟头节点的 $next$ 指针作为答案返回。

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

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -87,6 +93,29 @@ class Solution:
return dummy.next
```

```python
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNodes(self, head: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode(next=head)
cur = head
stk = []
while cur:
while stk and stk[-1].val < cur.val:
stk.pop()
if stk:
stk[-1].next = cur
else:
dummy.next = cur
stk.append(cur)
cur = cur.next
return dummy.next
```

### **Java**

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

```java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNodes(ListNode head) {
ListNode dummy = new ListNode(0, head);
Deque<ListNode> stk = new ArrayDeque<>();
for (ListNode cur = head; cur != null; cur = cur.next) {
while (!stk.isEmpty() && stk.peekLast().val < cur.val) {
stk.pollLast();
}
if (!stk.isEmpty()) {
stk.peekLast().next = cur;
} else {
dummy.next = cur;
}
stk.offerLast(cur);
}
return dummy.next;
}
}
```

### **C++**

```cpp
Expand Down Expand Up @@ -166,6 +226,39 @@ public:
};
```

```cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNodes(ListNode* head) {
ListNode* dummy = new ListNode(0, head);
ListNode* cur = head;
vector<ListNode*> stk;
for (ListNode* cur = head; cur; cur = cur->next) {
while (stk.size() && stk.back()->val < cur->val) {
stk.pop_back();
}
if (stk.size()) {
stk.back()->next = cur;
} else {
dummy->next = cur;
}
stk.push_back(cur);
}
return dummy->next;
}
};
```

### **Go**

```go
Expand Down Expand Up @@ -199,6 +292,32 @@ func removeNodes(head *ListNode) *ListNode {
}
```

```go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func removeNodes(head *ListNode) *ListNode {
dummy := &ListNode{Next: head}
stk := []*ListNode{}
for cur := head; cur != nil; cur = cur.Next {
for len(stk) > 0 && stk[len(stk)-1].Val < cur.Val {
stk = stk[:len(stk)-1]
}
if len(stk) > 0 {
stk[len(stk)-1].Next = cur
} else {
dummy.Next = cur
}
stk = append(stk, cur)
}
return dummy.Next
}
```

### **TypeScript**

```ts
Expand Down Expand Up @@ -236,6 +355,37 @@ function removeNodes(head: ListNode | null): ListNode | null {
}
```

```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 removeNodes(head: ListNode | null): ListNode | null {
const dummy = new ListNode(0, head);
const stk: ListNode[] = [];
for (let cur = head; cur; cur = cur.next) {
while (stk.length && stk.at(-1)!.val < cur.val) {
stk.pop();
}
if (stk.length) {
stk.at(-1)!.next = cur;
} else {
dummy.next = cur;
}
stk.push(cur);
}
return dummy.next;
}
```

### **...**

```
Expand Down
Loading