File tree Expand file tree Collapse file tree 4 files changed +52
-19
lines changed
solution/src/main/java/com/inuker/solution
test/src/main/java/com/inuker/test Expand file tree Collapse file tree 4 files changed +52
-19
lines changed Original file line number Diff line number Diff line change 4
4
* Created by dingjikerbo on 2016/12/17.
5
5
*/
6
6
7
+ /**
8
+ * 题目的意思是有n+1个数,这些数的范围限定在[1,n],其中只有一个重复数,重复的次数不限
9
+ */
7
10
public class FindTheDuplicateNumber {
8
11
12
+ /**
13
+ * 这道题涉及木桶原理,如果元素个数比桶多,说明有些桶里装了多个元素。
14
+ * 一共迭代lgn次,每次O(n),因此时间复杂度O(nlgn),空间O(l)
15
+ */
9
16
public int findDuplicate (int [] nums ) {
10
17
int min = 1 , max = nums .length - 1 ;
11
18
@@ -20,6 +27,12 @@ public int findDuplicate(int[] nums) {
20
27
}
21
28
}
22
29
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
+ */
23
36
if (count > mid ) {
24
37
max = mid ;
25
38
} else {
Original file line number Diff line number Diff line change 5
5
*/
6
6
7
7
public class SearchInRotatedSortedArray {
8
-
8
+ /**
9
+ * TestCase
10
+ * [1, 2], 1/2/3
11
+ * [2, 1], 2/1/3
12
+ * [1], 1/2
13
+ */
9
14
public int search (int [] nums , int target ) {
10
15
int left = 0 , right = nums .length - 1 ;
11
16
12
- // 注意这里的等号
13
17
while (left <= right ) {
14
- int mid = left + (( right - left ) >>> 1 ) ;
18
+ int mid = ( left + right ) / 2 ;
15
19
16
20
if (target == nums [mid ]) {
17
21
return mid ;
18
22
}
19
23
20
- // 注意这里的等号
21
- // 先确定单调区间,然后判断target是不是在单调区间内,如果不在就在另一半区间
22
24
if (nums [mid ] >= nums [left ]) {
23
25
if (target >= nums [left ] && target < nums [mid ]) {
24
26
right = mid - 1 ;
25
27
} else {
26
28
left = mid + 1 ;
27
29
}
28
- } else if ( nums [ mid ] < nums [ right ]) {
30
+ } else {
29
31
if (target > nums [mid ] && target <= nums [right ]) {
30
32
left = mid + 1 ;
31
33
} else {
Original file line number Diff line number Diff line change 5
5
*/
6
6
7
7
public class SearchInRotatedSortedArrayII {
8
-
8
+ /**
9
+ * TestCase
10
+ * [3,1,1], 3
11
+ */
9
12
public int search (int [] nums , int target ) {
10
13
int left = 0 , right = nums .length - 1 ;
11
14
Original file line number Diff line number Diff line change 21
21
*/
22
22
23
23
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 ;
24
30
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 ++;
31
46
} else {
32
- j --;
47
+ if (target > nums [mid ] && target <= nums [right ]) {
48
+ left = mid + 1 ;
49
+ } else {
50
+ right = mid - 1 ;
51
+ }
33
52
}
34
53
}
35
- }
36
54
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 ;
41
56
}
42
57
43
58
}
You can’t perform that action at this time.
0 commit comments