Skip to content

Commit e9e785e

Browse files
committed
update
1 parent 19ccb3a commit e9e785e

7 files changed

Lines changed: 490 additions & 0 deletions
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "旋转有序数组的二分查找问题"
5+
tags : [LeetCode,面试,二分查找,有序数组,旋转]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4>{{page.title|remove_first:'Leetcode之'}}</h4>
10+
11+
12+
>Suppose a sorted array is rotated at some pivot unknown to you beforehand.
13+
>
14+
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
15+
>
16+
>You are given a target value to search. If found in the array return its index, otherwise return -1.
17+
>
18+
>You may assume no duplicate exists in the array.
19+
20+
解法:哎,不好干啊。思想肯定是二分的思想,二分主要就是取哪一边的问题。分析见代码注释。
21+
22+
23+
代码如下:
24+
25+
//{java}
26+
public class Solution {
27+
public int search(int[] A, int target) {
28+
int s=0,e=A.length-1;
29+
while(s<=e)
30+
{
31+
int t=(s+e)/2;
32+
if(A[t]==target)
33+
return t;
34+
if(A[t]<target)
35+
{
36+
if(A[t]>A[e])
37+
//说明旋转点在t-e之间,要找的值肯定也在其中
38+
{
39+
s=t+1;
40+
}
41+
//旋转点不在t-e之间,要找的数可能在t-e之间,也可能在s-t之间。t-e之间的数肯定是升序排列,判断target是否可能在t-e之间。
42+
else if(A[e]>=target)
43+
{
44+
45+
s=t+1;
46+
}
47+
else
48+
{
49+
e=t-1;
50+
}
51+
}
52+
else if(A[t]>target)
53+
{
54+
if(A[t]<A[s])
55+
{
56+
e=t-1;
57+
}
58+
else if(A[s]<=target)
59+
{
60+
e=t-1;
61+
}
62+
else
63+
{
64+
s=t+1;
65+
}
66+
}
67+
}
68+
return -1;
69+
}
70+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "k在数组中可能的组合,可以重复使用元素"
5+
tags : [LeetCode,面试,组合,递归]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4>{{page.title|remove_first:'Leetcode之'}}</h4>
10+
11+
12+
> Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
13+
>
14+
The same repeated number may be chosen from C unlimited number of times.
15+
>For example, given candidate set 2,3,6,7 and target 7,
16+
A solution set is:
17+
`[7]`
18+
`[2, 2, 3]`
19+
20+
>要求:不能有重复组合,组合中元素升序排列。
21+
22+
解法:解法是递归,这里有些技巧,
23+
24+
1. 先将数组进行排序,这样会方便许多。
25+
2. 比如当tar为0是,返回一个List集合,集合中要有一个空的List。
26+
3. 标记当前查找的位置,每次只从当前位置向后找,因为数组是有序的,这样能保证不会有重复的组合以及数组会升序排列。
27+
28+
这题与后一题不同,这题数组中的元素是不会重复的。
29+
30+
代码如下:
31+
32+
//{java}
33+
public class Solution {
34+
public List<List<Integer>> combinationSum(int[] c, int tar) {
35+
Arrays.sort(c);
36+
return slove(c,tar,0);
37+
38+
}
39+
40+
//c中不会有重复元素
41+
public List<List<Integer>> slove(int[] c, int tar,int position) {
42+
List<List<Integer>> re = new ArrayList<List<Integer>>();
43+
if(tar==0)
44+
re.add(new ArrayList<Integer>());
45+
for(int i=position;i<c.length&&c[i]<=tar;i++)
46+
{
47+
List<List<Integer>> ll = slove(c,tar-c[i],i);
48+
for(List<Integer> l:ll)
49+
{
50+
l.add(0,c[i]);
51+
re.add(l);
52+
}
53+
}
54+
return re;
55+
}
56+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "k在数组中可能的组合,不能重复使用元素"
5+
tags : [LeetCode,面试,组合,递归]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4>{{page.title|remove_first:'Leetcode之'}}</h4>
10+
11+
12+
> Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
13+
>
14+
Each number in C may only be used once in the combination.
15+
>
16+
>For example, given candidate set `10,1,2,7,6,1,5` and target `8`,
17+
A solution set is:
18+
`[1, 7]`
19+
`[1, 2, 5]`
20+
`[2, 6]`
21+
`[1, 1, 6]`
22+
23+
>要求:不能有重复组合,组合中元素升序排列。
24+
25+
解法:解法大致与前一题类似。需要注意的是。但这题与前一题不同,这题数组中的元素是是会重复的。如果是用完一个数,直接从他的下一个数开始搜,有可能会有重复组合。
26+
如`[1,1,1,1] and target=3`,得出的结果就是`[1,1,1],[1,1,1]`。
27+
这里需要做些限制即可,在同一个层次的搜索中,相同的数只会被用来搜索一次。
28+
29+
代码如下:
30+
31+
//{java}
32+
public class Solution {
33+
public List<List<Integer>> combinationSum2(int[] c, int tar) {
34+
Arrays.sort(c);
35+
return slove(c,tar,0);
36+
37+
}
38+
39+
//c中可能有重复元素
40+
public List<List<Integer>> slove(int[] c, int tar,int position) {
41+
List<List<Integer>> re = new ArrayList<List<Integer>>();
42+
if(tar==0)
43+
re.add(new ArrayList<Integer>());
44+
for(int i=position;i<c.length&&c[i]<=tar;i++)
45+
{
46+
if(i!=position && c[i]==c[i-1])
47+
continue;
48+
List<List<Integer>> ll = slove(c,tar-c[i],i+1);
49+
for(List<Integer> l:ll)
50+
{
51+
l.add(0,c[i]);
52+
re.add(l);
53+
}
54+
}
55+
return re;
56+
}
57+
58+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "二分查找的变种"
5+
tags : [LeetCode,面试,查找,二分]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4>{{page.title|remove_first:'Leetcode之'}}</h4>
10+
11+
12+
> Given a sorted array of integers, find the starting and ending position of a given target value.
13+
>
14+
>Your algorithm's runtime complexity must be in the order of O(log n).
15+
>
16+
>If the target is not found in the array, return `[-1, -1]`.
17+
>
18+
>For example,
19+
Given `[5, 7, 7, 8, 8, 10]` and target value 8,
20+
return `[3, 4]`.
21+
22+
解法:这题先看看这个数能不能找到,如果能找到的话,再找出他的左右边界,找边界也是可以二分查找的。
23+
24+
代码如下:
25+
26+
//{java}
27+
public class Solution {
28+
public int[] searchRange(int[] A, int target) {
29+
int s=0,e=A.length-1,t=0;
30+
while(s<=e)
31+
{
32+
t =(s+e)/2;
33+
if(A[t]==target)
34+
{
35+
int left = findLeftBound(A,target,t-1);
36+
int right = findRightBound(A,target,t+1);
37+
return new int[]{left,right};
38+
}
39+
if(A[t]<target)
40+
s=t+1;
41+
else
42+
e=t-1;
43+
}
44+
return new int []{-1,-1};
45+
}
46+
public int findLeftBound(int[] A, int target, int e)
47+
{
48+
int s=0;
49+
while(s<=e)
50+
{
51+
int t =(s+e)/2;
52+
if(A[t]==target)
53+
e=t-1;
54+
else
55+
s=t+1;
56+
}
57+
return s;
58+
}
59+
60+
public int findRightBound(int[] A, int target, int s)
61+
{
62+
int e=A.length-1;
63+
while(s<=e)
64+
{
65+
int t =(s+e)/2;
66+
if(A[t]==target)
67+
s=t+1;
68+
else
69+
e=t-1;
70+
}
71+
return e;
72+
}
73+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
layout: post
3+
category : LeetCode
4+
tagline: "数独"
5+
tags : [LeetCode,面试,数独,递归]
6+
---
7+
{% include JB/setup %}
8+
9+
<h4>{{page.title|remove_first:'Leetcode之'}}</h4>
10+
11+
12+
>Write a program to solve a Sudoku puzzle by filling the empty cells.
13+
>
14+
>Empty cells are indicated by the character '.'.
15+
>
16+
>You may assume that there will be only one unique solution.
17+
18+
解法:数独问题,构造递归`slove(char[][] board,int i,int j)`表示在`i,j`之前的局面都是合法的,对每个`board[i][j]`可能出现的数都搜索一遍,当`i=9`时说明局面有效。
19+
20+
代码如下:
21+
22+
//{java}
23+
public class Solution {
24+
public void solveSudoku(char[][] board) {
25+
slove(board,0,0);
26+
}
27+
28+
public boolean slove(char[][] board,int i,int j)
29+
{
30+
if(j==9)
31+
{
32+
j=0;
33+
i+=1;
34+
}
35+
if(i==9)
36+
return true;
37+
if(board[i][j]=='.')
38+
{
39+
int flag[]=new int[9];
40+
for(int k=0;k<9;k++)
41+
{
42+
if(board[i][k]!='.')
43+
flag[board[i][k]-'1']=1;
44+
}
45+
for(int k=0;k<9;k++)
46+
{
47+
if(board[k][j]!='.')
48+
flag[board[k][j]-'1']=1;
49+
}
50+
for(int m=0;m<3;m++)
51+
{
52+
for(int n=0;n<3;n++)
53+
if(board[i/3*3+m][j/3*3+n]!='.')
54+
flag[board[i/3*3+m][j/3*3+n]-'1']=1;
55+
}
56+
for(int k=0;k<9;k++)
57+
{
58+
if(flag[k]==0)
59+
{
60+
board[i][j]=(char)(k+'1');
61+
if(slove(board,i,j+1))
62+
return true;
63+
}
64+
}
65+
board[i][j]='.';
66+
return false;
67+
}else
68+
{
69+
return slove(board,i,j+1);
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)