File tree 4 files changed +75
-2
lines changed
4 files changed +75
-2
lines changed Original file line number Diff line number Diff line change 1
1
package code ;
2
-
2
+ /*
3
+ * 118. Pascal's Triangle
4
+ * 题意:上层元素相邻的和,两边补1,得到下层元素
5
+ * 难度:Easy
6
+ * 分类:Array
7
+ * 思路:迭代计算
8
+ * Tips:
9
+ */
10
+ import java .util .ArrayList ;
3
11
import java .util .List ;
4
12
5
13
public class lc118 {
6
14
public List <List <Integer >> generate (int numRows ) {
15
+ List <List <Integer >> res = new ArrayList <>();
16
+ if (numRows ==0 ) return res ;
17
+ List <Integer > ls = new ArrayList ();
18
+ ls .add (1 ); //第一层1个1
19
+ res .add (ls );
20
+ numRows --;
21
+ if (numRows ==0 ) return res ;
7
22
while (numRows >0 ){
8
-
23
+ ArrayList <Integer > temp = new ArrayList ();
24
+ temp .add (1 );
25
+ for (int i = 0 ; i < ls .size ()-1 ; i ++) {
26
+ temp .add (ls .get (i )+ls .get (i +1 ));
27
+ }
28
+ temp .add (1 );
29
+ res .add (temp );
30
+ ls = temp ;
31
+ numRows --;
9
32
}
33
+ return res ;
10
34
}
11
35
}
Original file line number Diff line number Diff line change
1
+ package code ;
2
+ /*
3
+ * 125. Valid Palindrome
4
+ * 题意:判断是否为回文
5
+ * 难度:Easy
6
+ * 分类:Two Pointers, String
7
+ * 思路:两个指针。另一种是正则表达式替换数字,字母为空格,再反转,判断是否相等。
8
+ * Tips:记下另一种方法。第一种方法Bingo!
9
+ * lc234, lc5
10
+ */
11
+ public class lc125 {
12
+ public static void main (String [] args ) {
13
+ System .out .println (isPalindrome ("A man, a plan, a canal: Panama" ));
14
+ }
15
+ public static boolean isPalindrome (String s ) {
16
+ int begin = 0 ;
17
+ int end = s .length ()-1 ;
18
+ s = s .toLowerCase (); //转为小写
19
+ while (begin <end && !(helper (s .charAt (begin )))) begin ++;
20
+ while (begin <end && !(helper (s .charAt (end )))) end --;
21
+ while (begin <end ){
22
+ if (s .charAt (begin )==s .charAt (end )){
23
+ begin ++;
24
+ end --;
25
+ while (begin <end && !(helper (s .charAt (begin )))) begin ++;
26
+ while (begin <end && !(helper (s .charAt (end )))) end --;
27
+ }else {
28
+ return false ;
29
+ }
30
+ }
31
+ return true ;
32
+ }
33
+
34
+ public static boolean helper (Character ch ){ //过滤非字符数字
35
+ if (Character .isAlphabetic (ch )||Character .isDigit (ch )) return true ;
36
+ return false ;
37
+ }
38
+
39
+ public boolean isPalindrome2 (String s ) {
40
+ String actual = s .replaceAll ("[^A-Za-z0-9]" , "" ).toLowerCase ();
41
+ String rev = new StringBuffer (actual ).reverse ().toString ();
42
+ return actual .equals (rev );
43
+ }
44
+ }
Original file line number Diff line number Diff line change 5
5
* 难度:Medium
6
6
* 分类:String, Dynamic Programming
7
7
* Tips:从后往前遍历,保证后续dp时,子情况已计算出
8
+ * 还有一种思路是从中间往两边扩展,中间有两种情况,一种一个字符,一种两个字符
8
9
*/
9
10
public class lc5 {
10
11
public static void main (String [] args ) {
Original file line number Diff line number Diff line change @@ -75,13 +75,17 @@ Language: Java
75
75
| 098 [ Java] ( ./code/lc98.java )
76
76
| 101 [ Java] ( ./code/lc101.java )
77
77
| 102 [ Java] ( ./code/lc102.java )
78
+ | 103 [ Java] ( ./code/lc103.java )
78
79
| 104 [ Java] ( ./code/lc104.java )
79
80
| 105 [ Java] ( ./code/lc105.java )
80
81
| 108 [ Java] ( ./code/lc108.java )
81
82
| 114 [ Java] ( ./code/lc114.java )
83
+ | 114 [ Java] ( ./code/lc116.java )
84
+ | 114 [ Java] ( ./code/lc118.java )
82
85
| 121 [ Java] ( ./code/lc121.java )
83
86
| 122 [ Java] ( ./code/lc122.java )
84
87
| 124 [ Java] ( ./code/lc124.java )
88
+ | 125 [ Java] ( ./code/lc125.java )
85
89
| 128 [ Java] ( ./code/lc128.java )
86
90
| 136 [ Java] ( ./code/lc136.java )
87
91
| 139 [ Java] ( ./code/lc139.java )
You can’t perform that action at this time.
0 commit comments