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 424dc09

Browse files
feat: add solutions to lc problems: No.3210~3212 (doocs#3219)
* No.3210.Find the Encrypted String * No.3211.Generate Binary Strings Without Adjacent Zeros * No.3212.Count Submatrices With Equal Frequency of X and Y
1 parent 58496a9 commit 424dc09

File tree

29 files changed

+846
-52
lines changed

29 files changed

+846
-52
lines changed

‎solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ func letterCombinations(digits string) []string {
165165

166166
```ts
167167
function letterCombinations(digits: string): string[] {
168-
if (digits.length == 0) {
168+
if (digits.length === 0) {
169169
return [];
170170
}
171171
const ans: string[] = [''];
172172
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
173173
for (const i of digits) {
174-
const s = d[parseInt(i) - 2];
174+
const s = d[+i - 2];
175175
const t: string[] = [];
176176
for (const a of ans) {
177177
for (const b of s) {
@@ -218,13 +218,13 @@ impl Solution {
218218
* @return {string[]}
219219
*/
220220
var letterCombinations = function (digits) {
221-
if (digits.length == 0) {
221+
if (digits.length === 0) {
222222
return [];
223223
}
224224
const ans = [''];
225225
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
226226
for (const i of digits) {
227-
const s = d[parseInt(i) - 2];
227+
const s = d[+i - 2];
228228
const t = [];
229229
for (const a of ans) {
230230
for (const b of s) {
@@ -392,7 +392,7 @@ func letterCombinations(digits string) (ans []string) {
392392

393393
```ts
394394
function letterCombinations(digits: string): string[] {
395-
if (digits.length == 0) {
395+
if (digits.length === 0) {
396396
return [];
397397
}
398398
const ans: string[] = [];
@@ -403,7 +403,7 @@ function letterCombinations(digits: string): string[] {
403403
ans.push(t.join(''));
404404
return;
405405
}
406-
const s = d[parseInt(digits[i]) - 2];
406+
const s = d[+digits[i] - 2];
407407
for (const c of s) {
408408
t.push(c);
409409
dfs(i + 1);
@@ -453,7 +453,7 @@ impl Solution {
453453
* @return {string[]}
454454
*/
455455
var letterCombinations = function (digits) {
456-
if (digits.length == 0) {
456+
if (digits.length === 0) {
457457
return [];
458458
}
459459
const ans = [];
@@ -464,7 +464,7 @@ var letterCombinations = function (digits) {
464464
ans.push(t.join(''));
465465
return;
466466
}
467-
const s = d[parseInt(digits[i]) - 2];
467+
const s = d[+digits[i] - 2];
468468
for (const c of s) {
469469
t.push(c);
470470
dfs(i + 1);

‎solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ func letterCombinations(digits string) []string {
161161

162162
```ts
163163
function letterCombinations(digits: string): string[] {
164-
if (digits.length == 0) {
164+
if (digits.length === 0) {
165165
return [];
166166
}
167167
const ans: string[] = [''];
168168
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
169169
for (const i of digits) {
170-
const s = d[parseInt(i) - 2];
170+
const s = d[+i - 2];
171171
const t: string[] = [];
172172
for (const a of ans) {
173173
for (const b of s) {
@@ -214,13 +214,13 @@ impl Solution {
214214
* @return {string[]}
215215
*/
216216
var letterCombinations = function (digits) {
217-
if (digits.length == 0) {
217+
if (digits.length === 0) {
218218
return [];
219219
}
220220
const ans = [''];
221221
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
222222
for (const i of digits) {
223-
const s = d[parseInt(i) - 2];
223+
const s = d[+i - 2];
224224
const t = [];
225225
for (const a of ans) {
226226
for (const b of s) {
@@ -388,7 +388,7 @@ func letterCombinations(digits string) (ans []string) {
388388

389389
```ts
390390
function letterCombinations(digits: string): string[] {
391-
if (digits.length == 0) {
391+
if (digits.length === 0) {
392392
return [];
393393
}
394394
const ans: string[] = [];
@@ -399,7 +399,7 @@ function letterCombinations(digits: string): string[] {
399399
ans.push(t.join(''));
400400
return;
401401
}
402-
const s = d[parseInt(digits[i]) - 2];
402+
const s = d[+digits[i] - 2];
403403
for (const c of s) {
404404
t.push(c);
405405
dfs(i + 1);
@@ -449,7 +449,7 @@ impl Solution {
449449
* @return {string[]}
450450
*/
451451
var letterCombinations = function (digits) {
452-
if (digits.length == 0) {
452+
if (digits.length === 0) {
453453
return [];
454454
}
455455
const ans = [];
@@ -460,7 +460,7 @@ var letterCombinations = function (digits) {
460460
ans.push(t.join(''));
461461
return;
462462
}
463-
const s = d[parseInt(digits[i]) - 2];
463+
const s = d[+digits[i] - 2];
464464
for (const c of s) {
465465
t.push(c);
466466
dfs(i + 1);

‎solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
* @return {string[]}
44
*/
55
var letterCombinations = function (digits) {
6-
if (digits.length == 0) {
6+
if (digits.length === 0) {
77
return [];
88
}
99
const ans = [''];
1010
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
1111
for (const i of digits) {
12-
const s = d[parseInt(i) - 2];
12+
const s = d[+i - 2];
1313
const t = [];
1414
for (const a of ans) {
1515
for (const b of s) {

‎solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
function letterCombinations(digits: string): string[] {
2-
if (digits.length == 0) {
2+
if (digits.length === 0) {
33
return [];
44
}
55
const ans: string[] = [''];
66
const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz'];
77
for (const i of digits) {
8-
const s = d[parseInt(i) - 2];
8+
const s = d[+i - 2];
99
const t: string[] = [];
1010
for (const a of ans) {
1111
for (const b of s) {

‎solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @return {string[]}
44
*/
55
var letterCombinations = function (digits) {
6-
if (digits.length == 0) {
6+
if (digits.length === 0) {
77
return [];
88
}
99
const ans = [];
@@ -14,7 +14,7 @@ var letterCombinations = function (digits) {
1414
ans.push(t.join(''));
1515
return;
1616
}
17-
const s = d[parseInt(digits[i]) - 2];
17+
const s = d[+digits[i] - 2];
1818
for (const c of s) {
1919
t.push(c);
2020
dfs(i + 1);

‎solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function letterCombinations(digits: string): string[] {
2-
if (digits.length == 0) {
2+
if (digits.length === 0) {
33
return [];
44
}
55
const ans: string[] = [];
@@ -10,7 +10,7 @@ function letterCombinations(digits: string): string[] {
1010
ans.push(t.join(''));
1111
return;
1212
}
13-
const s = d[parseInt(digits[i]) - 2];
13+
const s = d[+digits[i] - 2];
1414
for (const c of s) {
1515
t.push(c);
1616
dfs(i + 1);

‎solution/0700-0799/0724.Find Pivot Index/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ tags:
7777

7878
### 方法一:前缀和
7979

80-
我们定义变量 $left$ 表示数组 `nums` 中下标 $i$ 左侧元素之和,变量 $right$ 表示数组 `nums` 中下标 $i$ 右侧元素之和。初始时 $left = 0,ドル $right = \sum_{i = 0}^{n - 1} nums[i]$。
80+
我们定义变量 $left$ 表示数组 $\textit{nums}$ 中下标 $i$ 左侧元素之和,变量 $right$ 表示数组 $\textit{nums}$ 中下标 $i$ 右侧元素之和。初始时 $left = 0,ドル $right = \sum_{i = 0}^{n - 1} nums[i]$。
8181

82-
遍历数组 `nums`,对于当前遍历到的数字 $x,ドル我们更新 $right = right - x,ドル此时如果 $left=right,ドル说明当前下标 $i$ 就是中间位置,直接返回即可。否则,我们更新 $left = left + x,ドル继续遍历下一个数字。
82+
遍历数组 $\textit{nums}$,对于当前遍历到的数字 $x,ドル我们更新 $right = right - x,ドル此时如果 $left=right,ドル说明当前下标 $i$ 就是中间位置,直接返回即可。否则,我们更新 $left = left + x,ドル继续遍历下一个数字。
8383

8484
遍历结束,如果没有找到中间位置,返回 $-1$。
8585

86-
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
86+
时间复杂度 $O(n),ドル空间复杂度 $O(1)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
8787

8888
相似题目:
8989

‎solution/0700-0799/0724.Find Pivot Index/README_EN.md‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,20 @@ Right sum = nums[1] + nums[2] = 1 + -1 = 0
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Prefix Sum
77+
78+
We define a variable $left$ to represent the sum of elements to the left of index $i$ in the array $\textit{nums},ドル and a variable $right$ to represent the sum of elements to the right of index $i$ in the array $\textit{nums}$. Initially, $left = 0,ドル $right = \sum_{i = 0}^{n - 1} nums[i]$.
79+
80+
We traverse the array $\textit{nums}$. For the current number $x$ being traversed, we update $right = right - x$. At this point, if $left = right,ドル it indicates that the current index $i$ is the middle position, and we can return it directly. Otherwise, we update $left = left + x$ and continue to traverse the next number.
81+
82+
If the middle position is not found by the end of the traversal, return $-1$.
83+
84+
The time complexity is $O(n),ドル and the space complexity is $O(1)$. Here, $n$ is the length of the array $\textit{nums}$.
85+
86+
Similar Problems:
87+
88+
- [1991. Find the Middle Index in Array](https://github.com/doocs/leetcode/blob/main/solution/1900-1999/1991.Find%20the%20Middle%20Index%20in%20Array/README_EN.md)
89+
- [2574. Left and Right Sum Differences](https://github.com/doocs/leetcode/blob/main/solution/2500-2599/2574.Left%20and%20Right%20Sum%20Differences/README_EN.md)
7790

7891
<!-- tabs:start -->
7992

‎solution/3200-3299/3210.Find the Encrypted String/README.md‎

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,32 +69,80 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3210.Fi
6969

7070
<!-- solution:start -->
7171

72-
### 方法一
72+
### 方法一:模拟
73+
74+
我们可以使用模拟的方法,对字符串的第 $i$ 个字符,我们将其替换为字符串的第 $(i + k) \bmod n$ 个字符。
75+
76+
时间复杂度 $O(n),ドル空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
7377

7478
<!-- tabs:start -->
7579

7680
#### Python3
7781

7882
```python
79-
83+
class Solution:
84+
def getEncryptedString(self, s: str, k: int) -> str:
85+
cs = list(s)
86+
n = len(s)
87+
for i in range(n):
88+
cs[i] = s[(i + k) % n]
89+
return "".join(cs)
8090
```
8191

8292
#### Java
8393

8494
```java
85-
95+
class Solution {
96+
public String getEncryptedString(String s, int k) {
97+
char[] cs = s.toCharArray();
98+
int n = cs.length;
99+
for (int i = 0; i < n; ++i) {
100+
cs[i] = s.charAt((i + k) % n);
101+
}
102+
return new String(cs);
103+
}
104+
}
86105
```
87106

88107
#### C++
89108

90109
```cpp
91-
110+
class Solution {
111+
public:
112+
string getEncryptedString(string s, int k) {
113+
int n = s.length();
114+
string cs(n, ' ');
115+
for (int i = 0; i < n; ++i) {
116+
cs[i] = s[(i + k) % n];
117+
}
118+
return cs;
119+
}
120+
};
92121
```
93122
94123
#### Go
95124
96125
```go
126+
func getEncryptedString(s string, k int) string {
127+
cs := []byte(s)
128+
for i := range s {
129+
cs[i] = s[(i+k)%len(s)]
130+
}
131+
return string(cs)
132+
}
133+
```
97134

135+
#### TypeScript
136+
137+
```ts
138+
function getEncryptedString(s: string, k: number): string {
139+
const cs: string[] = [];
140+
const n = s.length;
141+
for (let i = 0; i < n; ++i) {
142+
cs[i] = s[(i + k) % n];
143+
}
144+
return cs.join('');
145+
}
98146
```
99147

100148
<!-- tabs:end -->

0 commit comments

Comments
(0)

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