Skip to content

Commit a55e8be

Browse files
committed
feat: add solutions to lc problems: No.2119~2122
* No.2119.A Number After a Double Reversal * No.2120.Execution of All Suffix Instructions Staying in a Grid * No.2121.Intervals Between Identical Elements * No.2122.Recover the Original Array
1 parent 1516ff8 commit a55e8be

File tree

26 files changed

+1057
-11
lines changed

26 files changed

+1057
-11
lines changed

lcci/summary_en.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- [LCCI Solutions]
1+
- LCCI Solutions
22
- [01.01. Is Unique](/lcci/01.01.Is%20Unique/README_EN.md)
33
- [01.02. Check Permutation](/lcci/01.02.Check%20Permutation/README_EN.md)
44
- [01.03. String to URL](/lcci/01.03.String%20to%20URL/README_EN.md)

solution/2100-2199/2119.A Number After a Double Reversal/README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,40 @@
5555
<!-- 这里可写当前语言的特殊实现逻辑 -->
5656

5757
```python
58-
58+
class Solution:
59+
def isSameAfterReversals(self, num: int) -> bool:
60+
return num == 0 or num % 10 != 0
5961
```
6062

6163
### **Java**
6264

6365
<!-- 这里可写当前语言的特殊实现逻辑 -->
6466

6567
```java
68+
class Solution {
69+
public boolean isSameAfterReversals(int num) {
70+
return num == 0 || num % 10 != 0;
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
bool isSameAfterReversals(int num) {
81+
return num == 0 || num % 10 != 0;
82+
}
83+
};
84+
```
85+
86+
### **Go**
6687
88+
```go
89+
func isSameAfterReversals(num int) bool {
90+
return num == 0 || num%10 != 0
91+
}
6792
```
6893

6994
### **TypeScript**

solution/2100-2199/2119.A Number After a Double Reversal/README_EN.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,38 @@
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def isSameAfterReversals(self, num: int) -> bool:
56+
return num == 0 or num % 10 != 0
5557
```
5658

5759
### **Java**
5860

5961
```java
62+
class Solution {
63+
public boolean isSameAfterReversals(int num) {
64+
return num == 0 || num % 10 != 0;
65+
}
66+
}
67+
```
68+
69+
### **C++**
70+
71+
```cpp
72+
class Solution {
73+
public:
74+
bool isSameAfterReversals(int num) {
75+
return num == 0 || num % 10 != 0;
76+
}
77+
};
78+
```
79+
80+
### **Go**
6081
82+
```go
83+
func isSameAfterReversals(num int) bool {
84+
return num == 0 || num%10 != 0
85+
}
6186
```
6287

6388
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
bool isSameAfterReversals(int num) {
4+
return num == 0 || num % 10 != 0;
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func isSameAfterReversals(num int) bool {
2+
return num == 0 || num%10 != 0
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public boolean isSameAfterReversals(int num) {
3+
return num == 0 || num % 10 != 0;
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def isSameAfterReversals(self, num: int) -> bool:
3+
return num == 0 or num % 10 != 0

solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/README.md

+115-2
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,140 @@
7373
<li><code>s</code> 由 <code>'L'</code>、<code>'R'</code>、<code>'U'</code> 和 <code>'D'</code> 组成</li>
7474
</ul>
7575

76-
7776
## 解法
7877

7978
<!-- 这里可写通用的实现逻辑 -->
8079

80+
直接模拟。
81+
8182
<!-- tabs:start -->
8283

8384
### **Python3**
8485

8586
<!-- 这里可写当前语言的特殊实现逻辑 -->
8687

8788
```python
88-
89+
class Solution:
90+
def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:
91+
ans = []
92+
m = len(s)
93+
mp = {
94+
"L": [0, -1],
95+
"R": [0, 1],
96+
"U": [-1, 0],
97+
"D": [1, 0]
98+
}
99+
for i in range(m):
100+
x, y = startPos
101+
t = 0
102+
for j in range(i, m):
103+
a, b = mp[s[j]]
104+
if 0 <= x + a < n and 0 <= y + b < n:
105+
x, y, t = x + a, y + b, t + 1
106+
else:
107+
break
108+
ans.append(t)
109+
return ans
89110
```
90111

91112
### **Java**
92113

93114
<!-- 这里可写当前语言的特殊实现逻辑 -->
94115

95116
```java
117+
class Solution {
118+
public int[] executeInstructions(int n, int[] startPos, String s) {
119+
int m = s.length();
120+
int[] ans = new int[m];
121+
Map<Character, int[]> mp = new HashMap<>(4);
122+
mp.put('L', new int[]{0, -1});
123+
mp.put('R', new int[]{0, 1});
124+
mp.put('U', new int[]{-1, 0});
125+
mp.put('D', new int[]{1, 0});
126+
for (int i = 0; i < m; ++i) {
127+
int x = startPos[0], y = startPos[1];
128+
int t = 0;
129+
for (int j = i; j < m; ++j) {
130+
char c = s.charAt(j);
131+
int a = mp.get(c)[0], b = mp.get(c)[1];
132+
if (0 <= x + a && x + a < n && 0 <= y + b && y + b < n) {
133+
x += a;
134+
y += b;
135+
++t;
136+
} else {
137+
break;
138+
}
139+
}
140+
ans[i] = t;
141+
}
142+
return ans;
143+
}
144+
}
145+
```
146+
147+
### **C++**
148+
149+
```cpp
150+
class Solution {
151+
public:
152+
vector<int> executeInstructions(int n, vector<int>& startPos, string s) {
153+
int m = s.size();
154+
vector<int> ans(m);
155+
unordered_map<char, vector<int>> mp;
156+
mp['L'] = {0, -1};
157+
mp['R'] = {0, 1};
158+
mp['U'] = {-1, 0};
159+
mp['D'] = {1, 0};
160+
for (int i = 0; i < m; ++i)
161+
{
162+
int x = startPos[0], y = startPos[1];
163+
int t = 0;
164+
for (int j = i; j < m; ++j)
165+
{
166+
int a = mp[s[j]][0], b = mp[s[j]][1];
167+
if (0 <= x + a && x + a < n && 0 <= y + b && y + b < n)
168+
{
169+
x += a;
170+
y += b;
171+
++t;
172+
}
173+
else break;
174+
}
175+
ans[i] = t;
176+
}
177+
return ans;
178+
}
179+
};
180+
```
96181
182+
### **Go**
183+
184+
```go
185+
func executeInstructions(n int, startPos []int, s string) []int {
186+
m := len(s)
187+
mp := make(map[byte][]int)
188+
mp['L'] = []int{0, -1}
189+
mp['R'] = []int{0, 1}
190+
mp['U'] = []int{-1, 0}
191+
mp['D'] = []int{1, 0}
192+
ans := make([]int, m)
193+
for i := 0; i < m; i++ {
194+
x, y := startPos[0], startPos[1]
195+
t := 0
196+
for j := i; j < m; j++ {
197+
a, b := mp[s[j]][0], mp[s[j]][1]
198+
if 0 <= x+a && x+a < n && 0 <= y+b && y+b < n {
199+
x += a
200+
y += b
201+
t++
202+
} else {
203+
break
204+
}
205+
}
206+
ans[i] = t
207+
}
208+
return ans
209+
}
97210
```
98211

99212
### **TypeScript**

solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/README_EN.md

+113-1
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,125 @@
7070
### **Python3**
7171

7272
```python
73-
73+
class Solution:
74+
def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:
75+
ans = []
76+
m = len(s)
77+
mp = {
78+
"L": [0, -1],
79+
"R": [0, 1],
80+
"U": [-1, 0],
81+
"D": [1, 0]
82+
}
83+
for i in range(m):
84+
x, y = startPos
85+
t = 0
86+
for j in range(i, m):
87+
a, b = mp[s[j]]
88+
if 0 <= x + a < n and 0 <= y + b < n:
89+
x, y, t = x + a, y + b, t + 1
90+
else:
91+
break
92+
ans.append(t)
93+
return ans
7494
```
7595

7696
### **Java**
7797

7898
```java
99+
class Solution {
100+
public int[] executeInstructions(int n, int[] startPos, String s) {
101+
int m = s.length();
102+
int[] ans = new int[m];
103+
Map<Character, int[]> mp = new HashMap<>(4);
104+
mp.put('L', new int[]{0, -1});
105+
mp.put('R', new int[]{0, 1});
106+
mp.put('U', new int[]{-1, 0});
107+
mp.put('D', new int[]{1, 0});
108+
for (int i = 0; i < m; ++i) {
109+
int x = startPos[0], y = startPos[1];
110+
int t = 0;
111+
for (int j = i; j < m; ++j) {
112+
char c = s.charAt(j);
113+
int a = mp.get(c)[0], b = mp.get(c)[1];
114+
if (0 <= x + a && x + a < n && 0 <= y + b && y + b < n) {
115+
x += a;
116+
y += b;
117+
++t;
118+
} else {
119+
break;
120+
}
121+
}
122+
ans[i] = t;
123+
}
124+
return ans;
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
vector<int> executeInstructions(int n, vector<int>& startPos, string s) {
135+
int m = s.size();
136+
vector<int> ans(m);
137+
unordered_map<char, vector<int>> mp;
138+
mp['L'] = {0, -1};
139+
mp['R'] = {0, 1};
140+
mp['U'] = {-1, 0};
141+
mp['D'] = {1, 0};
142+
for (int i = 0; i < m; ++i)
143+
{
144+
int x = startPos[0], y = startPos[1];
145+
int t = 0;
146+
for (int j = i; j < m; ++j)
147+
{
148+
int a = mp[s[j]][0], b = mp[s[j]][1];
149+
if (0 <= x + a && x + a < n && 0 <= y + b && y + b < n)
150+
{
151+
x += a;
152+
y += b;
153+
++t;
154+
}
155+
else break;
156+
}
157+
ans[i] = t;
158+
}
159+
return ans;
160+
}
161+
};
162+
```
79163
164+
### **Go**
165+
166+
```go
167+
func executeInstructions(n int, startPos []int, s string) []int {
168+
m := len(s)
169+
mp := make(map[byte][]int)
170+
mp['L'] = []int{0, -1}
171+
mp['R'] = []int{0, 1}
172+
mp['U'] = []int{-1, 0}
173+
mp['D'] = []int{1, 0}
174+
ans := make([]int, m)
175+
for i := 0; i < m; i++ {
176+
x, y := startPos[0], startPos[1]
177+
t := 0
178+
for j := i; j < m; j++ {
179+
a, b := mp[s[j]][0], mp[s[j]][1]
180+
if 0 <= x+a && x+a < n && 0 <= y+b && y+b < n {
181+
x += a
182+
y += b
183+
t++
184+
} else {
185+
break
186+
}
187+
}
188+
ans[i] = t
189+
}
190+
return ans
191+
}
80192
```
81193

82194
### **TypeScript**

0 commit comments

Comments
 (0)