diff --git a/src/leo/class01/BSAwesome.java b/src/leo/class01/BSAwesome.java index 642ed1c..c6ba945 100644 --- a/src/leo/class01/BSAwesome.java +++ b/src/leo/class01/BSAwesome.java @@ -243,6 +243,32 @@ public class BSAwesome { return -1; } + public static int BSAwesome7(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; @@ -280,7 +306,7 @@ public class BSAwesome { boolean succeed = true; for (int i = 0; i < testTime; i++) { int[] arr = ArrayUtil.randomAdjacentNotEqualArray(maxSize, range); - final int index = BSAwesome6(arr); + final int index = BSAwesome7(arr); final int verifyIndex = verifyBSAwesome(arr,index); if (index != verifyIndex) { succeed = false; diff --git a/src/leo/class01/BSNear.java b/src/leo/class01/BSNear.java index 5208d6d..060bb53 100644 --- a/src/leo/class01/BSNear.java +++ b/src/leo/class01/BSNear.java @@ -209,6 +209,28 @@ public class BSNear { return index; } + + public static int BSNearLeft10(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 @@ -390,6 +412,27 @@ public class BSNear { return index; } + public static int BSNearRight8(int[] arr, int value) { + if (arr == null || arr.length == 0) { + 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; @@ -413,8 +456,8 @@ 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 = BSNearLeft9(sortArr, value); - final int res2 = forTestBSNearLeft(sortArr, value); + /*int res1 = BSNearLeft10(sortArr, value); + int res2 = forTestBSNearLeft(sortArr, value); if (res1 != res2) { success = false; ArrayUtil.printArr(sortArr); @@ -422,8 +465,9 @@ public class BSNear { System.out.println("forTestBSNearLeft=" + res2); break; }*/ - final int res3 = BSNearRight7(sortArr, value); - final int res4 = forTestBSNearRight(sortArr, value); + + int res3 = BSNearRight8(sortArr, value); + int res4 = forTestBSNearRight(sortArr, value); if (res3 != res4) { success = false; ArrayUtil.printArr(sortArr); diff --git a/src/leo/class01/EvenTimesOddTimes.java b/src/leo/class01/EvenTimesOddTimes.java index a8d3805..90f1176 100644 --- a/src/leo/class01/EvenTimesOddTimes.java +++ b/src/leo/class01/EvenTimesOddTimes.java @@ -71,6 +71,14 @@ public class EvenTimesOddTimes { } + public static void printOdd6(int[] arr) { + int eor = 0; + for (int i = 0; i < arr.length; i++) { + eor ^= arr[i]; + } + System.out.println(eor); + } + /** * 功能描述 : 有两种数出现了奇数次,找出他 @@ -179,12 +187,31 @@ public class EvenTimesOddTimes { System.out.println(eorOther + " " + (eor ^ eorOther)); } + public static void printOddTwo7(int[] arr) { + if (arr.length == 0) { + System.out.println("arr length is zero"); + } + int eor = 0; + for (int i = 0; i < arr.length; i++) { + eor ^= arr[i]; + } + 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}; - printOdd5(arrOne); + printOdd6(arrOne); int[] arrTwo = {1, 1, 9, 5, 5, 8, 1, 8, 9, 5, 5, 5}; - printOddTwo6(arrTwo); + printOddTwo7(arrTwo); } diff --git a/src/leo/class02/DeleteGivenValue.java b/src/leo/class02/DeleteGivenValue.java index 7a96af8..6ef2b2a 100644 --- a/src/leo/class02/DeleteGivenValue.java +++ b/src/leo/class02/DeleteGivenValue.java @@ -105,6 +105,27 @@ public class DeleteGivenValue { } return head; } + + public static Node removeNodeOfValue3(Node head, int value) { + while (head != null) { + if (head.value != value) { + break; + } + head = head.next; + } + Node pre = head; + Node cur = head; + while (cur != null) { + if (cur.value == value) { + pre.next = cur.next; + }else { + pre = head; + } + cur = cur.next; + } + return head; + } + /** * 功能描述 : 双链表删除某个给定值 * @author Leo @@ -245,7 +266,6 @@ public class DeleteGivenValue { return head; } - public static DoubleNode removeDoubleNodeOfValue5(DoubleNode head, int value) { while (head != null) { if (head.value != value) { @@ -379,7 +399,7 @@ public class DeleteGivenValue { for (int i = 0; i < testTime; i++) { int value = randomInt(range); Node nodeHead = randomNode(sizeMax, range); - Node node = removeNodeOfValue2(nodeHead, value); + Node node = removeNodeOfValue3(nodeHead, value); if (!verifyRemoveNodeOfValue(node,value)) { System.out.println("node fuck!"); diff --git a/src/leo/class02/GetMax.java b/src/leo/class02/GetMax.java index 946ed5b..6b88146 100644 --- a/src/leo/class02/GetMax.java +++ b/src/leo/class02/GetMax.java @@ -27,6 +27,20 @@ public class GetMax { return Math.max(leftMax, rightMax); } + public static int getMax1(int[] arr) { + + return process1(arr, 0, arr.length - 1); + } + + private static int process1(int[] arr, int l, int r) { + if (l == r) { + return arr[l]; + } + int mid = l + ((r - l) >> 1); + int leftMax = process1(arr, 0, mid); + int rightMax = process1(arr, mid+1, r); + return Math.max(leftMax, rightMax); + } private static int testGetMax(int[] arr) { Arrays.sort(arr); @@ -34,14 +48,14 @@ public class GetMax { } public static void main(String[] args){ - int maxSize = 10; + int maxSize = 5; int range = 100; - int testTime = 100; + int testTime = 5; System.out.println("Start!"); for (int i = 0; i < testTime; i++) { int[] arr = ArrayUtil.randomAdjacentNotEqualArray(maxSize, range); - int max = getMax(arr); + int max = getMax1(arr); int testMax = testGetMax(arr); if (max != testMax) { System.out.println("fuck"); diff --git a/src/leo/class02/GetMinStack.java b/src/leo/class02/GetMinStack.java index d05ce8b..32e6fdd 100644 --- a/src/leo/class02/GetMinStack.java +++ b/src/leo/class02/GetMinStack.java @@ -1,5 +1,7 @@ package leo.class02; +import jdk.nashorn.internal.ir.IfNode; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -89,6 +91,49 @@ public class GetMinStack { return min.peek(); } + } + + public static class MyStack5{ + private Stack data; + private Stack min; + + public MyStack5() { + data = new Stack(); + min = new Stack(); + } + + public void push(int value) { + try { + if (data.isEmpty() || this.getMin() >= value) { + min.push(value); + } + } catch (Exception e) { + min.push(value); + } + data.push(value); + } + + public int pop() throws Exception { + if (data.isEmpty()) { + throw new Exception("stack is empty"); + } + int value = data.pop(); + if (value == this.getMin()) { + min.pop(); + } + return value; + } + + + public int getMin() throws Exception { + if (min.isEmpty()) { + throw new Exception("stack is empty"); + } + return min.peek(); + } + + + } /** @@ -172,6 +217,46 @@ public class GetMinStack { } } + public static class MyStack6 { + private Stack data; + private Stack min; + + public MyStack6() { + this.data = new Stack<>(); + this.min = new Stack<>(); + } + + public void push(int value) { + try { + if (this.min.isEmpty() || this.getMin() > value) { + this.min.push(value); + } else { + this.min.push(this.getMin()); + } + } catch (Exception e) { + this.min.push(value); + } + data.push(value); + } + + public int pop() throws Exception { + if (this.data.isEmpty()) { + throw new Exception("stack is empty"); + } + int value = this.data.pop(); + this.min.pop(); + return value; + } + + public int getMin() throws Exception { + if (this.min.isEmpty()) { + throw new Exception("stack is empty"); + } + return this.min.peek(); + } + + } + /** * 用list实现 只存最小值的索引 */ @@ -262,7 +347,7 @@ public class GetMinStack { System.out.println("Start!"); for (int i = 0; i < testTime; i++) { - MyStack4 myStack = new MyStack4(); + MyStack5 myStack = new MyStack5(); TestMyStack testMyStack = new TestMyStack(); for (int j = 0; j < forTime; j++) { if (Math.random() < 0.5) { @@ -275,7 +360,7 @@ public class GetMinStack { int testMyStackValue = testMyStack.pop(); if (myStack1Value != testMyStackValue) { System.out.println("myStack1Value: "+myStack1Value); - System.out.println("testMyStackMin: "+testMyStackValue); + System.out.println("testMyStackValue: "+testMyStackValue); return; } } catch (Exception e) { diff --git a/src/leo/class02/LinkedList.java b/src/leo/class02/LinkedList.java index 14e9452..b49108a 100644 --- a/src/leo/class02/LinkedList.java +++ b/src/leo/class02/LinkedList.java @@ -119,7 +119,7 @@ public class LinkedList { } public static Node reverseNode6(Node head) { - Node pre = head; + Node pre = null; Node next = null; while (head != null) { next = head.next; @@ -130,9 +130,8 @@ public class LinkedList { return pre; } - public static Node reverseNode7(Node head) { - Node pre = head; + Node pre = null; Node next = null; while (head != null) { next = head.next; @@ -144,7 +143,19 @@ public class LinkedList { } public static Node reverseNode8(Node head) { - Node pre = head; + Node pre = null; + Node next = null; + while (head != null) { + next = head.next; + head.next = pre; + pre = head; + head = next; + } + return pre; + } + + public static Node reverseNode9(Node head) { + Node pre = null; Node next = null; while (head != null) { next = head.next; @@ -208,7 +219,6 @@ public class LinkedList { } - public static DoubleNode reverseDoubleNode4(DoubleNode head) { DoubleNode pre = head; DoubleNode next = null; @@ -222,6 +232,19 @@ public class LinkedList { return pre; } + public static DoubleNode reverseDoubleNode5(DoubleNode head) { + DoubleNode pre = null; + DoubleNode next = null; + while (head != null) { + next = head.next; + head.next = pre; + head.pre = next; + pre = head; + head = next; + } + return pre; + } + /** * 功能描述 : 随机生成单链表 * @author Leo @@ -345,21 +368,21 @@ public class LinkedList { } public static void main(String[] args){ - int maxSize = 40; + int maxSize = 5; int range = 90; int testTime = 10000; System.out.println("测试开始"); for (int i = 0; i < testTime; i++) { Node head = randomNode(maxSize, range); List nodeList = nodeToList(head); - Node node = reverseNode8(head); + Node node = reverseNode9(head); if (!verifyReverseListAndNode(nodeList, node)) { System.out.println("nodeFuck!!"); break; } DoubleNode doubleNodeHead = randomDoubleNode(maxSize, range); List doubleNodeList = DoubleNodeToList(doubleNodeHead); - DoubleNode doubleNode = reverseDoubleNode4(doubleNodeHead); + DoubleNode doubleNode = reverseDoubleNode5(doubleNodeHead); if (!verifyReverseListAndDoubleNode(doubleNodeList, doubleNode)) { System.out.println("doubleNodeFuck!!"); break; diff --git a/src/leo/class02/MergeSort.java b/src/leo/class02/MergeSort.java new file mode 100644 index 0000000..e26d334 --- /dev/null +++ b/src/leo/class02/MergeSort.java @@ -0,0 +1,304 @@ +package leo.class02; + + +import leo.util.ArrayUtil; + +import java.util.Arrays; + + +/** + * @author Leo + * @ClassName MergeSort + * @DATE 2020/11/21 9:21 下午 + * @Description 归并排序 + */ +public class MergeSort { + + + /** + * @author Leo + * @ClassName MergeSort + * @DATE 2020/11/21 9:21 下午 + * @Description 递归版归并排序 + */ + public static class Recursion { + + /** + * 功能描述 : 递归版归并排序 + * + * @param arr + * @return void + * @author Leo + * @date 2020/11/21 9:23 下午 + */ + public static void mergeSort(int[] arr) { + if (arr == null || arr.length < 2) { + return; + } + process(arr, 0, arr.length - 1); + } + + private static void process(int[] arr, int L, int R) { + if (L == R) { + return; + } + int mid = L + ((R - L) >> 1); + process(arr, L, mid); + process(arr, mid + 1, R); + merge(arr, L, mid, R); + } + + /** + * 功能描述 : 合并 + * + * @param arr 数组 + * @param L 左面第一个下标 + * @param M 中间下标 + * @param R 右面最后一个下标 + * @return void + * @author Leo + * @date 2020/11/21 9:28 下午 + */ + private static void merge(int[] arr, int L, int M, int R) { + int[] help = new int[R - L + 1]; + int i = 0; + int p1 = L; + int p2 = M + 1; + while (p1 <= M && p2 <= R) { + help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++]; + } + while (p1 <= M) { + help[i++] = arr[p1++]; + } + while (p2 <= R) { + help[i++] = arr[p2++]; + } + + for (i = 0; i < help.length; i++) { + arr[L + i] = help[i]; + } + + } + + } + + public static class Recursion1 { + public static void mergeSort(int[] arr) { + if (arr.length < 2 || arr == null) { + return; + } + process(arr, 0, arr.length - 1); + + } + + private static void process(int[] arr, int L, int R) { + if (L == R) { + return; + } + int M = L + ((R - L) >> 1); + process(arr, L, M); + process(arr, M + 1, R); + merge(arr, L, M, R); + } + + private static void merge(int[] arr, int L, int M, int R) { + int p1 = L; + int p2 = M + 1; + int i = 0; + int[] help = new int[R - L + 1]; + while (p1 <= M && p2 <= R) { + help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++]; + } + while (p1 <= M) { + help[i++] = arr[p1++]; + } + while (p2 <= R) { + help[i++] = arr[p2++]; + } + for (i = 0; i < help.length; i++) { + arr[L + i] = help[i]; + } + + } + + + } + + public static class Recursion2 { + public static void mergeSort(int[] arr) { + if (arr.length < 2 || arr == null) { + return; + } + process(arr, 0, arr.length - 1); + } + + private static void process(int[] arr, int l, int r) { + if (l == r) { + return; + } + int mid = l + ((r - l) >> 1); + process(arr, l, mid); + process(arr, mid + 1, r); + merge(arr, l, mid, r); + } + + private static void merge(int[] arr, int l, int mid, int r) { + if (l == r) { + return; + } + int p1 = l; + int p2 = mid + 1; + int[] help = new int[r - l + 1]; + int i = 0; + while (p1 <= mid && p2 <= r) { + help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++]; + } + while (p1 <= mid) { + help[i++] = arr[p1++]; + } + while (p2 <= r) { + help[i++] = arr[p2++]; + } + for (i = 0; i < help.length; i++) { + arr[l + i] = help[i]; + } + } + } + + /** + * @author Leo + * @ClassName MergeSort + * @DATE 2020/11/21 9:21 下午 + * @Description 非递归版归并排序 + */ + public static class NonRecursive{ + + public static void mergeSort(int[] arr) { + if (arr.length < 2 || arr == null) { + return; + } + int N = arr.length; + int mergeSize = 1; + while (mergeSize < N) { + int L = 0; + while (L < N) { + int M = mergeSize + L - 1; + if (M >= N) { + break; + } + int R = Math.min(M + mergeSize, N - 1); + merge(arr, L, M, R); + L = R + 1; + } + if (mergeSize > N / 2) { + break; + } + mergeSize <<= 1; + + } + + } + private static void merge(int[] arr, int L, int M, int R) { + if (L == R) { + return; + } + int[] help = new int[R - L + 1]; + int p1 = L; + int p2 = M + 1; + int i = 0; + while (p1 <= M && p2 <= R) { + help[i++] = arr[p1] <= arr[p2] ? arr[p1++] : arr[p2++]; + } + while (p1 <= M) { + help[i++] = arr[p1++]; + } + while (p2 <= R) { + help[i++] = arr[p2++]; + } + for (i = 0; i < help.length; i++) { + arr[L + i] = help[i]; + } + } + } + + public static class NonRecursive1 { + + public static void mergeSort(int[] arr) { + if (arr.length < 2 || arr == null) { + return; + } + int arrLength = arr.length; + int mergeSize = 1; + + while (mergeSize < arrLength) { + int L = 0; + while (L < arrLength) { + int M = L + mergeSize - 1; + if (M >= arrLength) { + break; + } + int R = Math.min(M + mergeSize, arrLength - 1); + merge(arr, L, M, R); + L = R + 1; + } + //防越界 + if (mergeSize > arrLength / 2) { + break; + } + mergeSize <<= 1; + } + + } + + private static void merge(int[] arr, int l, int m, int r) { + if (l == r) { + return; + } + int pl = l; + int pr = m + 1; + int[] help = new int[r - l + 1]; + int i = 0; + while (pl <= m && pr <= r) { + help[i++] = arr[pl] <= arr[pr] ? arr[pl++] : arr[pr++]; + } + while (pl <= m) { + help[i++] = arr[pl++]; + } + while (pr <= r) { + help[i++] = arr[pr++]; + } + for (i = 0; i < help.length; i++) { + arr[l + i] = help[i]; + } + } + + } + + public static void main(String[] args){ + int maxSize = 50; + int range = 80; + int testOfTime = 10000; + boolean succeed = true; + for (int i = 0; i < testOfTime; i++) { + int[] arr = ArrayUtil.randomArray(maxSize, range); + int[] anotherArr = ArrayUtil.copyArray(arr); + Recursion2.mergeSort(arr); + Arrays.sort(anotherArr); + if (!ArrayUtil.isEqual(arr, anotherArr)) { + succeed = false; + ArrayUtil.printArr(arr, "arr"); + ArrayUtil.printArr(anotherArr, "anotherArr"); + break; + } + } + System.out.println(succeed ? "Nice!" : "Fucking fucked!"); + + + } + + + + +} + + diff --git a/src/leo/class02/RingArray.java b/src/leo/class02/RingArray.java index 486a274..efd7b26 100644 --- a/src/leo/class02/RingArray.java +++ b/src/leo/class02/RingArray.java @@ -100,13 +100,54 @@ public class RingArray { } } + public static class MyQueue2{ + private int push; + private int poll; + private int size; + private final int limit; + private int[] arr; + public MyQueue2(int limit) { + this.push = 0; + this.poll = 0; + this.size = 0; + this.arr = new int[limit]; + this.limit = limit; + } + + public void push(int value) { + if (size == limit) { + throw new RuntimeException("队列满了!"); + } + this.arr[this.push] = value; + this.size++; + this.push = nextIndex(this.push); + } + + public int poll() { + if (size == 0) { + throw new RuntimeException("队列空了!"); + } + int value = this.arr[poll]; + size--; + this.poll = nextIndex(poll); + return value; + } + + public boolean isEmpty() { + return size == 0; + } + private int nextIndex(int i) { + return i < limit-1 ? i+1 : 0; + } + } + public static void main(String[] args) { int testTime = 10000; int range = 100; int sizeMax = 80; for (int i = 0; i < testTime; i++) { int length = randomInt(sizeMax); - MyQueue1 myQueue = new MyQueue1(length); + MyQueue2 myQueue = new MyQueue2(length); Queue queue = new LinkedList<>(); for (int j = 0; j < length; j++) { int value = randomInt(range); diff --git a/src/leo/class02/SmallSum.java b/src/leo/class02/SmallSum.java new file mode 100644 index 0000000..a0918e4 --- /dev/null +++ b/src/leo/class02/SmallSum.java @@ -0,0 +1,166 @@ +package leo.class02; + +import leo.util.ArrayUtil; + +/** + * @author Leo + * @ClassName SmallSum + * @DATE 2020/11/23 10:46 上午 + * @Description 小和问题 + */ +class SmallSum { + + public static int smallSum(int[] arr) { + if (arr.length == 0 || arr == null) { + return 0; + } + return process(arr, 0, arr.length - 1); + } + + private static int process(int[] arr, int L, int R) { + + if (L == R) { + return 0; + } + int mid = L + ((R - L) >> 1); + + return process(arr, L, mid) + + process(arr, mid + 1, R) + + merge(arr, L, mid, R); + } + + /** + * 左<右,小和=(右总长-有下标+1)*左; + * 左<右 左进入help数组,左移动下标, + * 左等于右或左大于右,右进help数组,右移动下标. + */ + private static int merge(int[] arr, int l, int m, int r) { + int[] help = new int[r - l + 1]; + int p1 = l; + int p2 = m + 1; + int res = 0; + int i = 0; + while (p1 <= m && p2 <= r) { + res += arr[p1] < arr[p2] ? (r - p2 + 1) * arr[p1] : 0; + help[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++]; + } + while (p1 <= m) { + help[i++] = arr[p1++]; + } + while (p2 <= r) { + help[i++] = arr[p2++]; + } + for (i = 0; i < help.length; i++) { + arr[l + i] = help[i]; + } + return res; + + } + + +} + +class SmallSum1{ + + public static int smallSum(int[] arr) { + if (arr.length < 2 || arr == null) { + return 0; + } + return process(arr, 0, arr.length - 1); + } + + private static int process(int[] arr, int l, int r) { + + if (l == r) { + return 0; + } + int mid = l + ((r - l) >> 1); + return process(arr, l, mid) + + process(arr, mid + 1, r) + + merge(arr, l, mid, r); + } + + private static int merge(int[] arr, int l, int m, int r) { + int p1 = l; + int p2 = m+1; + int i = 0; + int[] help = new int[r - l + 1]; + int res = 0; + while (p1 <= m && p2 <= r) { + res += arr[p1] < arr[p2] ? (r - p2 + 1) * arr[p1] : 0; + help[i++] = arr[p1] < arr[p2] ? arr[p1++] : arr[p2++]; + } + while (p1 <= m) { + help[i++] = arr[p1++]; + } + while (p2 <= r) { + help[i++] = arr[p2++]; + } + for (i = 0; i < help.length; i++) { + arr[l + i] = help[i]; + } + return res; + } + +} + +class TestSmallSum{ + public static int smallSum(int[] arr) { + if (arr.length < 2 || arr == null) { + return 0; + } + int res = 0; + for (int i = 1; i < arr.length; i++) { + for (int j = 0; j < i; j++) { + res += arr[j] < arr[i] ? arr[j] : 0; + } + } + 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); + } + } + } + return res; + } + + + public static void swap(int[] arr, int i, int j) { + if (i == j||arr[i]==arr[j]) { + return; + } + arr[i] = arr[i] ^ arr[j]; + arr[j] = arr[i] ^ arr[j]; + arr[i] = arr[i] ^ arr[j]; + } +} + +class TestMain { + + public static void main(String[] args){ + int testTime = 1000; + int range = 50; + int maxSize = 100; + System.out.println("start!"); + + for (int i = 0; i < testTime; i++) { + int[] arr = ArrayUtil.randomArray(maxSize, range); + int[] copyArray = ArrayUtil.copyArray(arr); + int sum = SmallSum1.smallSum(arr); + int testSum = TestSmallSum.smallSum(copyArray); + if (testSum != sum) { + System.out.println("sum :" + sum + ", testSum : " + testSum); + break; + } + if (!ArrayUtil.isEqual(arr, copyArray)) { + System.out.println("arr :" + arr ); + System.out.println("copyArray : " + copyArray); + break; + } + } + System.out.println("end!"); + + + } +}