Skip to content

Commit 383429c

Browse files
committed
fd
1 parent 8aa0674 commit 383429c

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

solution/src/main/java/com/inuker/solution/FindTheDuplicateNumber.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
* Created by dingjikerbo on 2016/12/17.
55
*/
66

7+
/**
8+
* 题目的意思是有n+1个数,这些数的范围限定在[1,n],其中只有一个重复数,重复的次数不限
9+
*/
710
public class FindTheDuplicateNumber {
811

12+
/**
13+
* 这道题涉及木桶原理,如果元素个数比桶多,说明有些桶里装了多个元素。
14+
* 一共迭代lgn次,每次O(n),因此时间复杂度O(nlgn),空间O(l)
15+
*/
916
public int findDuplicate(int[] nums) {
1017
int min = 1, max = nums.length - 1;
1118

@@ -20,6 +27,12 @@ public int findDuplicate(int[] nums) {
2027
}
2128
}
2229

30+
/**
31+
* 如果count>mid,说明元素个数count比桶数mid还打,则可以肯定重复的数在[1,mid]中
32+
* 但是count<=mid就能确定重复的数肯定不在[1,mid]中么?不一定吧,接下来推理一下:
33+
* 我们看右边的一半,[mid+1,n]区间的数有n+1-count个,可见元素有n+1-count个,桶有n-mid个,而count<=mid等价于count-1<mid,
34+
* 因此元素个数大于桶数,右区间一定有重复的数,由于题目已经限定只有一个重复数,因此只能在右区间
35+
*/
2336
if (count > mid) {
2437
max = mid;
2538
} else {

solution/src/main/java/com/inuker/solution/SearchInRotatedSortedArray.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@
55
*/
66

77
public class SearchInRotatedSortedArray {
8-
8+
/**
9+
* TestCase
10+
* [1, 2], 1/2/3
11+
* [2, 1], 2/1/3
12+
* [1], 1/2
13+
*/
914
public int search(int[] nums, int target) {
1015
int left = 0, right = nums.length - 1;
1116

12-
// 注意这里的等号
1317
while (left <= right) {
14-
int mid = left + ((right - left) >>> 1);
18+
int mid = (left + right) / 2;
1519

1620
if (target == nums[mid]) {
1721
return mid;
1822
}
1923

20-
// 注意这里的等号
21-
// 先确定单调区间,然后判断target是不是在单调区间内,如果不在就在另一半区间
2224
if (nums[mid] >= nums[left]) {
2325
if (target >= nums[left] && target < nums[mid]) {
2426
right = mid - 1;
2527
} else {
2628
left = mid + 1;
2729
}
28-
} else if (nums[mid] < nums[right]) {
30+
} else {
2931
if (target > nums[mid] && target <= nums[right]) {
3032
left = mid + 1;
3133
} else {

solution/src/main/java/com/inuker/solution/SearchInRotatedSortedArrayII.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
*/
66

77
public class SearchInRotatedSortedArrayII {
8-
8+
/**
9+
* TestCase
10+
* [3,1,1], 3
11+
*/
912
public int search(int[] nums, int target) {
1013
int left = 0, right = nums.length - 1;
1114

test/src/main/java/com/inuker/test/Test2.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,38 @@
2121
*/
2222

2323
public class Test2 {
24+
/**
25+
* TestCase
26+
* [3,1,1], 3
27+
*/
28+
public int search(int[] nums, int target) {
29+
int left = 0, right = nums.length - 1;
2430

25-
public void moveZeroes(int[] nums) {
26-
for (int i = 0, j = nums.length - 1; j > i; ) {
27-
if (nums[i] != 0) {
28-
i++;
29-
} else if (nums[j] != 0) {
30-
swap(nums, i, j--);
31+
while (left <= right) {
32+
int mid = (left + right) / 2;
33+
34+
if (target == nums[mid]) {
35+
return mid;
36+
}
37+
38+
if (nums[mid] > nums[left]) {
39+
if (target >= nums[left] && target < nums[mid]) {
40+
right = mid - 1;
41+
} else {
42+
left = mid + 1;
43+
}
44+
} else if (nums[mid] == nums[left]) {
45+
left++;
3146
} else {
32-
j--;
47+
if (target > nums[mid] && target <= nums[right]) {
48+
left = mid + 1;
49+
} else {
50+
right = mid - 1;
51+
}
3352
}
3453
}
35-
}
3654

37-
private void swap(int[] nums, int i, int j) {
38-
int t = nums[i];
39-
nums[i] = nums[j];
40-
nums[j] = t;
55+
return -1;
4156
}
4257

4358
}

0 commit comments

Comments
 (0)