代码:
import java.util.ArrayList;import java.util.List;public class Bit { int max; int min; int[] arr; public Bit(int[] arrInput) { // 找出极值 for (int i = 0; i < arrInput.length; i++) { if (max < arrInput[i]) { max = arrInput[i]; } if (min > arrInput[i]) { min = arrInput[i]; } } // 新建数组 arr = new int[max - min + 1]; // 数组插值 for (int i : arrInput) { int index = i - min; arr[index]++; } } public boolean hasDuplicateItem() { for (int i = 0; i < arr.length; i++) { int value = arr[i]; if (value > 1) { return true; } } return false; } public ListgetSortedList() { List ls = new ArrayList (); for (int i = 0; i < arr.length; i++) { int value = arr[i]; if (value != 0) { for (int j = 0; j < value; j++) { ls.add(min + i); } } } return ls; } public List getUniqueSortedList() { List ls = new ArrayList (); for (int i = 0; i < arr.length; i++) { int value = arr[i]; if (value != 0) { ls.add(min + i); } } return ls; } public int[] getUniqueSortedArray(){ List ls=getUniqueSortedList(); int[] arr=new int[ls.size()]; for(int i=0;i rightBound) { // 左边界大于右边界,已经找不到值 return -1; } else { if (sortedArray[curr] < seachValue) { // 当当前下标对应的值小于查找的值时,缩短左边界 leftBound = curr + 1; } else { // 当当前下标对应的值大于查找的值时,缩短右边界 rightBound = curr - 1; } } } } public static void main(String[] args) { int[] arr = { -2, -1, 3, 5, 7, 9, 30, 4, -2, 5, 8, 3 }; Bit b = new Bit(arr); System.out.println("排序后数组"); int[] arr2=b.getUniqueSortedArray(); for(int i=0;i
输出:
排序后数组0:-21:-12:33:44:55:76:87:98:302不在数组arr2中5排在数组arr2的第4个7排在数组arr2的第5个-2排在数组arr2的第0个90不在数组arr2中