Skip to content

Commit b687629

Browse files
committed
20190130
1 parent f7fb3a4 commit b687629

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

code/lc118.java

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
package code;
2-
2+
/*
3+
* 118. Pascal's Triangle
4+
* 题意:上层元素相邻的和,两边补1,得到下层元素
5+
* 难度:Easy
6+
* 分类:Array
7+
* 思路:迭代计算
8+
* Tips:
9+
*/
10+
import java.util.ArrayList;
311
import java.util.List;
412

513
public class lc118 {
614
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;
722
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--;
932
}
33+
return res;
1034
}
1135
}

code/lc125.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

code/lc5.java

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* 难度:Medium
66
* 分类:String, Dynamic Programming
77
* Tips:从后往前遍历,保证后续dp时,子情况已计算出
8+
* 还有一种思路是从中间往两边扩展,中间有两种情况,一种一个字符,一种两个字符
89
*/
910
public class lc5 {
1011
public static void main(String[] args) {

readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,17 @@ Language: Java
7575
| 098 [Java](./code/lc98.java)
7676
| 101 [Java](./code/lc101.java)
7777
| 102 [Java](./code/lc102.java)
78+
| 103 [Java](./code/lc103.java)
7879
| 104 [Java](./code/lc104.java)
7980
| 105 [Java](./code/lc105.java)
8081
| 108 [Java](./code/lc108.java)
8182
| 114 [Java](./code/lc114.java)
83+
| 114 [Java](./code/lc116.java)
84+
| 114 [Java](./code/lc118.java)
8285
| 121 [Java](./code/lc121.java)
8386
| 122 [Java](./code/lc122.java)
8487
| 124 [Java](./code/lc124.java)
88+
| 125 [Java](./code/lc125.java)
8589
| 128 [Java](./code/lc128.java)
8690
| 136 [Java](./code/lc136.java)
8791
| 139 [Java](./code/lc139.java)

0 commit comments

Comments
 (0)