联系,KM问题

pull/3/head
Leo 5 years ago
parent 5ad56aa9a8
commit 1ebc306775

@ -133,6 +133,32 @@ public class BSAwesome {
} }
public static int BSAwesome3(int[] arr) {
if (arr.length == 0 || arr == null) {
return -1;
}
if (arr.length == 1 || arr[0] < arr[1]) {
return 0;
}
if (arr[arr.length - 1] < arr[arr.length - 2]) {
return arr.length - 1;
}
int L = 0;
int R = arr.length - 1;
int mid = 0;
while (L < R) {
mid = L + ((R - L) >> 1);
if (arr[mid] > arr[mid + 1]) {
L = mid + 1;
} else if (arr[mid] > arr[mid - 1]) {
R = mid - 1;
}else {
return mid;
}
}
return -1;
}
public static int verifyBSAwesome(int[] arr, int index) { public static int verifyBSAwesome(int[] arr, int index) {
@ -172,7 +198,7 @@ public class BSAwesome {
boolean succeed = true; boolean succeed = true;
for (int i = 0; i < testTime; i++) { for (int i = 0; i < testTime; i++) {
int[] arr = ArrayUtil.randomAdjacentNotEqualArray(maxSize, range); int[] arr = ArrayUtil.randomAdjacentNotEqualArray(maxSize, range);
final int index = BSAwesome2(arr); final int index = BSAwesome3(arr);
final int verifyIndex = verifyBSAwesome(arr,index); final int verifyIndex = verifyBSAwesome(arr,index);
if (index != verifyIndex) { if (index != verifyIndex) {
succeed = false; succeed = false;

@ -109,6 +109,27 @@ public class BSExist {
} }
public static boolean exist5(int[] arr, int value) {
if (arr.length <= 0 || arr == null) {
return false;
}
int L = 0;
int R = arr.length - 1;
int mid = 0;
while (L < R) {
mid = L + ((R - L) >> 1);
if (arr[mid] > value) {
R = mid - 1;
} else if (arr[mid] < value) {
L = mid + 1;
}else{
return true;
}
}
return arr[L] == value;
}
/** /**
* : test * : test
* @author Leo * @author Leo
@ -137,7 +158,7 @@ public class BSExist {
int[] sortArr = ArrayUtil.randomSortArray(maxSize, range); int[] sortArr = ArrayUtil.randomSortArray(maxSize, range);
int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random()); int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random());
if (exist4(sortArr, value) != exist(sortArr, value)) { if (exist5(sortArr, value) != exist(sortArr, value)) {
succeed = false; succeed = false;
ArrayUtil.printArr(sortArr); ArrayUtil.printArr(sortArr);
break; break;

@ -105,6 +105,27 @@ public class BSNear {
} }
public static int BSNearLeft5(int[] arr, int value) {
if (arr.length <= 0 || arr == null) {
return -1;
}
int L = 0;
int R = arr.length - 1;
int mid = 0;
int index = -1;
while (L <= R) {
mid = L + ((R - L) >> 1);
if (arr[mid] >= value) {
index = mid;
R = mid - 1;
}else{
L = mid + 1;
}
}
return index;
}
/** /**
* : >=(for test) * : >=(for test)
* @author Leo * @author Leo
@ -202,6 +223,27 @@ public class BSNear {
return index; return index;
} }
public static int BSNearRight4(int[] arr, int value) {
int index = -1;
if (arr.length <= 0 || arr == null) {
return index;
}
int L = 0;
int R = arr.length - 1;
int mid = 0;
while (L <= R) {
mid = L + ((R - L) >> 1);
if (arr[mid] <= value) {
index = mid;
L = mid + 1;
}else{
R = mid - 1;
}
}
return index;
}
public static int forTestBSNearRight(int[] arr, int value) { public static int forTestBSNearRight(int[] arr, int value) {
int index = -1; int index = -1;
if (arr == null || arr.length == 0) { if (arr == null || arr.length == 0) {
@ -224,7 +266,7 @@ public class BSNear {
for (int i = 0; i < testTime; i++) { for (int i = 0; i < testTime; i++) {
int[] sortArr = ArrayUtil.randomSortArray(maxSize, range); int[] sortArr = ArrayUtil.randomSortArray(maxSize, range);
int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random()); int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random());
final int res1 = BSNearLeft4(sortArr, value); /* final int res1 = BSNearLeft5(sortArr, value);
final int res2 = forTestBSNearLeft(sortArr, value); final int res2 = forTestBSNearLeft(sortArr, value);
if (res1 != res2) { if (res1 != res2) {
success = false; success = false;
@ -232,8 +274,8 @@ public class BSNear {
System.out.println("BSNearLeft=" + res1); System.out.println("BSNearLeft=" + res1);
System.out.println("forTestBSNearLeft=" + res2); System.out.println("forTestBSNearLeft=" + res2);
break; break;
} }*/
/*final int res3 = BSNearRight(sortArr, value); final int res3 = BSNearRight4(sortArr, value);
final int res4 = forTestBSNearRight(sortArr, value); final int res4 = forTestBSNearRight(sortArr, value);
if (res3 != res4) { if (res3 != res4) {
success = false; success = false;
@ -241,7 +283,7 @@ public class BSNear {
System.out.println("BSNearRight=" + res3); System.out.println("BSNearRight=" + res3);
System.out.println("forTestBSNearRight=" + res4); System.out.println("forTestBSNearRight=" + res4);
break; break;
}*/ }
} }
System.out.println(success ? "Nice!!" : "Fucking Fucked!"); System.out.println(success ? "Nice!!" : "Fucking Fucked!");
} }

@ -101,6 +101,32 @@ public class BubbleSort {
} }
} }
public static void bubbleSort7(int[] arr) {
if (arr.length == 0 || arr == null) {
return;
}
for (int i = arr.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
}
public static void bubbleSort8(int[] arr) {
if (arr.length == 0 || arr == null) {
return;
}
for (int i = arr.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
}
/** /**
* : * :
* @author Leo * @author Leo
@ -131,7 +157,7 @@ public class BubbleSort {
for (int i = 0; i < testOfTime; i++) { for (int i = 0; i < testOfTime; i++) {
int[] arr = ArrayUtil.randomArray(maxSize, range); int[] arr = ArrayUtil.randomArray(maxSize, range);
int[] anotherArr = ArrayUtil.copyArray(arr); int[] anotherArr = ArrayUtil.copyArray(arr);
bubbleSort6(arr); bubbleSort8(arr);
Arrays.sort(anotherArr); Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) { if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false; succeed = false;

@ -32,6 +32,14 @@ public class EvenTimesOddTimes {
System.out.println(eor); System.out.println(eor);
} }
public static void printOdd2(int[] arr) {
int eor = 0;
for (int i = 0; i < arr.length; i++) {
eor ^= arr[i];
}
System.out.println(eor);
}
/** /**
* : , * : ,
@ -74,4 +82,25 @@ public class EvenTimesOddTimes {
} }
public static void printOddTwo3(int[] arr) {
int eor = 0;
for (int i = 0; i < arr.length; i++) {
eor ^= arr[i];
}
int rightOnly = eor & (-eor);
int eorOther = 0;
for (int i = 0; i < arr.length; i++) {
if ((rightOnly & arr[i]) != 0) {
eorOther ^= arr[i];
}
}
System.out.println(eor + " " + (eor ^ eorOther));
}
} }

@ -90,6 +90,30 @@ public class InsertionSort {
} }
} }
public static void insertionSort6(int[] arr) {
if (arr == null || arr.length < 0) {
return;
}
for (int i = 1; i < arr.length; i++) {
for (int j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--) {
swap(arr, j, j + 1);
}
}
}
public static void insertionSort7(int[] arr) {
if (arr == null || arr.length < 0) {
return;
}
for (int i = 1; i < arr.length; i++) {
for (int j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--) {
swap(arr, j, j + 1);
}
}
}
private static void swap(int[] arr, int i, int j) { private static void swap(int[] arr, int i, int j) {
if (arr == null if (arr == null
|| arr.length < 2 || arr.length < 2
@ -114,7 +138,7 @@ public class InsertionSort {
for (int i = 0; i < testOfTime; i++) { for (int i = 0; i < testOfTime; i++) {
int[] arr = ArrayUtil.randomArray(maxSize, range); int[] arr = ArrayUtil.randomArray(maxSize, range);
int[] anotherArr = ArrayUtil.copyArray(arr); int[] anotherArr = ArrayUtil.copyArray(arr);
insertionSort5(arr); insertionSort7(arr);
Arrays.sort(anotherArr); Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) { if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false; succeed = false;

@ -0,0 +1,135 @@
package leo.class01;
import java.util.HashMap;
import java.util.HashSet;
/**
* @author Leo
* @ClassName KM
* @DATE 2020/11/15 6:48
* @Description arrK,M,k<m,K.
* & 00; | 11;
*/
public class KM {
public static int testForOnlyKTimes(int[] arr, int k, int m) {
HashMap<Integer, Integer> map = new HashMap<>(8);
for (int num : arr) {
if (map.containsKey(num)) {
map.put(num, map.get(num) + 1);
}else {
map.put(num, 1);
}
}
for (int num : map.keySet()) {
if (map.get(num) == k) {
return num;
}
}
return -1;
}
public static int onlyKTimes(int[] arr, int k, int m) {
//准备32位的数组,用于记录arr中所有出现数的每一位出现1的次数
int[] t = new int[32];
for (int num : arr) {
for (int i = 0; i < t.length; i++) {
t[i] += (num >> i) & 1;
}
}
int ans = 0;
for (int i = 0; i < t.length; i++) {
//如果t的某一位取模m不等0;说明出现k次数的位上有1
if (t[i] % m != 0) {
ans |= (1 << i);
}
}
return ans;
}
public static void main(String[] args) {
int maxKinds = 8;
int range = 50;
int testTimes = 1000;
int max = 9;
System.out.println("开始");
for (int i = 0; i < testTimes; i++) {
int a = (int) (max * Math.random() + 1);
int b = (int) (max * Math.random() + 1);
int k = Math.min(a, b);
int m = Math.max(a, b);
if (k == m) {
m++;
}
int[] arr = randomArray(maxKinds, range, k, m);
int ans = onlyKTimes(arr, k, m);
int ans2 = testForOnlyKTimes(arr, k, m);
if (ans != ans2) {
System.out.println("出错了!!");
break;
}
}
System.out.println("结束");
}
/**
* :
* @author Leo
* @date 2020/11/15 8:01
* @param maxKinds
* @param range
* @param k k
* @param m m
* @throw
* @return int[]
*/
private static int[] randomArray(int maxKinds, int range, int k, int m) {
//一共几种数,必须大于2
int numKinds = (int) (maxKinds * Math.random() + 2);
//出现k的数
int kValue = randomInt(range);
//arr长度
int[] arr = new int[k + (numKinds - 1) * m];
int index = 0;
for (; index < k; index++) {
arr[index] = kValue;
}
numKinds--;
HashSet<Integer> set = new HashSet<>();
set.add(kValue);
while (numKinds != 0) {
int curNum;
do {
curNum = randomInt(range);
} while (set.contains(curNum));
set.add(curNum);
numKinds--;
for (int i = 0; i < m; i++) {
arr[index++] = curNum;
}
}
for (int i = 0; i < arr.length; i++) {
int j = (int) (arr.length * Math.random());
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}
private static int randomInt(int range) {
return (int) ((range * Math.random() + 1) - (range * Math.random() + 1));
}
}

@ -122,6 +122,33 @@ public class SelectionSort {
} }
} }
public static void selectionSort8(int[] arr) {
if (arr == null || arr.length < 0) {
return;
}
for (int i = 0; i < arr.length-1; i++) {
for (int j = i+1; j < arr.length; j++) {
if (arr[i] > arr[j ]) {
swap(arr, i, j );
}
}
}
}
public static void selectionSort9(int[] arr) {
if (arr == null || arr.length <=1) {
return;
}
for (int i = 0; i < arr.length-1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
swap(arr, i, j);
}
}
}
}
/** /**
* : * :
@ -153,7 +180,7 @@ public class SelectionSort {
for (int i = 0; i < testOfTime; i++) { for (int i = 0; i < testOfTime; i++) {
int[] arr = ArrayUtil.randomArray(maxSize, range); int[] arr = ArrayUtil.randomArray(maxSize, range);
int[] anotherArr = ArrayUtil.copyArray(arr); int[] anotherArr = ArrayUtil.copyArray(arr);
selectionSort7(arr); selectionSort9(arr);
Arrays.sort(anotherArr); Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) { if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false; succeed = false;

Loading…
Cancel
Save