Skip to content

Commit 03b3d3d

Browse files
committed
20181226
1 parent 1bb9af4 commit 03b3d3d

File tree

8 files changed

+246
-3
lines changed

8 files changed

+246
-3
lines changed

code/lc32.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.Stack;
44

55
/*
6-
* 31. Longest Valid Parentheses
6+
* 32. Longest Valid Parentheses
77
* 题意:最长有效子串
88
* 难度:Hard
99
* 分类:Dynamic Programming, String

code/lc33.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
/*
44
* 31. Search in Rotated Sorted Array
5-
* 题意:Longest Valid Parentheses
6-
* 难度:Hard
5+
* 题意:在翻转有序数组中查找指定数
6+
* 难度:Medium
77
* 分类:Array, Binary Search
88
* 思路:二分查找的思路,多了一步判断,判断哪部分有序,是否在这部分中
99
* Tips:注意边界判断,是否有等号

code/lc34.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package code;
2+
/*
3+
* 31. Find First and Last Position of Element in Sorted Array
4+
* 题意:在有序数组中寻找某个数的起始和终止位置
5+
* 难度:Medium
6+
* 分类:Array, Binary Search
7+
* 思路:两次二分查找,第一次找到起始位置,第二次找终止位置
8+
* Tips:注意/2时是否+1
9+
*/
10+
public class lc34 {
11+
public static void main(String[] args) {
12+
int[] nums = {5,7,7,8,8,10};
13+
int[] res = searchRange(nums,8);
14+
System.out.println(res[0]);
15+
System.out.println(res[1]);
16+
}
17+
18+
public static int[] searchRange(int[] nums, int target) {
19+
int[] res = {-1,-1};
20+
if(nums.length==0)
21+
return res;
22+
int start = 0;
23+
int end = nums.length-1;
24+
while(start<end){
25+
int mid = (start+end)/2;
26+
if(nums[mid]<target){
27+
start = mid + 1;
28+
}else{ //当==时,起始位置在左边
29+
end = mid;
30+
}
31+
}
32+
if(nums[start]!=target)
33+
return res;
34+
res[0] = start;
35+
end = nums.length-1; //不需要设置start=0了
36+
while(start<end){
37+
int mid = (start+end)/2+1; //+1使其偏向右边,否则会死循环
38+
if(nums[mid]>target){
39+
end = mid-1;
40+
}else{
41+
start = mid;
42+
}
43+
}
44+
if(nums[start]!=target)
45+
return res;
46+
res[1]=end;
47+
return res;
48+
}
49+
}

code/lc39.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package code;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/*
7+
* 39. Combination Sum
8+
* 题意:找出和为sum的所有组合
9+
* 难度:Medium
10+
* 分类:Array, Backtracking
11+
* 思路:回溯法
12+
* Tips:向res添加答案时注意要new一个新的List,否则后续循环的操作会影响res中的L; 设置一个start标志,记录上次数组循环到哪了,防止重复集合。
13+
* 和lc46做比较,46是排列组合,所以不需要start标志,start标志是为了防止相同元素的组合排列不同而当做了另一种
14+
*/
15+
public class lc39 {
16+
public static void main(String[] args) {
17+
int[] candidates = {2,3,6,7};
18+
int target = 7;
19+
System.out.println(combinationSum(candidates, target).toString());
20+
}
21+
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
22+
List<List<Integer>> res = new ArrayList<List<Integer>>();
23+
if(candidates.length==0||target==0)
24+
return res;
25+
List<Integer> l = new ArrayList<Integer>();
26+
backtracking(res,candidates,target,l,0,0);
27+
return res;
28+
}
29+
30+
public static void backtracking(List<List<Integer>> res, int[] candidates, int target, List<Integer> l, int sum, int start){
31+
for (int i = start; i < candidates.length; i++) {
32+
l.add(candidates[i]);
33+
if(sum+candidates[i]==target) {
34+
res.add(new ArrayList<>(l));//new 新的 List
35+
} else if(sum+candidates[i]<target){
36+
backtracking(res, candidates, target, l, sum+candidates[i],i);
37+
}
38+
l.remove((Integer)candidates[i]);
39+
}
40+
}
41+
}

code/lc42.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package code;
2+
/*
3+
* 42. Trapping Rain Water
4+
* 题意:能盛多少水
5+
* 难度:Hard
6+
* 分类:Array, Two Pointers, Stack
7+
* 思路:三种方法,DP先求出来每个位置的maxleft,maxright,再遍历一遍;两个指针,类似lc11题的思路;用栈数据结构;
8+
* Tips:
9+
*/
10+
public class lc42 {
11+
public static void main(String[] args) {
12+
int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};
13+
System.out.println(trap(height));
14+
}
15+
public static int trap(int[] height) {
16+
if(height.length<3)
17+
return 0;
18+
int left = 0;
19+
int right = height.length-1;
20+
int res =0;
21+
22+
while(left<right){
23+
if(height[left]<height[right]){
24+
int edge_l = height[left];
25+
left++;
26+
while(height[left]<edge_l && left<right){
27+
if(edge_l-height[left]>0)
28+
res += edge_l-height[left];
29+
left++;
30+
}
31+
}else if(height[left]>=height[right] && left<right){
32+
int edge_r = height[right];
33+
right--;
34+
while(height[right]<edge_r){
35+
if(edge_r-height[right]>0)
36+
res += edge_r-height[right];
37+
right--;
38+
}
39+
}
40+
}
41+
return res;
42+
}
43+
}

code/lc46.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package code;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/*
7+
* 46. Permutations
8+
* 题意:全排列
9+
* 难度:Medium
10+
* 分类:Backtracking
11+
* 思路:典型的回溯题,注意判断下相同元素重复添加,和lc39做比较
12+
*/
13+
public class lc46 {
14+
public static void main(String[] args) {
15+
int nums[] = {};
16+
System.out.println(permute(nums).toString());
17+
}
18+
19+
public static List<List<Integer>> permute(int[] nums) {
20+
List<List<Integer>> res = new ArrayList<>();
21+
backtracking(res,nums,new ArrayList());
22+
return res;
23+
}
24+
public static void backtracking(List<List<Integer>> res, int[] nums, List l){
25+
if(l.size()==nums.length){
26+
res.add(new ArrayList<>(l));
27+
return;
28+
}
29+
30+
for (int i = 0; i < nums.length ; i++) {
31+
if(l.contains((Integer)nums[i])) //防止相同的元素再次添加
32+
continue;
33+
l.add(nums[i]);
34+
backtracking(res,nums,l);
35+
l.remove((Integer)nums[i]);
36+
}
37+
}
38+
39+
}

code/lc48.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package code;
2+
/*
3+
* 48. Rotate Image
4+
* 题意:将数组顺时针翻转90度
5+
* 难度:Medium
6+
* 分类:Array
7+
* 思路:两种思路:先对角,再以竖轴对称;先以横轴对称,再对角.思路很新奇,记一下.
8+
*/
9+
public class lc48 {
10+
public static void main(String[] args) {
11+
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
12+
printArr(matrix);
13+
rotate(matrix);
14+
System.out.println();
15+
printArr(matrix);
16+
}
17+
public static void rotate(int[][] matrix) {
18+
for (int i = 0; i <matrix.length ; i++) {
19+
for (int j = 0; j < i; j++) {
20+
int temp = matrix[i][j];
21+
matrix[i][j] = matrix[j][i];
22+
matrix[j][i] = temp;
23+
}
24+
}
25+
for (int i = 0; i < matrix.length; i++) {
26+
for (int j = 0; j <matrix[0].length/2; j++) {
27+
int temp = matrix[i][j];
28+
matrix[i][j] = matrix[i][matrix[0].length-1-j];
29+
matrix[i][matrix[0].length-1-j] = temp;
30+
}
31+
}
32+
}
33+
34+
public static void printArr(int[][] matrix){
35+
for (int i = 0; i < matrix.length; i++) {
36+
for (int j = 0; j < matrix[0].length; j++) {
37+
System.out.print(matrix[i][j]);
38+
}
39+
System.out.println();
40+
}
41+
}
42+
}

code/lc49.java

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package code;
2+
3+
import java.util.*;
4+
5+
/*
6+
* 49. Group Anagrams
7+
* 题意:相同字母组合的字符串分到一个组里
8+
* 难度:Medium
9+
* 分类:Hash Table, String
10+
* Tips:思路很直接,用map即可.注意java和python的不一样,不能用set集合做key,即使set的内容一样,但java类似指针的引用方式,使得set不可能相等.
11+
*/
12+
public class lc49 {
13+
public List<List<String>> groupAnagrams(String[] strs) {
14+
HashMap<String,List<String>> m = new HashMap();
15+
for (int i = 0; i < strs.length ; i++) {
16+
char[] chs = strs[i].toCharArray();
17+
Arrays.sort(chs);
18+
String key = String.valueOf(chs);
19+
if(m.containsKey(key))
20+
m.get(key).add(strs[i]);
21+
else {
22+
ArrayList<String> l = new ArrayList<String>();
23+
l.add(strs[i]);
24+
m.put(key,l);
25+
}
26+
}
27+
return new ArrayList(m.values());
28+
}
29+
}

0 commit comments

Comments
 (0)