Skip to content

Commit 3285504

Browse files
author
programmerhjh
committed
日常水题(腾讯秋招50道)
1 parent 9fbd628 commit 3285504

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/myleetcode/PowerOfTwo.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package myleetcode;
2+
3+
/**
4+
* 2的幂
5+
* @author acer
6+
*
7+
*/
8+
public class PowerOfTwo {
9+
10+
public static boolean isPowerOfTwo(int n) {
11+
if ((n & n-1) == 0) {
12+
return true;
13+
} else {
14+
return false;
15+
}
16+
}
17+
18+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package myleetcode;
2+
3+
/**
4+
* 有效的括号
5+
* @author acer
6+
*
7+
*/
8+
public class ValidParentheses {
9+
10+
public static boolean isValid(String s) {
11+
s = s.trim();
12+
if (s.length() == 0) {
13+
return true;
14+
}
15+
if (s.length() % 2 == 1) {
16+
return false;
17+
}
18+
StringBuffer buffer = new StringBuffer(s);
19+
while (buffer.length() != 0) {
20+
int tempLen = buffer.length();
21+
for (int i = 0; i < buffer.length()-1; ) {
22+
if (
23+
((int) buffer.charAt(i) == 40 && (int) buffer.charAt(i+1) == 41)
24+
|| ((int) buffer.charAt(i) == 91 && (int) buffer.charAt(i+1) == 93)
25+
|| ((int) buffer.charAt(i) == 123 && (int) buffer.charAt(i+1) == 125)
26+
) {
27+
buffer.delete(i, i+2);
28+
break;
29+
} else {
30+
i++;
31+
}
32+
}
33+
if (buffer.length() == tempLen && tempLen != 0) {
34+
return false;
35+
}
36+
}
37+
return true;
38+
}
39+
40+
}

0 commit comments

Comments
 (0)