Skip to content

Commit e8d1b35

Browse files
committed
20190308
1 parent b8d2f7e commit e8d1b35

10 files changed

+99
-5
lines changed

code/lc14.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static void main(String[] args) {
1313

1414
}
1515

16-
public static String longestCommonPrefix(String[] strs) {
16+
public static String longestCommonPrefix(String[] strs) { //不是最优的方法,多做了比较
1717
if(strs.length==0)
1818
return "";
1919
Arrays.sort(strs);

code/lc20.java

+15
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,19 @@ public static boolean isValid(String s) {
3636
return true;
3737
return false;
3838
}
39+
40+
public boolean isValid2(String s) {
41+
Stack<Character> stack = new Stack<Character>();
42+
for (char c : s.toCharArray()) {
43+
if (c == '(')
44+
stack.push(')');
45+
else if (c == '{')
46+
stack.push('}');
47+
else if (c == '[')
48+
stack.push(']');
49+
else if (stack.isEmpty() || stack.pop() != c)
50+
return false;
51+
}
52+
return stack.isEmpty();
53+
}
3954
}

code/lc23.java

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* 分类:Linked List, Divide and Conquer, Heap
1212
* 思路:优先队列或分治方法
1313
* 注意:优先队列如何定义比较方法
14+
* lc378, lc264
1415
*/
1516
public class lc23 {
1617
public class ListNode {

code/lc263.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package code;
2+
/*
3+
* 263. Ugly Number
4+
* 题意:因子只包含2,3,5的数称为丑数
5+
* 难度:Easy
6+
* 分类:Math
7+
* 思路:
8+
* Tips:
9+
*/
10+
public class lc263 {
11+
public boolean isUgly(int num) {
12+
if(num==0) return false;
13+
if(num==1) return true;
14+
while(num%2==0) num/=2;
15+
while(num%3==0) num/=3;
16+
while(num%5==0) num/=5;
17+
return num==1;
18+
}
19+
}

code/lc264.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package code;
2+
3+
import java.util.PriorityQueue;
4+
5+
/*
6+
* 264. Ugly Number II
7+
* 题意:因子只包含2,3,5的数称为丑数,求第n个丑数
8+
* 难度:Medium
9+
* 分类:Math, Heap, Dynamic Programming
10+
* 思路:丑数乘以2,3,或5还是丑数
11+
* Tips:注意可能存在重复的数字,和溢出
12+
* lc23, lc378
13+
*/
14+
public class lc264 {
15+
public int nthUglyNumber(int n) {
16+
PriorityQueue<Long> pr = new PriorityQueue<>(); //用Long,防止溢出
17+
pr.add(1L);
18+
long res = 0;
19+
while(n>0){
20+
res = pr.remove();
21+
while(pr.size()>0 &&pr.peek()==res ) pr.remove(); //注意可能存在重复的数字
22+
pr.add(res*2);
23+
pr.add(res*3);
24+
pr.add(res*5);
25+
n--;
26+
}
27+
return (int)res;
28+
}
29+
}

code/lc279.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void main(String[] args) {
1616
public static int numSquares(int n) {
1717
int[] dp = new int[n];
1818
Arrays.fill(dp,Integer.MAX_VALUE);
19-
for (int i = 1; i <= n ; i++) {
19+
for (int i = 1; i <= n ; i++) { //两个for循环
2020
for (int j=1; j<=i ; j++) {
2121
if(j*j==i)
2222
dp[i-1] = 1;

code/lc4.java

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* 难度:Hard
66
* 分类:Array, Binary Search, Divide and Conquer
77
* 注意:两个数组长度可能不一样;边际问题
8+
* 复杂度是 O(log(min(m,n)) ,在短的数组上二分查找即可
89
*/
910
public class lc4 {
1011
public static void main(String[] args) {

code/lc416.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void main(String[] args) {
1616
int[] nums = {1, 2, 5};
1717
System.out.println(canPartition(nums));
1818
}
19-
public static boolean canPartition(int[] nums) {
19+
public static boolean canPartition(int[] nums) { //自己写的不知道什么,回头看已经看不懂自己的代码了。。。
2020
int sum = 0;
2121
for (int i : nums) {
2222
sum+=i;
@@ -39,4 +39,31 @@ public static boolean canPartition(int[] nums) {
3939
}
4040
return false;
4141
}
42+
43+
public boolean canPartition2(int[] nums) {
44+
// check edge case
45+
if (nums == null || nums.length == 0) {
46+
return true;
47+
}
48+
// preprocess
49+
int volumn = 0;
50+
for (int num : nums) {
51+
volumn += num;
52+
}
53+
if (volumn % 2 != 0) {
54+
return false;
55+
}
56+
volumn /= 2;
57+
// dp def
58+
boolean[] dp = new boolean[volumn + 1];
59+
// dp init
60+
dp[0] = true;
61+
// dp transition
62+
for (int i = 1; i <= nums.length; i++) {
63+
for (int j = volumn; j >= nums[i-1]; j--) { //从后往前更新,压缩空间
64+
dp[j] = dp[j] || dp[j - nums[i-1]];
65+
}
66+
}
67+
return dp[volumn];
68+
}
4269
}

code/lc877.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* 分类:Math, Dynamic Programming, Minimax
77
* 思路:之前做过有印象,先拿的人一定赢的。
88
* dp的思路需要借鉴一下的, dp[i][j] 表示数组 i~j 的最优解
9-
* 向两边拓展的dp, 用一个变量表示 size 这个dp。想想一下三角形的区域是怎么一步步被填满的
9+
* 向两边拓展的dp, 用一个变量表示 size 这个dp。想想一下三角形的区域是怎么一步步被填满的,斜线斜线的,最后是一个点
1010
* 拿了piles[i], 则dp[i+1][j]就被另一个人拿了,结果是 piles[i] - dp[i + 1][j]
1111
* 拿了piles[j], 则dp[i][j-1]就被另一个人拿了,结果是 piles[j] - dp[i][j - 1]
1212
* Tips:

readme.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LeetCode 指南
55
- 说明: 每道题在代码头部都添加了我的解题思路和批注,Eg:
66

77

8-
/*
8+
/*****
99
* 287. Find the Duplicate Number
1010
* 题意:n+1个数属于[1~n],找出重复的那个数
1111
* 难度:Medium
@@ -149,6 +149,8 @@ LeetCode 指南
149149
| 239 [Java](./code/lc239.java)
150150
| 240 [Java](./code/lc240.java)
151151
| 242 [Java](./code/lc242.java)
152+
| 263 [Java](./code/lc263.java)
153+
| 264 [Java](./code/lc264.java)
152154
| 268 [Java](./code/lc268.java)
153155
| 279 [Java](./code/lc279.java)
154156
| 283 [Java](./code/lc283.java)

0 commit comments

Comments
 (0)