返回结果换成list
在找到这个结果后,把其加入list 并,继续找
package a;
import java.util.ArrayList;
import java.util.Arrays;
public class BaniarySearchDuplicate {
public static void main(String[] args) {
int[] arr = {1, 2, 2, 2, 3, 4, 5, 5, 5, 6, 6, 6, 66};
int sta = 0;
int end = arr.length-1;
int value = 5;
ArrayList<Integer> ress = seek(arr,value,sta,end);
System.out.println(ress);
}
private static ArrayList<Integer> seek(int[] arr, int value, int sta, int end) {
ArrayList ress = new ArrayList<Integer>();
if (sta > end) {
return ress;
}
int mid = (sta+end)/2;
if (arr[mid] < value) {
return seek(arr, value, mid + 1, end);
} else if (arr[mid] > value) {
return seek(arr, value, sta, mid - 1);
}else {
int temp = mid;
while (temp >= 0 && arr[temp] == value) {
ress.add(temp);
temp--;
}
mid++;
while (mid<= arr.length && arr[mid] == value) {
ress.add(mid);
mid++;
}
return ress;
}
}
}