Skip to content

Commit c73a8fb

Browse files
authored
feat: add solutions to lc problems: No.1427,1428 (#2065)
* No.1427.Perform String Shifts * No.1428.Leftmost Column with at Least a One
1 parent 3347850 commit c73a8fb

File tree

14 files changed

+574
-295
lines changed

14 files changed

+574
-295
lines changed

Diff for: solution/1400-1499/1427.Perform String Shifts/README.md

+20-8
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@
5858

5959
**方法一:模拟**
6060

61-
遍历 `shift`,累加(减)得到最终偏移量 $x$,取模后对字符串 `s` 进行左移或右移
61+
我们不妨记字符串 $s$ 的长度为 $n$。接下来遍历数组 $shift$,累加得到最终的偏移量 $x$,然后将 $x$ 对 $n$ 取模,最终结果就是将 $s$ 的前 $n - x$ 个字符移动到末尾
6262

63-
时间复杂度 $O(n+m)$其中 $n$ 为字符串 `s` 的长度,$m$ `shift` 的长度。
63+
时间复杂度 $O(n + m)$其中 $n$ $m$ 分别是字符串 $s$ 的长度和数组 $shift$ 的长度。空间复杂度 $O(1)$
6464

6565
<!-- tabs:start -->
6666

@@ -71,11 +71,7 @@
7171
```python
7272
class Solution:
7373
def stringShift(self, s: str, shift: List[List[int]]) -> str:
74-
x = 0
75-
for a, b in shift:
76-
if a == 0:
77-
b = -b
78-
x += b
74+
x = sum((b if a else -b) for a, b in shift)
7975
x %= len(s)
8076
return s[-x:] + s[:-x]
8177
```
@@ -90,7 +86,7 @@ class Solution {
9086
int x = 0;
9187
for (var e : shift) {
9288
if (e[0] == 0) {
93-
e[1] = -e[1];
89+
e[1] *= -1;
9490
}
9591
x += e[1];
9692
}
@@ -138,6 +134,22 @@ func stringShift(s string, shift [][]int) string {
138134
}
139135
```
140136

137+
### **TypeScript**
138+
139+
```ts
140+
function stringShift(s: string, shift: number[][]): string {
141+
let x = 0;
142+
for (const [a, b] of shift) {
143+
x += a === 0 ? -b : b;
144+
}
145+
x %= s.length;
146+
if (x < 0) {
147+
x += s.length;
148+
}
149+
return s.slice(-x) + s.slice(0, -x);
150+
}
151+
```
152+
141153
### **...**
142154

143155
```

Diff for: solution/1400-1499/1427.Perform String Shifts/README_EN.md

+24-6
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,20 @@
5050

5151
## Solutions
5252

53+
**Solution 1: Simulation**
54+
55+
We can denote the length of the string $s$ as $n$. Next, we traverse the array $shift$, accumulate to get the final offset $x$, then take $x$ modulo $n$, the final result is to move the first $n - x$ characters of $s$ to the end.
56+
57+
The time complexity is $O(n + m)$, where $n$ and $m$ are the lengths of the string $s$ and the array $shift$ respectively. The space complexity is $O(1)$.
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**
5662

5763
```python
5864
class Solution:
5965
def stringShift(self, s: str, shift: List[List[int]]) -> str:
60-
x = 0
61-
for a, b in shift:
62-
if a == 0:
63-
b = -b
64-
x += b
66+
x = sum((b if a else -b) for a, b in shift)
6567
x %= len(s)
6668
return s[-x:] + s[:-x]
6769
```
@@ -74,7 +76,7 @@ class Solution {
7476
int x = 0;
7577
for (var e : shift) {
7678
if (e[0] == 0) {
77-
e[1] = -e[1];
79+
e[1] *= -1;
7880
}
7981
x += e[1];
8082
}
@@ -122,6 +124,22 @@ func stringShift(s string, shift [][]int) string {
122124
}
123125
```
124126

127+
### **TypeScript**
128+
129+
```ts
130+
function stringShift(s: string, shift: number[][]): string {
131+
let x = 0;
132+
for (const [a, b] of shift) {
133+
x += a === 0 ? -b : b;
134+
}
135+
x %= s.length;
136+
if (x < 0) {
137+
x += s.length;
138+
}
139+
return s.slice(-x) + s.slice(0, -x);
140+
}
141+
```
142+
125143
### **...**
126144

127145
```
+13-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
class Solution {
2-
public String stringShift(String s, int[][] shift) {
3-
int x = 0;
4-
for (var e : shift) {
5-
if (e[0] == 0) {
6-
e[1] = -e[1];
7-
}
8-
x += e[1];
9-
}
10-
int n = s.length();
11-
x = (x % n + n) % n;
12-
return s.substring(n - x) + s.substring(0, n - x);
13-
}
1+
class Solution {
2+
public String stringShift(String s, int[][] shift) {
3+
int x = 0;
4+
for (var e : shift) {
5+
if (e[0] == 0) {
6+
e[1] *= -1;
7+
}
8+
x += e[1];
9+
}
10+
int n = s.length();
11+
x = (x % n + n) % n;
12+
return s.substring(n - x) + s.substring(0, n - x);
13+
}
1414
}
+5-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
class Solution:
2-
def stringShift(self, s: str, shift: List[List[int]]) -> str:
3-
x = 0
4-
for a, b in shift:
5-
if a == 0:
6-
b = -b
7-
x += b
8-
x %= len(s)
9-
return s[-x:] + s[:-x]
1+
class Solution:
2+
def stringShift(self, s: str, shift: List[List[int]]) -> str:
3+
x = sum((b if a else -b) for a, b in shift)
4+
x %= len(s)
5+
return s[-x:] + s[:-x]
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function stringShift(s: string, shift: number[][]): string {
2+
let x = 0;
3+
for (const [a, b] of shift) {
4+
x += a === 0 ? -b : b;
5+
}
6+
x %= s.length;
7+
if (x < 0) {
8+
x += s.length;
9+
}
10+
return s.slice(-x) + s.slice(0, -x);
11+
}

0 commit comments

Comments
 (0)