复习/异或查找数字

pull/3/head
Leo 5 years ago
parent a60a9625ff
commit 5ad56aa9a8

@ -68,6 +68,14 @@ public class BSAwesome {
return -1; return -1;
} }
/**
* :
* @author Leo
* @date 2020/11/14 4:14
* @param arr
* @throw
* @return int
*/
public static int testBSAwesome(int[] arr) { public static int testBSAwesome(int[] arr) {
if (arr.length == 0 || arr == null) { if (arr.length == 0 || arr == null) {
return -1; return -1;
@ -98,6 +106,32 @@ public class BSAwesome {
} }
public static int BSAwesome2(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;
}
@ -138,7 +172,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 = BSAwesome1(arr); final int index = BSAwesome2(arr);
final int verifyIndex = verifyBSAwesome(arr,index); final int verifyIndex = verifyBSAwesome(arr,index);
if (index != verifyIndex) { if (index != verifyIndex) {
succeed = false; succeed = false;

@ -14,7 +14,7 @@ import java.util.Arrays;
public class BSExist { public class BSExist {
/** /**
* : * : ,
* @author Leo * @author Leo
* @date 2020/11/12 10:36 * @date 2020/11/12 10:36
* @param arr * @param arr
@ -87,6 +87,28 @@ public class BSExist {
} }
public static boolean exist4(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
@ -109,13 +131,13 @@ public class BSExist {
int maxSize = 50; int maxSize = 50;
int range = 500; int range = 500;
int testTime = 10; int testTime = 1000;
boolean succeed = true; boolean succeed = true;
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());
if (exist3(sortArr, value) != exist(sortArr, value)) { if (exist4(sortArr, value) != exist(sortArr, value)) {
succeed = false; succeed = false;
ArrayUtil.printArr(sortArr); ArrayUtil.printArr(sortArr);
break; break;

@ -84,6 +84,26 @@ public class BSNear {
} }
public static int BSNearLeft4(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)
@ -160,6 +180,28 @@ public class BSNear {
return index; return index;
} }
public static int BSNearRight3(int[] arr, int value) {
if (arr.length == 0 || arr == null) {
return -1;
}
int L = 0;
int R = arr.length - 1;
int index = -1;
int mid = 0;
while (L <= 2) {
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) {
@ -182,7 +224,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 = BSNearLeft3(sortArr, value); final int res1 = BSNearLeft4(sortArr, value);
final int res2 = forTestBSNearLeft(sortArr, value); final int res2 = forTestBSNearLeft(sortArr, value);
if (res1 != res2) { if (res1 != res2) {
success = false; success = false;
@ -190,8 +232,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 = BSNearRight(sortArr, value);
final int res4 = forTestBSNearRight(sortArr, value); final int res4 = forTestBSNearRight(sortArr, value);
if (res3 != res4) { if (res3 != res4) {
success = false; success = false;
@ -199,7 +241,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!");
} }

@ -14,6 +14,7 @@ public class BubbleSort {
/** /**
* : O(n²) * : O(n²)
*
* @author Leo * @author Leo
* @date 2020/11/11 10:09 * @date 2020/11/11 10:09
* @param arr * @param arr
@ -60,7 +61,34 @@ public class BubbleSort {
} }
} }
public static void bubbleSort4(int[] arr) { public static void bubbleSort4(int[] arr) {
if (arr.length < 2 || 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 bubbleSort5(int[] arr){
if (arr.length < 2 || 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 bubbleSort6(int[] arr) {
if (arr.length < 2 || arr == null) { if (arr.length < 2 || arr == null) {
return; return;
} }
@ -103,7 +131,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);
bubbleSort4(arr); bubbleSort6(arr);
Arrays.sort(anotherArr); Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) { if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false; succeed = false;

@ -0,0 +1,77 @@
package leo.class01;
/**
* @author Leo
* @ClassName EvenTimesOddTimes
* @DATE 2020/11/13 3:34
* @Description
*/
public class EvenTimesOddTimes {
/**
* :
* @author Leo
* @date 2020/11/13 4:28
* @param arr
* @throw
* @return void
*/
public static void printOdd(int[] arr) {
int eor = 0;
for (int i = 0; i < arr.length; i++) {
eor ^= arr[i];
}
System.out.println(eor);
}
public static void printOdd1(int[] arr) {
int eor = 0;
for (int i = 0; i < arr.length; i++) {
eor ^= arr[i];
}
System.out.println(eor);
}
/**
* : ,
* @author Leo
* @date 2020/11/14 4:21
* @param arr
* @throw
* @return void
*/
public static void printOddTwo(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(eorOther + " " + (eor ^ eorOther));
}
public static void printOddTwo2(int[] arr) {
int eor = 0;
for (int i = 0; i < arr.length; i++) {
eor ^= arr[i];
}
int rightOne = eor & (-eor);
int eorOther = 0;
for (int i = 0; i < arr.length; i++) {
if ((rightOne & arr[i]) != 0) {
eorOther ^= arr[i];
}
}
System.out.println(eorOther + " " + (eor ^ eorOther));
}
}

@ -66,6 +66,29 @@ public class InsertionSort {
} }
} }
public static void insertionSort4(int[] arr) {
if (arr.length < 2 || 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 insertionSort5(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
@ -86,12 +109,12 @@ public class InsertionSort {
public static void main(String[] args){ public static void main(String[] args){
int maxSize = 50; int maxSize = 50;
int range = 80; int range = 80;
int testOfTime = 100000; int testOfTime = 1000;
boolean succeed = true; boolean succeed = true;
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);
insertionSort3(arr); insertionSort5(arr);
Arrays.sort(anotherArr); Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) { if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false; succeed = false;

@ -95,6 +95,33 @@ public class SelectionSort {
} }
public static void selectionSort6(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 selectionSort7(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);
}
}
}
}
/** /**
* : * :
@ -121,12 +148,12 @@ public class SelectionSort {
public static void main(String[] args){ public static void main(String[] args){
int maxSize = 100; int maxSize = 100;
int range = 50; int range = 50;
int testOfTime = 20; int testOfTime = 1000;
boolean succeed = true; boolean succeed = true;
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);
selectionSort5(arr); selectionSort7(arr);
Arrays.sort(anotherArr); Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) { if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false; succeed = false;

Loading…
Cancel
Save