pull/3/head
Leo 5 years ago
parent 42d6552ffa
commit 8b6af78573

@ -216,6 +216,33 @@ public class BSAwesome {
return -1;
}
public static int BSAwesome6(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) {
if (arr.length == 0 || index == -1) {
return -1;
@ -253,7 +280,7 @@ public class BSAwesome {
boolean succeed = true;
for (int i = 0; i < testTime; i++) {
int[] arr = ArrayUtil.randomAdjacentNotEqualArray(maxSize, range);
final int index = BSAwesome5(arr);
final int index = BSAwesome6(arr);
final int verifyIndex = verifyBSAwesome(arr,index);
if (index != verifyIndex) {
succeed = false;

@ -3,6 +3,7 @@ package leo.class01;
import leo.util.ArrayUtil;
import javax.swing.plaf.TreeUI;
import java.util.Arrays;
/**
@ -172,6 +173,26 @@ public class BSExist {
return arr[L] == value;
}
public static boolean exist8(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
* @author Leo
@ -200,7 +221,7 @@ public class BSExist {
int[] sortArr = ArrayUtil.randomSortArray(maxSize, range);
int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random());
if (exist7(sortArr, value) != exist(sortArr, value)) {
if (exist8(sortArr, value) != exist(sortArr, value)) {
succeed = false;
ArrayUtil.printArr(sortArr);
break;

@ -189,6 +189,26 @@ public class BSNear {
return index;
}
public static int BSNearLeft9(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)
* @author Leo
@ -349,6 +369,28 @@ public class BSNear {
return index;
}
public static int BSNearRight7(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;
L = mid + 1;
}else{
R = mid - 1;
}
}
return index;
}
public static int forTestBSNearRight(int[] arr, int value) {
int index = -1;
if (arr == null || arr.length == 0) {
@ -371,7 +413,7 @@ public class BSNear {
for (int i = 0; i < testTime; i++) {
int[] sortArr = randomArray(maxSize, range);
int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random());
final int res1 = BSNearLeft8(sortArr, value);
/*final int res1 = BSNearLeft9(sortArr, value);
final int res2 = forTestBSNearLeft(sortArr, value);
if (res1 != res2) {
success = false;
@ -379,8 +421,8 @@ public class BSNear {
System.out.println("BSNearLeft=" + res1);
System.out.println("forTestBSNearLeft=" + res2);
break;
}
/*final int res3 = BSNearRight6(sortArr, value);
}*/
final int res3 = BSNearRight7(sortArr, value);
final int res4 = forTestBSNearRight(sortArr, value);
if (res3 != res4) {
success = false;
@ -388,7 +430,7 @@ public class BSNear {
System.out.println("BSNearRight=" + res3);
System.out.println("forTestBSNearRight=" + res4);
break;
}*/
}
}
System.out.println(success ? "Nice!!" : "Fucking Fucked!");
}

@ -154,6 +154,18 @@ public class BubbleSort {
}
}
public static void bubbleSort11(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);
}
}
}
}
@ -188,7 +200,7 @@ public class BubbleSort {
for (int i = 0; i < testOfTime; i++) {
int[] arr = ArrayUtil.randomArray(maxSize, range);
int[] anotherArr = ArrayUtil.copyArray(arr);
bubbleSort10(arr);
bubbleSort11(arr);
Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false;

@ -2,6 +2,8 @@ package leo.class01;
import sun.applet.Main;
import java.nio.ByteOrder;
/**
* @author Leo
* @ClassName EvenTimesOddTimes
@ -60,6 +62,15 @@ public class EvenTimesOddTimes {
}
public static void printOdd5(int[] arr) {
int eor = 0;
for (int num : arr) {
eor ^= num;
}
System.out.println(eor);
}
/**
* : ,
@ -152,13 +163,28 @@ public class EvenTimesOddTimes {
System.out.println(eorOther + " " + (eor ^ eorOther));
}
public static void printOddTwo6(int[] arr) {
int eor = 0;
for (int num : arr) {
eor ^= num;
}
//找出最右侧出现1的位置
int rightOne = eor & (-eor);
int eorOther = 0;
for (int num : arr) {
if ((rightOne & num) != 0) {
eorOther ^= num;
}
}
System.out.println(eorOther + " " + (eor ^ eorOther));
}
public static void main(String[] args){
int[] arrOne = {1, 1, 5, 5, 8, 1, 8, 5, 5};
printOdd4(arrOne);
printOdd5(arrOne);
int[] arrTwo = {1, 1, 9, 5, 5, 8, 1, 8, 9, 5, 5, 5};
printOddTwo5(arrTwo);
printOddTwo6(arrTwo);
}

@ -164,6 +164,39 @@ public class InsertionSort {
}
}
public static void insertionSort12(int[] arr) {
if (arr.length == 0 || arr == null) {
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 insertionSort13(int[] arr) {
if (arr.length == 0 || arr == null) {
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 insertionSort14(int[] arr) {
if (arr.length == 0 || arr == null) {
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) {
if (arr == null
|| arr.length < 2
@ -188,7 +221,7 @@ public class InsertionSort {
for (int i = 0; i < testOfTime; i++) {
int[] arr = ArrayUtil.randomArray(maxSize, range);
int[] anotherArr = ArrayUtil.copyArray(arr);
insertionSort11(arr);
insertionSort14(arr);
Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false;

@ -1,5 +1,7 @@
package leo.class01;
import class01.Code08_KM;
import java.util.HashMap;
import java.util.HashSet;
@ -119,10 +121,44 @@ public class KM {
}
public static int onlyKTime5(int[] arr, int k, int m) {
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++) {
if (t[i] % m != 0) {
if (t[i] % m == k) {
ans |= (1 << i);
}else{
return -1;
}
}
}
if (ans == 0) {
int count = 0;
for (int num : arr) {
if (num == 0) {
count++;
}
}
if (count != k) {
return -1;
}
}
return ans;
}
public static void main(String[] args) {
int maxKinds = 8;
int maxKinds = 20;
int range = 50;
int testTimes = 1000;
int testTimes = 10000;
int max = 9;
System.out.println("开始");
for (int i = 0; i < testTimes; i++) {
@ -133,15 +169,15 @@ public class KM {
if (k == m) {
m++;
}
int[] arr = randomArray2(maxKinds, range, k, m);
int[] arr = randomArray(maxKinds, range, k, m);
int ans = onlyKTime4(arr, k, m);
int ans = onlyKTime5(arr, k, m);
int ans2 = testForOnlyKTimes(arr, k, m);
if (ans != ans2) {
System.out.println(ans);
System.out.println(ans2);
System.out.println("出错了!!");
break;
// break;
}
}
System.out.println("结束");
@ -192,12 +228,12 @@ public class KM {
int numKinds = (int) (maxKinds * Math.random() + 2);
//出现k的数
int kValue = randomInt(range);
int Ktime = Math.random() < 0.5 ? k : (int) ((Math.random() * (m - 1)) + 1);
//arr长度
int[] arr = new int[k + (numKinds - 1) * m];
int[] arr = new int[Ktime + (numKinds - 1) * m];
int index = 0;
for (; index < k; index++) {
for (; index < Ktime; index++) {
arr[index] = kValue;
}
numKinds--;

@ -176,6 +176,19 @@ public class SelectionSort {
}
}
public static void selectionSort12(int[] arr) {
if (arr.length == 0 || arr == null) {
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);
}
}
}
}
/**
* :
@ -207,7 +220,7 @@ public class SelectionSort {
for (int i = 0; i < testOfTime; i++) {
int[] arr = ArrayUtil.randomArray(maxSize, range);
int[] anotherArr = ArrayUtil.copyArray(arr);
selectionSort11(arr);
selectionSort12(arr);
Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false;

Loading…
Cancel
Save