练习class01

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

@ -2,6 +2,7 @@ package leo.class01;
import com.sun.org.apache.xpath.internal.functions.FuncFalse;
import leo.util.ArrayUtil;
import sun.jvm.hotspot.debugger.Page;
import sun.security.util.Length;
/**
@ -188,6 +189,33 @@ public class BSAwesome {
}
public static int BSAwesome5(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;
@ -225,7 +253,7 @@ public class BSAwesome {
boolean succeed = true;
for (int i = 0; i < testTime; i++) {
int[] arr = ArrayUtil.randomAdjacentNotEqualArray(maxSize, range);
final int index = BSAwesome4(arr);
final int index = BSAwesome5(arr);
final int verifyIndex = verifyBSAwesome(arr,index);
if (index != verifyIndex) {
succeed = false;

@ -151,6 +151,27 @@ public class BSExist {
return arr[L] == value;
}
public static boolean exist7(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) {
return true;
} else if (arr[mid] > value) {
R = mid - 1;
} else if (arr[mid] < value) {
L = mid + 1;
}
}
return arr[L] == value;
}
/**
* : test
* @author Leo
@ -179,7 +200,7 @@ public class BSExist {
int[] sortArr = ArrayUtil.randomSortArray(maxSize, range);
int value = (int) ((range + 1) * Math.random() - (range + 1) * Math.random());
if (exist6(sortArr, value) != exist(sortArr, value)) {
if (exist7(sortArr, value) != exist(sortArr, value)) {
succeed = false;
ArrayUtil.printArr(sortArr);
break;

@ -168,6 +168,27 @@ public class BSNear {
return index;
}
public static int BSNearLeft8(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
@ -308,6 +329,26 @@ public class BSNear {
}
public static int BSNearRight6(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) {
@ -330,7 +371,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 = BSNearLeft7(sortArr, value);
final int res1 = BSNearLeft8(sortArr, value);
final int res2 = forTestBSNearLeft(sortArr, value);
if (res1 != res2) {
success = false;
@ -338,8 +379,8 @@ public class BSNear {
System.out.println("BSNearLeft=" + res1);
System.out.println("forTestBSNearLeft=" + res2);
break;
}*/
final int res3 = BSNearRight5(sortArr, value);
}
/*final int res3 = BSNearRight6(sortArr, value);
final int res4 = forTestBSNearRight(sortArr, value);
if (res3 != res4) {
success = false;
@ -347,7 +388,7 @@ public class BSNear {
System.out.println("BSNearRight=" + res3);
System.out.println("forTestBSNearRight=" + res4);
break;
}
}*/
}
System.out.println(success ? "Nice!!" : "Fucking Fucked!");
}

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

@ -1,5 +1,7 @@
package leo.class01;
import sun.applet.Main;
/**
* @author Leo
* @ClassName EvenTimesOddTimes
@ -49,6 +51,15 @@ public class EvenTimesOddTimes {
}
public static void printOdd4(int[] arr) {
int eor = 0;
for (int i = 0; i < arr.length; i++) {
eor ^= arr[i];
}
System.out.println(eor);
}
/**
* : ,
@ -126,6 +137,29 @@ public class EvenTimesOddTimes {
}
public static void printOddTwo5(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));
}
public static void main(String[] args){
int[] arrOne = {1, 1, 5, 5, 8, 1, 8, 5, 5};
printOdd4(arrOne);
int[] arrTwo = {1, 1, 9, 5, 5, 8, 1, 8, 9, 5, 5, 5};
printOddTwo5(arrTwo);
}
}

@ -139,6 +139,31 @@ public class InsertionSort {
}
}
public static void insertionSort10(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 insertionSort11(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
@ -158,12 +183,12 @@ public class InsertionSort {
public static void main(String[] args){
int maxSize = 50;
int range = 80;
int testOfTime = 1000;
int testOfTime = 10000;
boolean succeed = true;
for (int i = 0; i < testOfTime; i++) {
int[] arr = ArrayUtil.randomArray(maxSize, range);
int[] anotherArr = ArrayUtil.copyArray(arr);
insertionSort9(arr);
insertionSort11(arr);
Arrays.sort(anotherArr);
if (!ArrayUtil.isEqual(arr, anotherArr)) {
succeed = false;

@ -84,6 +84,40 @@ public class KM {
return ans;
}
public static int onlyKTimes3(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 j = 0; j < t.length; j++) {
if (t[j] % m != 0) {
ans |= 1 << j;
}
}
return ans;
}
public static int onlyKTime4(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) {
ans |= 1 << i;
}
}
return ans;
}
public static void main(String[] args) {
int maxKinds = 8;
@ -99,9 +133,9 @@ public class KM {
if (k == m) {
m++;
}
int[] arr = randomArray1(maxKinds, range, k, m);
int[] arr = randomArray2(maxKinds, range, k, m);
int ans = onlyKTimes2(arr, k, m);
int ans = onlyKTime4(arr, k, m);
int ans2 = testForOnlyKTimes(arr, k, m);
if (ans != ans2) {
System.out.println(ans);
@ -190,6 +224,39 @@ public class KM {
return arr;
}
private static int[] randomArray2(int maxKinds, int range, int k, int m) {
int kind = (int) (maxKinds * Math.random() + 1);
int arrLength = k + (kind - 1) * m;
int kNumber = randomInt(range);
int[] arr = new int[arrLength];
int index = 0;
HashSet<Integer> set = new HashSet<>();
for (; index < k; index++) {
arr[index] = kNumber;
}
set.add(kNumber);
kind--;
while (kind != 0) {
int curNum;
do {
curNum = randomInt(range);
} while (set.contains(curNum));
for (int i = 0; i < m; i++) {
arr[index++] = curNum;
}
set.add(curNum);
kind--;
}
for (int i = 0; i < arr.length; i++) {
int j = (int) (arrLength * 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));

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

Loading…
Cancel
Save