File tree Expand file tree Collapse file tree 2 files changed +58
-0
lines changed
Expand file tree Collapse file tree 2 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments