Skip to content

Commit 1777721

Browse files
committed
feat: add solutions to lc problems: No.0784,1776
* No.0784.Letter Case Permutation * No.1776.Car Fleet II
1 parent 689ae81 commit 1777721

File tree

12 files changed

+465
-158
lines changed

12 files changed

+465
-158
lines changed

solution/0700-0799/0784.Letter Case Permutation/README.md

Lines changed: 67 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42-
DFS 回溯法。
42+
**方法一:DFS**
43+
44+
由于 $s$ 中的每个字母都可以转换为大写或小写,因此可以使用 DFS 深度优先搜索的方法,枚举所有可能的情况。
45+
46+
具体地,从左到右遍历字符串 $s$,对于遍历到的每个字母,可以选择将其转变为大写或小写,然后继续遍历后面的字母。当遍历到字符串的末尾时,得到一个转换方案,将该方案加入答案即可。
47+
48+
转变大小写的方法可以使用位运算实现。对于一个字母,小写形式与大写形式的 ASCII 码之差为 $32$,因此,我们可以通过将该字母的 ASCII 码与 $32$ 进行异或运算来实现大小写转换。
49+
50+
时间复杂度 $O(n\times 2^n)$,其中 $n$ 是字符串 $s$ 的长度。对于每个字母,我们可以选择将其转换为大写或小写,因此一共有 $2^n$ 种转换方案。对于每种转换方案,我们需要 $O(n)$ 的时间生成一个新的字符串。
4351

4452
<!-- tabs:start -->
4553

@@ -50,36 +58,18 @@ DFS 回溯法。
5058
```python
5159
class Solution:
5260
def letterCasePermutation(self, s: str) -> List[str]:
53-
def dfs(i, t):
54-
if i == len(t):
61+
def dfs(i):
62+
if i >= len(s):
5563
ans.append(''.join(t))
5664
return
57-
dfs(i + 1, t)
65+
dfs(i + 1)
5866
if t[i].isalpha():
59-
t[i] = t[i].upper() if t[i].islower() else t[i].lower()
60-
dfs(i + 1, t)
67+
t[i] = chr(ord(t[i]) ^ 32)
68+
dfs(i + 1)
6169

62-
ans = []
6370
t = list(s)
64-
dfs(0, t)
65-
return ans
66-
```
67-
68-
```python
69-
class Solution:
70-
def letterCasePermutation(self, s: str) -> List[str]:
71-
def dfs(i, t):
72-
if i == len(s):
73-
ans.append(t)
74-
return
75-
if s[i].isalpha():
76-
dfs(i + 1, t + s[i].upper())
77-
dfs(i + 1, t + s[i].lower())
78-
else:
79-
dfs(i + 1, t + s[i])
80-
8171
ans = []
82-
dfs(0, '')
72+
dfs(0)
8373
return ans
8474
```
8575

@@ -89,22 +79,24 @@ class Solution:
8979

9080
```java
9181
class Solution {
92-
public List<String> letterCasePermutation(String S) {
93-
char[] cs = S.toCharArray();
94-
List<String> res = new ArrayList<>();
95-
dfs(cs, 0, res);
96-
return res;
82+
private List<String> ans = new ArrayList<>();
83+
private char[] t;
84+
85+
public List<String> letterCasePermutation(String s) {
86+
t = s.toCharArray();
87+
dfs(0);
88+
return ans;
9789
}
9890

99-
private void dfs(char[] cs, int i, List<String> res) {
100-
if (i == cs.length) {
101-
res.add(String.valueOf(cs));
91+
private void dfs(int i) {
92+
if (i >= t.length) {
93+
ans.add(String.valueOf(t));
10294
return;
10395
}
104-
dfs(cs, i + 1, res);
105-
if (cs[i] >= 'A') {
106-
cs[i] ^= 32;
107-
dfs(cs, i + 1, res);
96+
dfs(i + 1);
97+
if (t[i] >= 'A') {
98+
t[i] ^= 32;
99+
dfs(i + 1);
108100
}
109101
}
110102
}
@@ -115,33 +107,50 @@ class Solution {
115107
```cpp
116108
class Solution {
117109
public:
118-
vector<string> ans;
119-
string s;
120-
121110
vector<string> letterCasePermutation(string s) {
122-
this->s = s;
123-
string t = "";
124-
dfs(0, t);
111+
vector<string> ans;
112+
function<void(int)> dfs = [&](int i) {
113+
if (i >= s.size()) {
114+
ans.emplace_back(s);
115+
return;
116+
}
117+
dfs(i + 1);
118+
if (s[i] >= 'A') {
119+
s[i] ^= 32;
120+
dfs(i + 1);
121+
}
122+
};
123+
dfs(0);
125124
return ans;
126125
}
127-
128-
void dfs(int i, string t) {
129-
if (i == s.size()) {
130-
ans.push_back(t);
131-
return;
132-
}
133-
if (isalpha(s[i])) {
134-
char c1 = toupper(s[i]);
135-
char c2 = tolower(s[i]);
136-
dfs(i + 1, t + c1);
137-
dfs(i + 1, t + c2);
138-
} else {
139-
dfs(i + 1, t + s[i]);
140-
}
141-
}
142126
};
143127
```
144128
129+
### **Go**
130+
131+
```go
132+
func letterCasePermutation(s string) []string {
133+
ans := []string{}
134+
t := []byte(s)
135+
136+
var dfs func(int)
137+
dfs = func(i int) {
138+
if i >= len(t) {
139+
ans = append(ans, string(t))
140+
return
141+
}
142+
dfs(i + 1)
143+
if t[i] >= 'A' {
144+
t[i] ^= 32
145+
dfs(i + 1)
146+
}
147+
}
148+
149+
dfs(0)
150+
return ans
151+
}
152+
```
153+
145154
### **...**
146155

147156
```

solution/0700-0799/0784.Letter Case Permutation/README_EN.md

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -42,59 +42,43 @@ DFS.
4242
```python
4343
class Solution:
4444
def letterCasePermutation(self, s: str) -> List[str]:
45-
def dfs(i, t):
46-
if i == len(t):
45+
def dfs(i):
46+
if i >= len(s):
4747
ans.append(''.join(t))
4848
return
49-
dfs(i + 1, t)
49+
dfs(i + 1)
5050
if t[i].isalpha():
51-
t[i] = t[i].upper() if t[i].islower() else t[i].lower()
52-
dfs(i + 1, t)
51+
t[i] = chr(ord(t[i]) ^ 32)
52+
dfs(i + 1)
5353

54-
ans = []
5554
t = list(s)
56-
dfs(0, t)
57-
return ans
58-
```
59-
60-
```python
61-
class Solution:
62-
def letterCasePermutation(self, s: str) -> List[str]:
63-
def dfs(i, t):
64-
if i == len(s):
65-
ans.append(t)
66-
return
67-
if s[i].isalpha():
68-
dfs(i + 1, t + s[i].upper())
69-
dfs(i + 1, t + s[i].lower())
70-
else:
71-
dfs(i + 1, t + s[i])
72-
7355
ans = []
74-
dfs(0, '')
56+
dfs(0)
7557
return ans
7658
```
7759

7860
### **Java**
7961

8062
```java
8163
class Solution {
82-
public List<String> letterCasePermutation(String S) {
83-
char[] cs = S.toCharArray();
84-
List<String> res = new ArrayList<>();
85-
dfs(cs, 0, res);
86-
return res;
64+
private List<String> ans = new ArrayList<>();
65+
private char[] t;
66+
67+
public List<String> letterCasePermutation(String s) {
68+
t = s.toCharArray();
69+
dfs(0);
70+
return ans;
8771
}
8872

89-
private void dfs(char[] cs, int i, List<String> res) {
90-
if (i == cs.length) {
91-
res.add(String.valueOf(cs));
73+
private void dfs(int i) {
74+
if (i >= t.length) {
75+
ans.add(String.valueOf(t));
9276
return;
9377
}
94-
dfs(cs, i + 1, res);
95-
if (cs[i] >= 'A') {
96-
cs[i] ^= 32;
97-
dfs(cs, i + 1, res);
78+
dfs(i + 1);
79+
if (t[i] >= 'A') {
80+
t[i] ^= 32;
81+
dfs(i + 1);
9882
}
9983
}
10084
}
@@ -105,33 +89,50 @@ class Solution {
10589
```cpp
10690
class Solution {
10791
public:
108-
vector<string> ans;
109-
string s;
110-
11192
vector<string> letterCasePermutation(string s) {
112-
this->s = s;
113-
string t = "";
114-
dfs(0, t);
93+
vector<string> ans;
94+
function<void(int)> dfs = [&](int i) {
95+
if (i >= s.size()) {
96+
ans.emplace_back(s);
97+
return;
98+
}
99+
dfs(i + 1);
100+
if (s[i] >= 'A') {
101+
s[i] ^= 32;
102+
dfs(i + 1);
103+
}
104+
};
105+
dfs(0);
115106
return ans;
116107
}
117-
118-
void dfs(int i, string t) {
119-
if (i == s.size()) {
120-
ans.push_back(t);
121-
return;
122-
}
123-
if (isalpha(s[i])) {
124-
char c1 = toupper(s[i]);
125-
char c2 = tolower(s[i]);
126-
dfs(i + 1, t + c1);
127-
dfs(i + 1, t + c2);
128-
} else {
129-
dfs(i + 1, t + s[i]);
130-
}
131-
}
132108
};
133109
```
134110
111+
### **Go**
112+
113+
```go
114+
func letterCasePermutation(s string) []string {
115+
ans := []string{}
116+
t := []byte(s)
117+
118+
var dfs func(int)
119+
dfs = func(i int) {
120+
if i >= len(t) {
121+
ans = append(ans, string(t))
122+
return
123+
}
124+
dfs(i + 1)
125+
if t[i] >= 'A' {
126+
t[i] ^= 32
127+
dfs(i + 1)
128+
}
129+
}
130+
131+
dfs(0)
132+
return ans
133+
}
134+
```
135+
135136
### **...**
136137

137138
```
Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
class Solution {
22
public:
3-
vector<string> ans;
4-
string s;
5-
63
vector<string> letterCasePermutation(string s) {
7-
this->s = s;
8-
string t = "";
9-
dfs(0, t);
4+
vector<string> ans;
5+
function<void(int)> dfs = [&](int i) {
6+
if (i >= s.size()) {
7+
ans.emplace_back(s);
8+
return;
9+
}
10+
dfs(i + 1);
11+
if (s[i] >= 'A') {
12+
s[i] ^= 32;
13+
dfs(i + 1);
14+
}
15+
};
16+
dfs(0);
1017
return ans;
1118
}
12-
13-
void dfs(int i, string t) {
14-
if (i == s.size()) {
15-
ans.push_back(t);
16-
return;
17-
}
18-
if (isalpha(s[i])) {
19-
char c1 = toupper(s[i]);
20-
char c2 = tolower(s[i]);
21-
dfs(i + 1, t + c1);
22-
dfs(i + 1, t + c2);
23-
} else {
24-
dfs(i + 1, t + s[i]);
25-
}
26-
}
2719
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func letterCasePermutation(s string) []string {
2+
ans := []string{}
3+
t := []byte(s)
4+
5+
var dfs func(int)
6+
dfs = func(i int) {
7+
if i >= len(t) {
8+
ans = append(ans, string(t))
9+
return
10+
}
11+
dfs(i + 1)
12+
if t[i] >= 'A' {
13+
t[i] ^= 32
14+
dfs(i + 1)
15+
}
16+
}
17+
18+
dfs(0)
19+
return ans
20+
}

0 commit comments

Comments
 (0)