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 bc4507a

Browse files
kkty39labuladong
authored andcommitted
fix code: java 22
1 parent 3436f2e commit bc4507a

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26875,40 +26875,40 @@ func backtrack(left int, right int, track *string, res *[]string) {
2687526875
```java
2687626876
// by labuladong (java)
2687726877
class Solution {
26878-
public:
26879-
vector<string> generateParenthesis(int n) {
26880-
if (n == 0) return {};
26878+
26879+
public List<String> generateParenthesis(int n) {
26880+
if (n == 0) return new ArrayList<>();
2688126881
// 记录所有合法的括号组合
26882-
vector<string> res;
26882+
List<String> res = new ArrayList<>();
2688326883
// 回溯过程中的路径
26884-
string track;
26884+
StringBuilder track = new StringBuilder();
2688526885
// 可用的左括号和右括号数量初始化为 n
2688626886
backtrack(n, n, track, res);
2688726887
return res;
2688826888
}
2688926889

2689026890
// 可用的左括号数量为 left 个,可用的右括号数量为 rgiht 个
2689126891
void backtrack(int left, int right,
26892-
string& track, vector<string>& res) {
26892+
StringBuilder track, List<String> res) {
2689326893
// 若左括号剩下的多,说明不合法
2689426894
if (right < left) return;
2689526895
// 数量小于 0 肯定是不合法的
2689626896
if (left < 0 || right < 0) return;
2689726897
// 当所有括号都恰好用完时,得到一个合法的括号组合
2689826898
if (left == 0 && right == 0) {
26899-
res.push_back(track);
26899+
res.add(track.toString());
2690026900
return;
2690126901
}
2690226902

2690326903
// 尝试放一个左括号
26904-
track.push_back('('); // 选择
26904+
track.append('('); // 选择
2690526905
backtrack(left - 1, right, track, res);
26906-
track.pop_back(); // 撤消选择
26906+
track.deleteCharAt(track.length() - 1); // 撤消选择
2690726907

2690826908
// 尝试放一个右括号
26909-
track.push_back(')'); // 选择
26909+
track.append(')'); // 选择
2691026910
backtrack(left, right - 1, track, res);
26911-
track.pop_back(); // 撤消选择
26911+
track.deleteCharAt(track.length() - 1); // 撤消选择
2691226912
}
2691326913
}
2691426914
```

0 commit comments

Comments
(0)

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