Skip to content

Commit 6fa3657

Browse files
committed
新增1题,累积37题
1 parent 3ea7420 commit 6fa3657

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.cjl.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/*
7+
22. 括号生成
8+
问题描述:
9+
数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 有效的 括号组合。
10+
示例 1:
11+
输入:n = 3
12+
输出:["((()))","(()())","(())()","()(())","()()()"]
13+
示例 2:
14+
输入:n = 1
15+
输出:["()"]
16+
提示:
17+
1 <= n <= 8
18+
*/
19+
public class Question_22 {
20+
21+
// 时间复杂度是O(N),空间复杂度是O(N)
22+
public List<String> solution1(int n) {
23+
List<String> res = new ArrayList<>();
24+
recur(res, "", 0, 0, n);
25+
return res;
26+
}
27+
28+
/**
29+
*
30+
* @param res 返回结果
31+
* @param str 括号组合字符串
32+
* @param leftNum 左括号数量
33+
* @param rightNum 右括号数量
34+
* @param n 总对数
35+
*/
36+
private void recur(List<String> res, String str, int leftNum, int rightNum, int n) {
37+
if (leftNum > n || rightNum > n) {
38+
return;
39+
}
40+
if (leftNum == n && rightNum == n) {
41+
res.add(str);
42+
}
43+
if (leftNum >= rightNum) {
44+
recur(res, str + "(", leftNum + 1, rightNum, n);
45+
recur(res, str + ")", leftNum, rightNum + 1, n);
46+
}
47+
}
48+
}

src/com/cjl/leetcode/Question_32.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.cjl.leetcode;
22

3-
import com.sun.jmx.remote.internal.ArrayQueue;
43

54
import java.util.*;
65

src/com/cjl/leetcode/Question_7.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,22 @@ public static int solution1(int x){
4141
}
4242
return flag ? res: -res;
4343
}
44+
45+
public static int solution2(int x) {
46+
if (x == Integer.MIN_VALUE) {
47+
return 0;
48+
}
49+
int res = 0;
50+
int temp = Math.abs(x);
51+
while (temp != 0) {
52+
int num = temp % 10;
53+
if (res > (Integer.MAX_VALUE - num) / 10) {
54+
return 0;
55+
} else {
56+
res = res * 10 + num;
57+
temp = temp / 10;
58+
}
59+
}
60+
return (x > 0)? res : -res;
61+
}
4462
}

0 commit comments

Comments
 (0)