File tree 10 files changed +99
-5
lines changed
10 files changed +99
-5
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ public static void main(String[] args) {
13
13
14
14
}
15
15
16
- public static String longestCommonPrefix (String [] strs ) {
16
+ public static String longestCommonPrefix (String [] strs ) { //不是最优的方法,多做了比较
17
17
if (strs .length ==0 )
18
18
return "" ;
19
19
Arrays .sort (strs );
Original file line number Diff line number Diff line change @@ -36,4 +36,19 @@ public static boolean isValid(String s) {
36
36
return true ;
37
37
return false ;
38
38
}
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
+ }
39
54
}
Original file line number Diff line number Diff line change 11
11
* 分类:Linked List, Divide and Conquer, Heap
12
12
* 思路:优先队列或分治方法
13
13
* 注意:优先队列如何定义比较方法
14
+ * lc378, lc264
14
15
*/
15
16
public class lc23 {
16
17
public class ListNode {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ public static void main(String[] args) {
16
16
public static int numSquares (int n ) {
17
17
int [] dp = new int [n ];
18
18
Arrays .fill (dp ,Integer .MAX_VALUE );
19
- for (int i = 1 ; i <= n ; i ++) {
19
+ for (int i = 1 ; i <= n ; i ++) { //两个for循环
20
20
for (int j =1 ; j <=i ; j ++) {
21
21
if (j *j ==i )
22
22
dp [i -1 ] = 1 ;
Original file line number Diff line number Diff line change 5
5
* 难度:Hard
6
6
* 分类:Array, Binary Search, Divide and Conquer
7
7
* 注意:两个数组长度可能不一样;边际问题
8
+ * 复杂度是 O(log(min(m,n)) ,在短的数组上二分查找即可
8
9
*/
9
10
public class lc4 {
10
11
public static void main (String [] args ) {
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ public static void main(String[] args) {
16
16
int [] nums = {1 , 2 , 5 };
17
17
System .out .println (canPartition (nums ));
18
18
}
19
- public static boolean canPartition (int [] nums ) {
19
+ public static boolean canPartition (int [] nums ) { //自己写的不知道什么,回头看已经看不懂自己的代码了。。。
20
20
int sum = 0 ;
21
21
for (int i : nums ) {
22
22
sum +=i ;
@@ -39,4 +39,31 @@ public static boolean canPartition(int[] nums) {
39
39
}
40
40
return false ;
41
41
}
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
+ }
42
69
}
Original file line number Diff line number Diff line change 6
6
* 分类:Math, Dynamic Programming, Minimax
7
7
* 思路:之前做过有印象,先拿的人一定赢的。
8
8
* dp的思路需要借鉴一下的, dp[i][j] 表示数组 i~j 的最优解
9
- * 向两边拓展的dp, 用一个变量表示 size 这个dp。想想一下三角形的区域是怎么一步步被填满的
9
+ * 向两边拓展的dp, 用一个变量表示 size 这个dp。想想一下三角形的区域是怎么一步步被填满的,斜线斜线的,最后是一个点
10
10
* 拿了piles[i], 则dp[i+1][j]就被另一个人拿了,结果是 piles[i] - dp[i + 1][j]
11
11
* 拿了piles[j], 则dp[i][j-1]就被另一个人拿了,结果是 piles[j] - dp[i][j - 1]
12
12
* Tips:
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ LeetCode 指南
5
5
- 说明: 每道题在代码头部都添加了我的解题思路和批注,Eg:
6
6
7
7
8
- /*
8
+ /*****
9
9
* 287. Find the Duplicate Number
10
10
* 题意:n+1个数属于[1~n],找出重复的那个数
11
11
* 难度:Medium
@@ -149,6 +149,8 @@ LeetCode 指南
149
149
| 239 [ Java] ( ./code/lc239.java )
150
150
| 240 [ Java] ( ./code/lc240.java )
151
151
| 242 [ Java] ( ./code/lc242.java )
152
+ | 263 [ Java] ( ./code/lc263.java )
153
+ | 264 [ Java] ( ./code/lc264.java )
152
154
| 268 [ Java] ( ./code/lc268.java )
153
155
| 279 [ Java] ( ./code/lc279.java )
154
156
| 283 [ Java] ( ./code/lc283.java )
You can’t perform that action at this time.
0 commit comments