Skip to content

Commit 6caf011

Browse files
author
Algorithmica
committed
reorganizing code
1 parent 57f66de commit 6caf011

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.alg.top20.divideprune;
2+
3+
4+
public class CountOnes {
5+
6+
public static int countOnes(int[] in) {
7+
int left = 0, right = in.length - 1;
8+
if (in.length == 0 ||
9+
in[left] == 0)
10+
return 0;
11+
while (left < right) {
12+
int mid = (int) Math.ceil(left + (right - left) >>> 1);
13+
if (in[mid] == 1)
14+
left = mid;
15+
else
16+
right = mid - 1;
17+
}
18+
return right + 1;
19+
}
20+
21+
public static void main(String[] args) {
22+
// TODO Auto-generated method stub
23+
24+
}
25+
26+
}

2018-feb/adhoc-thinking/src/com/alg/top20/adhoc/MatrixSearch.java renamed to 2018-feb/divide-and-prune/src/com/alg/top20/divideprune/MatrixSearch.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package com.alg.top20.adhoc;
1+
package com.alg.top20.divideprune;
2+
23

34
import java.util.Arrays;
45

@@ -35,7 +36,7 @@ public static boolean matrixSearch3(int[][] in, int target) {
3536
int left = 0, right = n * m - 1;
3637

3738
while (left <= right) {
38-
int mid = (left + right) / 2;
39+
int mid = left + (right-left)>>>1;
3940
int element = in[mid / n][mid % n];
4041
if (element == target)
4142
return true;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.alg.top20.divideprune;
2+
3+
4+
public class MinInRotatedSortedArray {
5+
6+
public static int findMin(int[] in) {
7+
int left = 0, right = in.length - 1;
8+
9+
while (left < right) {
10+
int mid = left + (right - left) >>> 1;
11+
if (in[mid] > in[right])
12+
left = mid + 1;
13+
else
14+
right = mid;
15+
}
16+
return in[right];
17+
}
18+
public static void main(String[] args) {
19+
// TODO Auto-generated method stub
20+
21+
}
22+
23+
}

0 commit comments

Comments
 (0)