[leetCode刷题笔记]2017.02.07
来源:互联网 发布:淘宝子账号认证失败 编辑:程序博客网 时间:2024/05/16 07:14
34. Search for a Range
这道题完全可以用递归做。主要还是二分法思想,寻找等于target的最靠边的一个位置。。。
public class Solution {
public int[] searchRange(int[] nums, int target) {int[] result = new int[2];
result[0] = getBoundRange(nums, 0, nums.length-1, target, true);
result[1] = getBoundRange(nums, 0, nums.length-1, target, false);
return result;
}
private int getBoundRange(int[] nums, int first, int last, int target, boolean left) {
if (first > last) {
return -1;
}
int mid = (last + first) / 2;
if (nums[mid] == target) {
if (left) {
if (mid == 0 || nums[mid - 1] < target) {
return mid;
}
else {
return getBoundRange(nums, first, mid - 1, target, left);
}
}
else {
if (mid == nums.length - 1 || nums[mid + 1] > target) {
return mid;
}
else {
return getBoundRange(nums, mid + 1, last, target, left);
}
}
}
else if (nums[mid] > target) {
return getBoundRange(nums, first, mid - 1, target, left);
}else {
return getBoundRange(nums, mid + 1, last, target, left);
}
}
}
35. Search Insert Position
这个简单,之间用循环弄出来。target大于mid, first变成mid+1,target小于mid,last变成mid -1.
public class Solution {
public int searchInsert(int[] nums, int target) {
int first = 0;
int last = nums.length - 1;
if (nums[first] == target) {
return first;
}
if (nums[last] == target) {
return last;
}
while (first <= last) {
int mid = (first + last) / 2;
if (nums[mid] > target) {
last = mid - 1;
}
else if (nums[mid] < target){
first = mid + 1;
}
else {
return mid;
}
}
return first;
}
}
39. Combination Sum
这道题用递归做比较方便。http://www.1point3acres.com/bbs/thread-108008-1-1.html里面的解法不错。。。
每次都减去数组中的一个数,如果得到的值不为零,则要进入递归,如果为零,那么就退出递归返回list
40. Combination Sum II
也是用递归,这个和前面差不多,但是由于不能重复,所以要从i+1开始递归
public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> item = new ArrayList<Integer>();
if (candidates == null || candidates.length == 0 || target <= 0) {
return result;
}
// sort
Arrays.sort(candidates);
combineHelper(result, item, candidates, target, 0, 0);
return result;
}
public void combineHelper(List<List<Integer>> list, List<Integer> item, int[] candidates, int target, int sum, int start){
// when equal to target push to list
if (sum==target && !list.contains(item)) {
list.add(new ArrayList<Integer>(item));
return ;
}
if (sum>target) {
// larger then target drop
return ;
}
for (int i = start; i < candidates.length; i++) {
sum += candidates[i];
item.add(candidates[i]);
combineHelper(list, item, candidates, target, sum, i+1);
// delete, if not, it will duplicate
sum -= candidates[i];
item.remove(item.size()-1);
}
}
}
- [leetCode刷题笔记]2017.02.07
- Leetcode刷题笔记
- LeetCode刷题笔记
- LeetCode刷题笔记
- leetcode刷题笔记
- [leetCode刷题笔记]2017.02.01
- [leetCode刷题笔记]2017.02.02
- [leetCode刷题笔记]2017.02.03
- [leetCode刷题笔记]2017.02.04
- [leetCode刷题笔记]2017.02.05
- [leetCode刷题笔记]2017.02.06
- [leetCode刷题笔记]2017.02.08
- [leetCode刷题笔记]2017.02.09
- [leetCode刷题笔记]2017.02.11
- [leetCode刷题笔记]2017.02.12
- [leetCode刷题笔记]2017.02.13
- [leetCode刷题笔记]2017.02.14
- [leetCode刷题笔记]2017.02.16
- scrum理解
- 1027. Colors in Mars 解析
- C++的一些知识以及和C语言的不同
- ZCMU-1416-Find the Lost Sock
- java服务器代理线程
- [leetCode刷题笔记]2017.02.07
- EventBus注销的必要性,用实例说明为什么EventBus用完必须注销
- toposort
- windows.event对象介绍
- Webstrom 20163.3刷新页面一直提示未授权的解决方法
- 1028. List Sorting 解析
- Accessibility辅助功能--一念天堂,一念地狱
- HPC GPU Node:
- Android系统广播大全及开机自启动的服务