Skip to content

Commit adde345

Browse files
committed
feat: solve No.237,881,2000,2441,2487
1 parent 5680e2c commit adde345

5 files changed

+441
-0
lines changed
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 2000. Reverse Prefix of Word
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Two Pointers, String.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Given a **0-indexed** string `word` and a character `ch`, **reverse** the segment of `word` that starts at index `0` and ends at the index of the **first occurrence** of `ch` (**inclusive**). If the character `ch` does not exist in `word`, do nothing.
10+
11+
12+
13+
- For example, if `word = "abcdefd"` and `ch = "d"`, then you should **reverse** the segment that starts at `0` and ends at `3` (**inclusive**). The resulting string will be `"dcbaefd"`.
14+
15+
16+
Return **the resulting string**.
17+
18+
 
19+
Example 1:
20+
21+
```
22+
Input: word = "abcdefd", ch = "d"
23+
Output: "dcbaefd"
24+
Explanation: The first occurrence of "d" is at index 3.
25+
Reverse the part of word from 0 to 3 (inclusive), the resulting string is "dcbaefd".
26+
```
27+
28+
Example 2:
29+
30+
```
31+
Input: word = "xyxzxe", ch = "z"
32+
Output: "zxyxxe"
33+
Explanation: The first and only occurrence of "z" is at index 3.
34+
Reverse the part of word from 0 to 3 (inclusive), the resulting string is "zxyxxe".
35+
```
36+
37+
Example 3:
38+
39+
```
40+
Input: word = "abcd", ch = "z"
41+
Output: "abcd"
42+
Explanation: "z" does not exist in word.
43+
You should not do any reverse operation, the resulting string is "abcd".
44+
```
45+
46+
 
47+
**Constraints:**
48+
49+
50+
51+
- `1 <= word.length <= 250`
52+
53+
- `word` consists of lowercase English letters.
54+
55+
- `ch` is a lowercase English letter.
56+
57+
58+
59+
## Solution
60+
61+
```javascript
62+
/**
63+
* @param {string} word
64+
* @param {character} ch
65+
* @return {string}
66+
*/
67+
var reversePrefix = function(word, ch) {
68+
var index = word.indexOf(ch);
69+
if (index === -1) return word;
70+
var arr = word.split('');
71+
for (var i = 0; i < index / 2; i++) {
72+
var tmp = arr[i];
73+
arr[i] = arr[index - i];
74+
arr[index - i] = tmp;
75+
}
76+
return arr.join('');
77+
};
78+
```
79+
80+
**Explain:**
81+
82+
nope.
83+
84+
**Complexity:**
85+
86+
* Time complexity : O(n).
87+
* Space complexity : O(n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# 237. Delete Node in a Linked List
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Linked List.
5+
- Similar Questions: Remove Linked List Elements, Remove Nodes From Linked List.
6+
7+
## Problem
8+
9+
There is a singly-linked list `head` and we want to delete a node `node` in it.
10+
11+
You are given the node to be deleted `node`. You will **not be given access** to the first node of `head`.
12+
13+
All the values of the linked list are **unique**, and it is guaranteed that the given node `node` is not the last node in the linked list.
14+
15+
Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:
16+
17+
18+
19+
- The value of the given node should not exist in the linked list.
20+
21+
- The number of nodes in the linked list should decrease by one.
22+
23+
- All the values before `node` should be in the same order.
24+
25+
- All the values after `node` should be in the same order.
26+
27+
28+
**Custom testing:**
29+
30+
31+
32+
- For the input, you should provide the entire linked list `head` and the node to be given `node`. `node` should not be the last node of the list and should be an actual node in the list.
33+
34+
- We will build the linked list and pass the node to your function.
35+
36+
- The output will be the entire list after calling your function.
37+
38+
39+
 
40+
Example 1:
41+
42+
![](https://assets.leetcode.com/uploads/2020/09/01/node1.jpg)
43+
44+
```
45+
Input: head = [4,5,1,9], node = 5
46+
Output: [4,1,9]
47+
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
48+
```
49+
50+
Example 2:
51+
52+
![](https://assets.leetcode.com/uploads/2020/09/01/node2.jpg)
53+
54+
```
55+
Input: head = [4,5,1,9], node = 1
56+
Output: [4,5,9]
57+
Explanation: You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
58+
```
59+
60+
 
61+
**Constraints:**
62+
63+
64+
65+
- The number of the nodes in the given list is in the range `[2, 1000]`.
66+
67+
- `-1000 <= Node.val <= 1000`
68+
69+
- The value of each node in the list is **unique**.
70+
71+
- The `node` to be deleted is **in the list** and is **not a tail** node.
72+
73+
74+
75+
## Solution
76+
77+
```javascript
78+
/**
79+
* Definition for singly-linked list.
80+
* function ListNode(val) {
81+
* this.val = val;
82+
* this.next = null;
83+
* }
84+
*/
85+
/**
86+
* @param {ListNode} node
87+
* @return {void} Do not return anything, modify node in-place instead.
88+
*/
89+
var deleteNode = function(node) {
90+
while (node.next.next) {
91+
node.val = node.next.val;
92+
node = node.next;
93+
}
94+
node.val = node.next.val;
95+
node.next = null;
96+
};
97+
```
98+
99+
**Explain:**
100+
101+
nope.
102+
103+
**Complexity:**
104+
105+
* Time complexity : O(n).
106+
* Space complexity : O(1).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# 2441. Largest Positive Integer That Exists With Its Negative
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Hash Table, Two Pointers, Sorting.
5+
- Similar Questions: Two Sum.
6+
7+
## Problem
8+
9+
Given an integer array `nums` that **does not contain** any zeros, find **the largest positive** integer `k` such that `-k` also exists in the array.
10+
11+
Return **the positive integer **`k`. If there is no such integer, return `-1`.
12+
13+
 
14+
Example 1:
15+
16+
```
17+
Input: nums = [-1,2,-3,3]
18+
Output: 3
19+
Explanation: 3 is the only valid k we can find in the array.
20+
```
21+
22+
Example 2:
23+
24+
```
25+
Input: nums = [-1,10,6,7,-7,1]
26+
Output: 7
27+
Explanation: Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
28+
```
29+
30+
Example 3:
31+
32+
```
33+
Input: nums = [-10,8,6,7,-2,-3]
34+
Output: -1
35+
Explanation: There is no a single valid k, we return -1.
36+
```
37+
38+
 
39+
**Constraints:**
40+
41+
42+
43+
- `1 <= nums.length <= 1000`
44+
45+
- `-1000 <= nums[i] <= 1000`
46+
47+
- `nums[i] != 0`
48+
49+
50+
51+
## Solution
52+
53+
```javascript
54+
/**
55+
* @param {number[]} nums
56+
* @return {number}
57+
*/
58+
var findMaxK = function(nums) {
59+
var map = {};
60+
var max = -1;
61+
for (var i = 0; i < nums.length; i++) {
62+
if (map[-nums[i]]) {
63+
max = Math.max(max, Math.abs(nums[i]));
64+
}
65+
map[nums[i]] = true;
66+
}
67+
return max;
68+
};
69+
```
70+
71+
**Explain:**
72+
73+
nope.
74+
75+
**Complexity:**
76+
77+
* Time complexity : O(n).
78+
* Space complexity : O(n).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# 2487. Remove Nodes From Linked List
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Linked List, Stack, Recursion, Monotonic Stack.
5+
- Similar Questions: Reverse Linked List, Delete Node in a Linked List, Next Greater Element I.
6+
7+
## Problem
8+
9+
You are given the `head` of a linked list.
10+
11+
Remove every node which has a node with a greater value anywhere to the right side of it.
12+
13+
Return **the **`head`** of the modified linked list.**
14+
15+
 
16+
Example 1:
17+
18+
![](https://assets.leetcode.com/uploads/2022/10/02/drawio.png)
19+
20+
```
21+
Input: head = [5,2,13,3,8]
22+
Output: [13,8]
23+
Explanation: The nodes that should be removed are 5, 2 and 3.
24+
- Node 13 is to the right of node 5.
25+
- Node 13 is to the right of node 2.
26+
- Node 8 is to the right of node 3.
27+
```
28+
29+
Example 2:
30+
31+
```
32+
Input: head = [1,1,1,1]
33+
Output: [1,1,1,1]
34+
Explanation: Every node has value 1, so no nodes are removed.
35+
```
36+
37+
 
38+
**Constraints:**
39+
40+
41+
42+
- The number of the nodes in the given list is in the range `[1, 105]`.
43+
44+
- `1 <= Node.val <= 105`
45+
46+
47+
48+
## Solution
49+
50+
```javascript
51+
/**
52+
* Definition for singly-linked list.
53+
* function ListNode(val, next) {
54+
* this.val = (val===undefined ? 0 : val)
55+
* this.next = (next===undefined ? null : next)
56+
* }
57+
*/
58+
/**
59+
* @param {ListNode} head
60+
* @return {ListNode}
61+
*/
62+
var removeNodes = function(head) {
63+
var newHead = new ListNode(Number.MAX_SAFE_INTEGER, head);
64+
var stack = [newHead];
65+
var current = head;
66+
while (current) {
67+
while (current.val > stack[stack.length - 1].val) {
68+
stack.pop();
69+
}
70+
stack[stack.length - 1].next = current;
71+
stack.push(current);
72+
current = current.next;
73+
}
74+
return newHead.next;
75+
};
76+
```
77+
78+
**Explain:**
79+
80+
nope.
81+
82+
**Complexity:**
83+
84+
* Time complexity : O(n).
85+
* Space complexity : O(n).

0 commit comments

Comments
 (0)