提交 03b3d3d4 编写于 作者: L liu13

20181226

上级 1bb9af43
......@@ -3,7 +3,7 @@ package code;
import java.util.Stack;
/*
* 31. Longest Valid Parentheses
* 32. Longest Valid Parentheses
* 题意:最长有效子串
* 难度:Hard
* 分类:Dynamic Programming, String
......
......@@ -2,8 +2,8 @@ package code;
/*
* 31. Search in Rotated Sorted Array
* 题意:Longest Valid Parentheses
* 难度:Hard
* 题意:在翻转有序数组中查找指定数
* 难度:Medium
* 分类:Array, Binary Search
* 思路:二分查找的思路,多了一步判断,判断哪部分有序,是否在这部分中
* Tips:注意边界判断,是否有等号
......
package code;
/*
* 31. Find First and Last Position of Element in Sorted Array
* 题意:在有序数组中寻找某个数的起始和终止位置
* 难度:Medium
* 分类:Array, Binary Search
* 思路:两次二分查找,第一次找到起始位置,第二次找终止位置
* Tips:注意/2时是否+1
*/
public class lc34 {
public static void main(String[] args) {
int[] nums = {5,7,7,8,8,10};
int[] res = searchRange(nums,8);
System.out.println(res[0]);
System.out.println(res[1]);
}
public static int[] searchRange(int[] nums, int target) {
int[] res = {-1,-1};
if(nums.length==0)
return res;
int start = 0;
int end = nums.length-1;
while(start<end){
int mid = (start+end)/2;
if(nums[mid]<target){
start = mid + 1;
}else{ //当==时,起始位置在左边
end = mid;
}
}
if(nums[start]!=target)
return res;
res[0] = start;
end = nums.length-1; //不需要设置start=0了
while(start<end){
int mid = (start+end)/2+1; //+1使其偏向右边,否则会死循环
if(nums[mid]>target){
end = mid-1;
}else{
start = mid;
}
}
if(nums[start]!=target)
return res;
res[1]=end;
return res;
}
}
package code;
import java.util.ArrayList;
import java.util.List;
/*
* 39. Combination Sum
* 题意:找出和为sum的所有组合
* 难度:Medium
* 分类:Array, Backtracking
* 思路:回溯法
* Tips:向res添加答案时注意要new一个新的List,否则后续循环的操作会影响res中的L; 设置一个start标志,记录上次数组循环到哪了,防止重复集合。
* 和lc46做比较,46是排列组合,所以不需要start标志,start标志是为了防止相同元素的组合排列不同而当做了另一种
*/
public class lc39 {
public static void main(String[] args) {
int[] candidates = {2,3,6,7};
int target = 7;
System.out.println(combinationSum(candidates, target).toString());
}
public static List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(candidates.length==0||target==0)
return res;
List<Integer> l = new ArrayList<Integer>();
backtracking(res,candidates,target,l,0,0);
return res;
}
public static void backtracking(List<List<Integer>> res, int[] candidates, int target, List<Integer> l, int sum, int start){
for (int i = start; i < candidates.length; i++) {
l.add(candidates[i]);
if(sum+candidates[i]==target) {
res.add(new ArrayList<>(l));//new 新的 List
} else if(sum+candidates[i]<target){
backtracking(res, candidates, target, l, sum+candidates[i],i);
}
l.remove((Integer)candidates[i]);
}
}
}
package code;
/*
* 42. Trapping Rain Water
* 题意:能盛多少水
* 难度:Hard
* 分类:Array, Two Pointers, Stack
* 思路:三种方法,DP先求出来每个位置的maxleft,maxright,再遍历一遍;两个指针,类似lc11题的思路;用栈数据结构;
* Tips:
*/
public class lc42 {
public static void main(String[] args) {
int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};
System.out.println(trap(height));
}
public static int trap(int[] height) {
if(height.length<3)
return 0;
int left = 0;
int right = height.length-1;
int res =0;
while(left<right){
if(height[left]<height[right]){
int edge_l = height[left];
left++;
while(height[left]<edge_l && left<right){
if(edge_l-height[left]>0)
res += edge_l-height[left];
left++;
}
}else if(height[left]>=height[right] && left<right){
int edge_r = height[right];
right--;
while(height[right]<edge_r){
if(edge_r-height[right]>0)
res += edge_r-height[right];
right--;
}
}
}
return res;
}
}
package code;
import java.util.ArrayList;
import java.util.List;
/*
* 46. Permutations
* 题意:全排列
* 难度:Medium
* 分类:Backtracking
* 思路:典型的回溯题,注意判断下相同元素重复添加,和lc39做比较
*/
public class lc46 {
public static void main(String[] args) {
int nums[] = {};
System.out.println(permute(nums).toString());
}
public static List<List<Integer>> permute(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
backtracking(res,nums,new ArrayList());
return res;
}
public static void backtracking(List<List<Integer>> res, int[] nums, List l){
if(l.size()==nums.length){
res.add(new ArrayList<>(l));
return;
}
for (int i = 0; i < nums.length ; i++) {
if(l.contains((Integer)nums[i])) //防止相同的元素再次添加
continue;
l.add(nums[i]);
backtracking(res,nums,l);
l.remove((Integer)nums[i]);
}
}
}
package code;
/*
* 48. Rotate Image
* 题意:将数组顺时针翻转90度
* 难度:Medium
* 分类:Array
* 思路:两种思路:先对角,再以竖轴对称;先以横轴对称,再对角.思路很新奇,记一下.
*/
public class lc48 {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printArr(matrix);
rotate(matrix);
System.out.println();
printArr(matrix);
}
public static void rotate(int[][] matrix) {
for (int i = 0; i <matrix.length ; i++) {
for (int j = 0; j < i; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j <matrix[0].length/2; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[i][matrix[0].length-1-j];
matrix[i][matrix[0].length-1-j] = temp;
}
}
}
public static void printArr(int[][] matrix){
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j]);
}
System.out.println();
}
}
}
package code;
import java.util.*;
/*
* 49. Group Anagrams
* 题意:相同字母组合的字符串分到一个组里
* 难度:Medium
* 分类:Hash Table, String
* Tips:思路很直接,用map即可.注意java和python的不一样,不能用set集合做key,即使set的内容一样,但java类似指针的引用方式,使得set不可能相等.
*/
public class lc49 {
public List<List<String>> groupAnagrams(String[] strs) {
HashMap<String,List<String>> m = new HashMap();
for (int i = 0; i < strs.length ; i++) {
char[] chs = strs[i].toCharArray();
Arrays.sort(chs);
String key = String.valueOf(chs);
if(m.containsKey(key))
m.get(key).add(strs[i]);
else {
ArrayList<String> l = new ArrayList<String>();
l.add(strs[i]);
m.put(key,l);
}
}
return new ArrayList(m.values());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册