Skip to content

Commit a5a28fa

Browse files
committed
20190115
1 parent ad113df commit a5a28fa

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

Diff for: code/lc240.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package code;
2+
/*
3+
* 240. Search a 2D Matrix II
4+
* 题意:有序矩阵中搜索值
5+
* 难度:Medium
6+
* 分类:Binary Search, Divide and Conquer
7+
* 思路:两种方法,一种O(mlg(n)),遍历每一行,每行二分查找。另一种O(m+n),从右上角开始移动
8+
* Tips:
9+
*/
10+
public class lc240 {
11+
public boolean searchMatrix(int[][] matrix, int target) {
12+
if(matrix.length==0)
13+
return false;
14+
int i = 0;
15+
int j = matrix[0].length;
16+
while( i<matrix.length || j>=0 ){
17+
if(matrix[i][j]==target)
18+
return true;
19+
else if(matrix[i][j]>target)
20+
j--;
21+
else
22+
i++;
23+
}
24+
return false;
25+
}
26+
}

Diff for: code/lc279.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package code;
2+
/*
3+
* 279. Perfect Squares
4+
* 题意:给定一个数,求该数最少可以由几个平方数的和组成
5+
* 难度:Medium
6+
* 分类:Math, Dynamic Programming, Breadth-first Search
7+
* 思路:dp[i] = dp[i-j*j] +1
8+
* Tips:
9+
*/
10+
import java.util.Arrays;
11+
12+
public class lc279 {
13+
public static void main(String[] args) {
14+
System.out.println(numSquares(12));
15+
}
16+
public static int numSquares(int n) {
17+
int[] dp = new int[n];
18+
Arrays.fill(dp,Integer.MAX_VALUE);
19+
for (int i = 1; i <= n ; i++) {
20+
for (int j=1; j<=i ; j++) {
21+
if(j*j==i)
22+
dp[i-1] = 1;
23+
if(j*j<i){
24+
dp[i-1] = Math.min(dp[i-1-j*j]+1,dp[i-1]);
25+
}
26+
}
27+
}
28+
return dp[n-1];
29+
}
30+
}

Diff for: code/lc283.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package code;
2+
/*
3+
* 283. Move Zeroes
4+
* 题意:把非0元素移到数组前边,相对位置不变
5+
* 难度:Easy
6+
* 分类:Array, Two Pointers
7+
* 思路:遍历一遍数组,如果这个数非0,就合前边的数字交换
8+
* Tips:
9+
*/
10+
public class lc283 {
11+
public void moveZeroes(int[] nums) {
12+
for (int i = 0, j=0; i < nums.length ; i++) {
13+
if(nums[i]!=0){
14+
int temp = nums[j];
15+
nums[j] = nums[i];
16+
nums[i] = temp;
17+
j++;
18+
}
19+
}
20+
}
21+
}

Diff for: code/lc312.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package code;
2+
/*
3+
* 312. Burst Balloons
4+
* 题意:踩气球,求怎样踩,值最大
5+
* 难度:Hard
6+
* 分类:Divide and Conquer, Dynamic Programming
7+
* 思路:假设第n个气球是最后一个被踩爆,则从第n个气球开始,数组可以分为无前后相关性的两块
8+
* Tips:太难了,弃疗了,不会写。直接粘答案。
9+
*/
10+
public class lc312 {
11+
public static void main(String[] args) {
12+
System.out.println(maxCoins(new int[]{3,1,5,8}));
13+
}
14+
public static int maxCoins(int[] iNums) {
15+
int[] nums = new int[iNums.length + 2];
16+
int n = 1;
17+
for (int x : iNums) if (x > 0) nums[n++] = x;
18+
nums[0] = nums[n++] = 1;
19+
20+
21+
int[][] memo = new int[n][n];
22+
int res = burst(memo, nums, 0, n - 1);
23+
return res;
24+
}
25+
26+
public static int burst(int[][] memo, int[] nums, int left, int right) {
27+
if (left + 1 == right) return 0;
28+
if (memo[left][right] > 0) return memo[left][right];
29+
int ans = 0;
30+
for (int i = left + 1; i < right; ++i)
31+
ans = Math.max(ans, nums[left] * nums[i] * nums[right]
32+
+ burst(memo, nums, left, i) + burst(memo, nums, i, right));
33+
memo[left][right] = ans;
34+
return ans;
35+
}
36+
37+
}

0 commit comments

Comments
 (0)