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 bd55d15

Browse files
update content
1 parent f3b9207 commit bd55d15

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

‎多语言解法代码/solution_code.md

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,42 +2624,41 @@ func backtrack(left, right int, track *string, res *[]string) {
26242624
```
26252625

26262626
```java
2627-
// by labuladong (java)
2627+
// by chatGPT (java)
26282628
class Solution {
2629-
public:
2630-
vector<string> generateParenthesis(int n) {
2631-
if (n == 0) return {};
2629+
public List<String> generateParenthesis(int n) {
2630+
if (n == 0) return new ArrayList<>();
26322631
// 记录所有合法的括号组合
2633-
vector<string> res;
2632+
List<String> res = new ArrayList<>();
26342633
// 回溯过程中的路径
2635-
string track;
2634+
StringBuilder track = new StringBuilder();
26362635
// 可用的左括号和右括号数量初始化为 n
26372636
backtrack(n, n, track, res);
26382637
return res;
26392638
}
26402639

2641-
// 可用的左括号数量为 left 个,可用的右括号数量为 rgiht
2640+
// 可用的左括号数量为 left 个,可用的右括号数量为 right
26422641
void backtrack(int left, int right,
2643-
string& track, vector<string>& res) {
2642+
StringBuilder track, List<String> res) {
26442643
// 若左括号剩下的多,说明不合法
26452644
if (right < left) return;
26462645
// 数量小于 0 肯定是不合法的
26472646
if (left < 0 || right < 0) return;
26482647
// 当所有括号都恰好用完时,得到一个合法的括号组合
26492648
if (left == 0 && right == 0) {
2650-
res.push_back(track);
2649+
res.add(track.toString());
26512650
return;
26522651
}
26532652

26542653
// 尝试放一个左括号
2655-
track.push_back('('); // 选择
2654+
track.append('('); // 选择
26562655
backtrack(left - 1, right, track, res);
2657-
track.pop_back(); // 撤消选择
2656+
track.deleteCharAt(track.length() - 1); // 撤消选择
26582657

26592658
// 尝试放一个右括号
2660-
track.push_back(')'); // 选择
2659+
track.append(')'); // 选择
26612660
backtrack(left, right - 1, track, res);
2662-
track.pop_back(); // 撤消选择
2661+
track.deleteCharAt(track.length() - 1); // 撤消选择
26632662
}
26642663
}
26652664
```
@@ -26873,7 +26872,7 @@ func backtrack(left int, right int, track *string, res *[]string) {
2687326872
```
2687426873

2687526874
```java
26876-
// by labuladong (java)
26875+
// by chatGPT (java)
2687726876
class Solution {
2687826877
public List<String> generateParenthesis(int n) {
2687926878
if (n == 0) return new ArrayList<>();
@@ -32965,6 +32964,53 @@ class Solution:
3296532964

3296632965
https://leetcode.cn/problems/lMSNwu 的多语言解法👆
3296732966

32967+
https://leetcode.cn/problems/letter-case-permutation 的多语言解法👇
32968+
32969+
```java
32970+
// by labuladong (java)
32971+
class Solution {
32972+
public List<String> letterCasePermutation(String s) {
32973+
backtrack(s, 0);
32974+
return res;
32975+
}
32976+
32977+
StringBuilder track = new StringBuilder();
32978+
List<String> res = new LinkedList<>();
32979+
32980+
void backtrack(String s, int index) {
32981+
if (index == s.length()) {
32982+
res.add(track.toString());
32983+
return;
32984+
}
32985+
32986+
if ('0' <= s.charAt(index) && s.charAt(index) <= '9') {
32987+
// s[index] 是数字
32988+
// 做选择
32989+
track.append(s.charAt(index));
32990+
backtrack(s, index + 1);
32991+
// 撤销选择
32992+
track.deleteCharAt(track.length() - 1);
32993+
} else {
32994+
// s[index] 是字母
32995+
32996+
// 小写字母,做选择
32997+
track.append(Character.toLowerCase(s.charAt(index)));
32998+
backtrack(s, index + 1);
32999+
// 撤销选择
33000+
track.deleteCharAt(track.length() - 1);
33001+
33002+
// 大写字母,做选择
33003+
track.append(Character.toUpperCase(s.charAt(index)));
33004+
backtrack(s, index + 1);
33005+
// 撤销选择
33006+
track.deleteCharAt(track.length() - 1);
33007+
}
33008+
}
33009+
}
33010+
```
33011+
33012+
https://leetcode.cn/problems/letter-case-permutation 的多语言解法👆
33013+
3296833014
https://leetcode.cn/problems/letter-combinations-of-a-phone-number 的多语言解法👇
3296933015

3297033016
```cpp
@@ -53608,7 +53654,7 @@ func deleteDuplicates(head *ListNode) *ListNode {
5360853654
```java
5360953655
// by labuladong (java)
5361053656
class Solution {
53611-
public ListNode deleteDuplicates(ListNode head) {
53657+
public deleteDuplicates(ListNode head) {
5361253658
if (head == null) return null;
5361353659
ListNode slow = head, fast = head;
5361453660
while (fast != null) {

‎算法思维系列/回溯算法详解修订版.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ def backtrack(...):
445445
| [698. Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/?show=1) | [698. 划分为k个相等的子集](https://leetcode.cn/problems/partition-to-k-equal-sum-subsets/?show=1) |
446446
| [77. Combinations](https://leetcode.com/problems/combinations/?show=1) | [77. 组合](https://leetcode.cn/problems/combinations/?show=1) |
447447
| [78. Subsets](https://leetcode.com/problems/subsets/?show=1) | [78. 子集](https://leetcode.cn/problems/subsets/?show=1) |
448+
| [784. Letter Case Permutation](https://leetcode.com/problems/letter-case-permutation/?show=1) | [784. 字母大小写全排列](https://leetcode.cn/problems/letter-case-permutation/?show=1) |
448449
| [93. Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/?show=1) | [93. 复原 IP 地址](https://leetcode.cn/problems/restore-ip-addresses/?show=1) |
449450
| - | [剑指 Offer 34. 二叉树中和为某一值的路径](https://leetcode.cn/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/?show=1) |
450451
| - | [剑指 Offer II 079. 所有子集](https://leetcode.cn/problems/TVdhkn/?show=1) |

0 commit comments

Comments
(0)

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