parent
6ac967d999
commit
0dfd08357a
@ -1,115 +0,0 @@
|
||||
package class001;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Code02_ContainAllCharExactly {
|
||||
|
||||
public static int containExactly1(String s, String a) {
|
||||
if (s == null || a == null || s.length() < a.length()) {
|
||||
return -1;
|
||||
}
|
||||
char[] aim = a.toCharArray();
|
||||
Arrays.sort(aim);
|
||||
String aimSort = String.valueOf(aim);
|
||||
for (int L = 0; L < s.length(); L++) {
|
||||
for (int R = L; R < s.length(); R++) {
|
||||
char[] cur = s.substring(L, R + 1).toCharArray();
|
||||
Arrays.sort(cur);
|
||||
String curSort = String.valueOf(cur);
|
||||
if (curSort.equals(aimSort)) {
|
||||
return L;
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static int containExactly2(String s, String a) {
|
||||
if (s == null || a == null || s.length() < a.length()) {
|
||||
return -1;
|
||||
}
|
||||
char[] str = s.toCharArray();
|
||||
char[] aim = a.toCharArray();
|
||||
for (int L = 0; L <= str.length - aim.length; L++) {
|
||||
if (isCountEqual(str, L, aim)) {
|
||||
return L;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static boolean isCountEqual(char[] str, int L, char[] aim) {
|
||||
int[] count = new int[256];
|
||||
for (int i = 0; i < aim.length; i++) {
|
||||
count[aim[i]]++;
|
||||
}
|
||||
for (int i = 0; i < aim.length; i++) {
|
||||
if (count[str[L + i]]-- == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static int containExactly3(String s, String a) {
|
||||
if (s == null || a == null || s.length() < a.length()) {
|
||||
return -1;
|
||||
}
|
||||
char[] aim = a.toCharArray();
|
||||
int[] count = new int[256];
|
||||
for (int i = 0; i < aim.length; i++) {
|
||||
count[aim[i]]++;
|
||||
}
|
||||
int M = aim.length;
|
||||
char[] str = s.toCharArray();
|
||||
int inValidTimes = 0;
|
||||
int R = 0;
|
||||
for (; R < M; R++) {
|
||||
if (count[str[R]]-- <= 0) {
|
||||
inValidTimes++;
|
||||
}
|
||||
}
|
||||
for (; R < str.length; R++) {
|
||||
if (inValidTimes == 0) {
|
||||
return R - M;
|
||||
}
|
||||
if (count[str[R]]-- <= 0) {
|
||||
inValidTimes++;
|
||||
}
|
||||
if (count[str[R - M]]++ < 0) {
|
||||
inValidTimes--;
|
||||
}
|
||||
}
|
||||
return inValidTimes == 0 ? R - M : -1;
|
||||
}
|
||||
|
||||
// for test
|
||||
public static String getRandomString(int possibilities, int maxSize) {
|
||||
char[] ans = new char[(int) (Math.random() * maxSize) + 1];
|
||||
for (int i = 0; i < ans.length; i++) {
|
||||
ans[i] = (char) ((int) (Math.random() * possibilities) + 'a');
|
||||
}
|
||||
return String.valueOf(ans);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int possibilities = 5;
|
||||
int strMaxSize = 20;
|
||||
int aimMaxSize = 5;
|
||||
int testTimes = 500000;
|
||||
System.out.println("test begin, test time : " + testTimes);
|
||||
for (int i = 0; i < testTimes; i++) {
|
||||
String str = getRandomString(possibilities, strMaxSize);
|
||||
String aim = getRandomString(possibilities, aimMaxSize);
|
||||
int ans1 = containExactly1(str, aim);
|
||||
int ans2 = containExactly2(str, aim);
|
||||
int ans3 = containExactly3(str, aim);
|
||||
if (ans1 != ans2 || ans2 != ans3) {
|
||||
System.out.println("Oops!");
|
||||
}
|
||||
}
|
||||
System.out.println("test finish");
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
package class002;
|
||||
|
||||
public class Code01_MakeNo {
|
||||
|
||||
public static int[] makeNo(int size) {
|
||||
if (size == 1) {
|
||||
return new int[] { 1 };
|
||||
}
|
||||
int halfSize = (size + 1) / 2;
|
||||
int[] base = makeNo(halfSize);
|
||||
int[] ans = new int[size];
|
||||
int index = 0;
|
||||
for (; index < halfSize; index++) {
|
||||
ans[index] = base[index] * 2 + 1;
|
||||
}
|
||||
for (int i = 0; index < size; index++, i++) {
|
||||
ans[index] = base[i] * 2;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
// 检验函数
|
||||
public static boolean isValid(int[] arr) {
|
||||
int N = arr.length;
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int k = i + 1; k < N; k++) {
|
||||
for (int j = k + 1; j < N; j++) {
|
||||
if (arr[i] + arr[j] == 2 * arr[k]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("test begin");
|
||||
for (int N = 1; N < 1000; N++) {
|
||||
int[] arr = makeNo(N);
|
||||
if (!isValid(arr)) {
|
||||
System.out.println("Oops!");
|
||||
}
|
||||
}
|
||||
System.out.println("test end");
|
||||
System.out.println(isValid(makeNo(1042)));
|
||||
System.out.println(isValid(makeNo(2981)));
|
||||
}
|
||||
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
package class005;
|
||||
|
||||
public class Code01_RotateString {
|
||||
|
||||
public static String rotate1(String s, int leftSize) {
|
||||
if (leftSize <= 0 || leftSize >= s.length()) {
|
||||
return s;
|
||||
}
|
||||
return process1(s.toCharArray(), 0, leftSize - 1, s.length() - 1);
|
||||
}
|
||||
|
||||
public static String process1(char[] str, int L, int M, int R) {
|
||||
reverse(str, L, M);
|
||||
reverse(str, M + 1, R);
|
||||
reverse(str, L, R);
|
||||
return String.valueOf(str);
|
||||
}
|
||||
|
||||
public static void reverse(char[] str, int L, int R) {
|
||||
while (L < R) {
|
||||
char tmp = str[L];
|
||||
str[L++] = str[R];
|
||||
str[R--] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
public static String rotate2(String s, int leftSize) {
|
||||
if (leftSize <= 0 || leftSize >= s.length()) {
|
||||
return s;
|
||||
}
|
||||
char[] str = s.toCharArray();
|
||||
int L = 0;
|
||||
int R = str.length - 1;
|
||||
int lpart = leftSize;
|
||||
int rpart = str.length - leftSize;
|
||||
int same = Math.min(lpart, rpart);
|
||||
int diff = lpart - rpart;
|
||||
exchange(str, L, R, same);
|
||||
while (diff != 0) {
|
||||
if (diff > 0) {
|
||||
L += same;
|
||||
lpart = diff;
|
||||
} else {
|
||||
R -= same;
|
||||
rpart = -diff;
|
||||
}
|
||||
same = Math.min(lpart, rpart);
|
||||
diff = lpart - rpart;
|
||||
exchange(str, L, R, same);
|
||||
}
|
||||
return String.valueOf(str);
|
||||
}
|
||||
|
||||
public static void exchange(char[] chas, int start, int end, int size) {
|
||||
int i = end - size + 1;
|
||||
char tmp = 0;
|
||||
while (size-- != 0) {
|
||||
tmp = chas[start];
|
||||
chas[start] = chas[i];
|
||||
chas[i] = tmp;
|
||||
start++;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// for test
|
||||
public static String getRandomString(int possibilities, int strMaxSize) {
|
||||
char[] ans = new char[(int) (Math.random() * strMaxSize) + 1];
|
||||
for (int i = 0; i < ans.length; i++) {
|
||||
ans[i] = (char) ((int) (Math.random() * possibilities) + 'a');
|
||||
}
|
||||
return String.valueOf(ans);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int possibilities = 5;
|
||||
int strMaxSize = 10;
|
||||
int testTimes = 5000000;
|
||||
System.out.println("test begin, test time : " + testTimes);
|
||||
for (int i = 0; i < testTimes; i++) {
|
||||
String str = getRandomString(possibilities, strMaxSize);
|
||||
int leftSize = (int) (Math.random() * (str.length() + 1));
|
||||
String ans1 = rotate1(str, leftSize);
|
||||
String ans2 = rotate2(str, leftSize);
|
||||
if (!ans1.equals(ans2)) {
|
||||
System.out.println("Oops!");
|
||||
}
|
||||
}
|
||||
System.out.println("test finish");
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package class008;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Code02_LongestConsecutiveSequence {
|
||||
|
||||
public static int longestConsecutive(int[] nums) {
|
||||
HashMap<Integer, Integer> map = new HashMap<>();
|
||||
int len = 0;
|
||||
for (int num : nums) {
|
||||
if (!map.containsKey(num)) {
|
||||
map.put(num, 1);
|
||||
int preLen = map.containsKey(num - 1) ? map.get(num - 1) : 0;
|
||||
int posLen = map.containsKey(num + 1) ? map.get(num + 1) : 0;
|
||||
int all = preLen + posLen + 1;
|
||||
map.put(num - preLen, all);
|
||||
map.put(num + posLen, all);
|
||||
len = Math.max(len, all);
|
||||
}
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package class010;
|
||||
|
||||
public class Code01_SwapWithoutTmp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int a = 111;
|
||||
int b = 111;
|
||||
System.out.println(a);
|
||||
System.out.println(b);
|
||||
a = a ^ b;
|
||||
b = a ^ b;
|
||||
a = a ^ b;
|
||||
System.out.println(a);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
}
|
@ -1,133 +0,0 @@
|
||||
package class011;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Code01_CopyListWithRandom {
|
||||
|
||||
public static class Node {
|
||||
public int value;
|
||||
public Node next;
|
||||
public Node rand;
|
||||
|
||||
public Node(int data) {
|
||||
this.value = data;
|
||||
}
|
||||
}
|
||||
|
||||
public static Node copyListWithRand1(Node head) {
|
||||
HashMap<Node, Node> map = new HashMap<Node, Node>();
|
||||
Node cur = head;
|
||||
while (cur != null) {
|
||||
map.put(cur, new Node(cur.value));
|
||||
cur = cur.next;
|
||||
}
|
||||
cur = head;
|
||||
while (cur != null) {
|
||||
// cur 老
|
||||
// map.get(cur) 新
|
||||
map.get(cur).next = map.get(cur.next);
|
||||
map.get(cur).rand = map.get(cur.rand);
|
||||
cur = cur.next;
|
||||
}
|
||||
return map.get(head);
|
||||
}
|
||||
|
||||
public static Node copyListWithRand2(Node head) {
|
||||
if (head == null) {
|
||||
return null;
|
||||
}
|
||||
Node cur = head;
|
||||
Node next = null;
|
||||
// copy node and link to every node
|
||||
// 1 -> 2
|
||||
// 1 -> 1' -> 2
|
||||
while (cur != null) {
|
||||
// 1 -> 2 -> 3
|
||||
// cur next
|
||||
// 1 -> 1' -> 2 -> 2'
|
||||
next = cur.next;
|
||||
cur.next = new Node(cur.value);
|
||||
cur.next.next = next;
|
||||
cur = next;
|
||||
}
|
||||
cur = head;
|
||||
Node curCopy = null;
|
||||
// set copy node rand
|
||||
// 1 -> 1' -> 2 -> 2'
|
||||
while (cur != null) {
|
||||
// 1 -> 1' -> 2 -> 2'
|
||||
// cur next
|
||||
// cur 老 cur.next 新
|
||||
next = cur.next.next;
|
||||
curCopy = cur.next;
|
||||
curCopy.rand = cur.rand != null ? cur.rand.next : null;
|
||||
cur = next;
|
||||
}
|
||||
// head head.next
|
||||
Node res = head.next;
|
||||
cur = head;
|
||||
// split
|
||||
while (cur != null) {
|
||||
next = cur.next.next;
|
||||
curCopy = cur.next;
|
||||
cur.next = next;
|
||||
curCopy.next = next != null ? next.next : null;
|
||||
cur = next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void printRandLinkedList(Node head) {
|
||||
Node cur = head;
|
||||
System.out.print("order: ");
|
||||
while (cur != null) {
|
||||
System.out.print(cur.value + " ");
|
||||
cur = cur.next;
|
||||
}
|
||||
System.out.println();
|
||||
cur = head;
|
||||
System.out.print("rand: ");
|
||||
while (cur != null) {
|
||||
System.out.print(cur.rand == null ? "- " : cur.rand.value + " ");
|
||||
cur = cur.next;
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Node head = null;
|
||||
Node res1 = null;
|
||||
Node res2 = null;
|
||||
printRandLinkedList(head);
|
||||
res1 = copyListWithRand1(head);
|
||||
printRandLinkedList(res1);
|
||||
res2 = copyListWithRand2(head);
|
||||
printRandLinkedList(res2);
|
||||
printRandLinkedList(head);
|
||||
System.out.println("=========================");
|
||||
|
||||
head = new Node(1);
|
||||
head.next = new Node(2);
|
||||
head.next.next = new Node(3);
|
||||
head.next.next.next = new Node(4);
|
||||
head.next.next.next.next = new Node(5);
|
||||
head.next.next.next.next.next = new Node(6);
|
||||
|
||||
head.rand = head.next.next.next.next.next; // 1 -> 6
|
||||
head.next.rand = head.next.next.next.next.next; // 2 -> 6
|
||||
head.next.next.rand = head.next.next.next.next; // 3 -> 5
|
||||
head.next.next.next.rand = head.next.next; // 4 -> 3
|
||||
head.next.next.next.next.rand = null; // 5 -> null
|
||||
head.next.next.next.next.next.rand = head.next.next.next; // 6 -> 4
|
||||
|
||||
printRandLinkedList(head);
|
||||
res1 = copyListWithRand1(head);
|
||||
printRandLinkedList(res1);
|
||||
res2 = copyListWithRand2(head);
|
||||
printRandLinkedList(res2);
|
||||
printRandLinkedList(head);
|
||||
System.out.println("=========================");
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package class013;
|
||||
|
||||
public class Code01_PalindromeNumber {
|
||||
|
||||
public static boolean isPalindrome(int n) {
|
||||
if (n < 0) {
|
||||
return false;
|
||||
}
|
||||
n = Math.abs(n);
|
||||
int help = 1;
|
||||
while (n / help >= 10) {
|
||||
help *= 10;
|
||||
}
|
||||
while (n != 0) {
|
||||
if (n / help != n % 10) {
|
||||
return false;
|
||||
}
|
||||
n = (n % help) / 10;
|
||||
help /= 100;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
@ -1,70 +0,0 @@
|
||||
package class013;
|
||||
|
||||
public class Code03_LongestNoRepeatSubstring {
|
||||
|
||||
public static int maxUnique(String str) {
|
||||
if (str == null || str.equals("")) {
|
||||
return 0;
|
||||
}
|
||||
char[] chas = str.toCharArray();
|
||||
// map 替代了哈希表 假设字符的码是0~255
|
||||
int[] map = new int[256];
|
||||
for (int i = 0; i < 256; i++) {
|
||||
map[i] = -1;
|
||||
}
|
||||
int len = 0;
|
||||
int pre = -1;
|
||||
int cur = 0;
|
||||
for (int i = 0; i != chas.length; i++) {
|
||||
pre = Math.max(pre, map[chas[i]]);
|
||||
cur = i - pre;
|
||||
len = Math.max(len, cur);
|
||||
map[chas[i]] = i;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
// for test
|
||||
public static String getRandomString(int len) {
|
||||
char[] str = new char[len];
|
||||
int base = 'a';
|
||||
int range = 'z' - 'a' + 1;
|
||||
for (int i = 0; i != len; i++) {
|
||||
str[i] = (char) ((int) (Math.random() * range) + base);
|
||||
}
|
||||
return String.valueOf(str);
|
||||
}
|
||||
|
||||
// for test
|
||||
public static String maxUniqueString(String str) {
|
||||
if (str == null || str.equals("")) {
|
||||
return str;
|
||||
}
|
||||
char[] chas = str.toCharArray();
|
||||
int[] map = new int[256];
|
||||
for (int i = 0; i < 256; i++) {
|
||||
map[i] = -1;
|
||||
}
|
||||
int len = -1;
|
||||
int pre = -1;
|
||||
int cur = 0;
|
||||
int end = -1;
|
||||
for (int i = 0; i != chas.length; i++) {
|
||||
pre = Math.max(pre, map[chas[i]]);
|
||||
cur = i - pre;
|
||||
if (cur > len) {
|
||||
len = cur;
|
||||
end = i;
|
||||
}
|
||||
map[chas[i]] = i;
|
||||
}
|
||||
return str.substring(end - len + 1, end + 1);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String str = getRandomString(20);
|
||||
System.out.println(str);
|
||||
System.out.println(maxUnique(str));
|
||||
System.out.println(maxUniqueString(str));
|
||||
}
|
||||
}
|
@ -1,88 +0,0 @@
|
||||
package class015;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Code01_CordCoverMaxPoint {
|
||||
|
||||
public static int maxPoint1(int[] arr, int L) {
|
||||
int res = 1;
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
int nearest = nearestIndex(arr, i, arr[i] - L);
|
||||
res = Math.max(res, i - nearest + 1);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static int nearestIndex(int[] arr, int R, int value) {
|
||||
int L = 0;
|
||||
int index = R;
|
||||
while (L <= R) {
|
||||
int mid = L + ((R - L) >> 1);
|
||||
if (arr[mid] >= value) {
|
||||
index = mid;
|
||||
R = mid - 1;
|
||||
} else {
|
||||
L = mid + 1;
|
||||
}
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
public static int maxPoint2(int[] arr, int L) {
|
||||
int left = 0;
|
||||
int right = 0;
|
||||
int N = arr.length;
|
||||
int max = 0;
|
||||
while (left < N) {
|
||||
while (right < N && arr[right] - arr[left] <= L) {
|
||||
right++;
|
||||
}
|
||||
max = Math.max(max, right - (left++));
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
// for test
|
||||
public static int test(int[] arr, int L) {
|
||||
int max = 0;
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
int pre = i - 1;
|
||||
while (pre >= 0 && arr[i] - arr[pre] <= L) {
|
||||
pre--;
|
||||
}
|
||||
max = Math.max(max, i - pre);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
// for test
|
||||
public static int[] generateArray(int len, int max) {
|
||||
int[] ans = new int[(int) (Math.random() * len) + 1];
|
||||
for (int i = 0; i < ans.length; i++) {
|
||||
ans[i] = (int) (Math.random() * max);
|
||||
}
|
||||
Arrays.sort(ans);
|
||||
return ans;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int len = 100;
|
||||
int max = 1000;
|
||||
int testTime = 100000;
|
||||
System.out.println("测试开始");
|
||||
for (int i = 0; i < testTime; i++) {
|
||||
int L = (int) (Math.random() * max);
|
||||
int[] arr = generateArray(len, max);
|
||||
int ans1 = maxPoint1(arr, L);
|
||||
int ans2 = maxPoint2(arr, L);
|
||||
int ans3 = test(arr, L);
|
||||
if (ans1 != ans2 || ans2 != ans3) {
|
||||
System.out.println("oops!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.println("测试结束");
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
package class015;
|
||||
|
||||
public class Code03_MaxABSBetweenLeftAndRight {
|
||||
|
||||
public static int maxABS1(int[] arr) {
|
||||
int res = Integer.MIN_VALUE;
|
||||
int maxLeft = 0;
|
||||
int maxRight = 0;
|
||||
for (int i = 0; i != arr.length - 1; i++) {
|
||||
maxLeft = Integer.MIN_VALUE;
|
||||
for (int j = 0; j != i + 1; j++) {
|
||||
maxLeft = Math.max(arr[j], maxLeft);
|
||||
}
|
||||
maxRight = Integer.MIN_VALUE;
|
||||
for (int j = i + 1; j != arr.length; j++) {
|
||||
maxRight = Math.max(arr[j], maxRight);
|
||||
}
|
||||
res = Math.max(Math.abs(maxLeft - maxRight), res);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static int maxABS2(int[] arr) {
|
||||
int[] lArr = new int[arr.length];
|
||||
int[] rArr = new int[arr.length];
|
||||
lArr[0] = arr[0];
|
||||
rArr[arr.length - 1] = arr[arr.length - 1];
|
||||
for (int i = 1; i < arr.length; i++) {
|
||||
lArr[i] = Math.max(lArr[i - 1], arr[i]);
|
||||
}
|
||||
for (int i = arr.length - 2; i > -1; i--) {
|
||||
rArr[i] = Math.max(rArr[i + 1], arr[i]);
|
||||
}
|
||||
int max = 0;
|
||||
for (int i = 0; i < arr.length - 1; i++) {
|
||||
max = Math.max(max, Math.abs(lArr[i] - rArr[i + 1]));
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static int maxABS3(int[] arr) {
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
max = Math.max(arr[i], max);
|
||||
}
|
||||
return max - Math.min(arr[0], arr[arr.length - 1]);
|
||||
}
|
||||
|
||||
public static int[] generateRandomArray(int length) {
|
||||
int[] arr = new int[length];
|
||||
for (int i = 0; i != arr.length; i++) {
|
||||
arr[i] = (int) (Math.random() * 1000) - 499;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int len = 100;
|
||||
int testTime = 100000;
|
||||
System.out.println("测试开始");
|
||||
for (int i = 0; i < testTime; i++) {
|
||||
int[] arr = generateRandomArray(len);
|
||||
int ans1 = maxABS1(arr);
|
||||
int ans2 = maxABS2(arr);
|
||||
int ans3 = maxABS3(arr);
|
||||
if (ans1 != ans2 || ans1 != ans3) {
|
||||
System.out.println("Oops!");
|
||||
}
|
||||
}
|
||||
System.out.println("测试结束");
|
||||
}
|
||||
}
|
@ -1,111 +0,0 @@
|
||||
package class017;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Code01_SubArrayMaxSum {
|
||||
|
||||
public static int test(int[] arr) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int N = arr.length;
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (int L = 0; L < N; L++) {
|
||||
for (int R = L; R < N; R++) {
|
||||
// arr[L...R]
|
||||
int sum = 0;
|
||||
for (int i = L; i <= R; i++) {
|
||||
sum += arr[i];
|
||||
}
|
||||
max = Math.max(max, sum);
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static int maxSum1(int[] arr) {
|
||||
if (arr == null || arr.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
// 0结尾时候的答案
|
||||
int pre = arr[0];
|
||||
int max = arr[0];
|
||||
for (int i = 1; i < arr.length; i++) {
|
||||
// i结尾时候的答案
|
||||
pre = arr[i] + (pre > 0 ? pre : 0);
|
||||
max = Math.max(max, pre);
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static List<List<Integer>> maxSum2(int[] arr) {
|
||||
List<List<Integer>> ans = new ArrayList<>();
|
||||
if (arr == null || arr.length == 0) {
|
||||
return ans;
|
||||
}
|
||||
int L = 0;
|
||||
int maxLen = 0;
|
||||
int maxSum = Integer.MIN_VALUE;
|
||||
int cur = 0;
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
// L...i sum
|
||||
cur += arr[i];
|
||||
if (cur == maxSum && (i - L + 1) == maxLen) {
|
||||
List<Integer> curAns = new ArrayList<>();
|
||||
curAns.add(L);
|
||||
curAns.add(i);
|
||||
ans.add(curAns);
|
||||
}
|
||||
if (cur > maxSum || (cur == maxSum && (i - L + 1) > maxLen)) {
|
||||
ans.clear();
|
||||
List<Integer> curAns = new ArrayList<>();
|
||||
curAns.add(L);
|
||||
curAns.add(i);
|
||||
ans.add(curAns);
|
||||
maxLen = i - L + 1;
|
||||
}
|
||||
maxSum = Math.max(maxSum, cur);
|
||||
if (cur < 0) {
|
||||
cur = 0;
|
||||
L = i + 1;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
public static int[] generateArray(int N, int V) {
|
||||
int n = (int) (Math.random() * N) + 1;
|
||||
int[] arr = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
arr[i] = (int) (Math.random() * V) - (int) (Math.random() * V);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// int N = 100;
|
||||
// int V = 100;
|
||||
// int testTime = 1000000;
|
||||
// System.out.println("test begin");
|
||||
// for (int i = 0; i < testTime; i++) {
|
||||
// int[] arr = generateArray(N, V);
|
||||
// int ans1 = maxSum1(arr);
|
||||
// int ans2 = maxSum2(arr);
|
||||
// if (ans1 != ans2) {
|
||||
// System.out.println("Oops!");
|
||||
// }
|
||||
// }
|
||||
// System.out.println("test finish");
|
||||
|
||||
int[] test = { 2, 2, 1, -9, 2, 3, -9, 6, -9, 2, 2, 2, -9, 2, 2, 2, -9, 1, 4, 1 };
|
||||
|
||||
List<List<Integer>> ans = maxSum2(test);
|
||||
|
||||
for (List<Integer> cur : ans) {
|
||||
System.out.println("start : " + cur.get(0) + ", end : " + cur.get(1));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
package class017;
|
||||
|
||||
public class Code02_SubMatrixMaxSum {
|
||||
|
||||
public static int maxSum1(int[][] matrix) {
|
||||
int n = matrix.length;
|
||||
int m = matrix[0].length;
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (int ai = 0; ai < n; ai++) {
|
||||
for (int aj = 0; aj < m; aj++) {
|
||||
for (int bi = ai; bi < n; bi++) {
|
||||
for (int bj = aj; bj < m; bj++) {
|
||||
max = Math.max(max, sum(matrix, ai, aj, bi, bj));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static int sum(int[][] matrix, int ai, int aj, int bi, int bj) {
|
||||
int sum = 0;
|
||||
for (int i = ai; i <= bi; i++) {
|
||||
for (int j = aj; j <= bj; j++) {
|
||||
sum += matrix[i][j];
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static int maxSum2(int[][] matrix) {
|
||||
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int max = Integer.MIN_VALUE;
|
||||
int cur = 0;
|
||||
int[] s = null;
|
||||
for (int i = 0; i != matrix.length; i++) {
|
||||
s = new int[matrix[0].length];
|
||||
for (int j = i; j != matrix.length; j++) {
|
||||
cur = 0;
|
||||
for (int k = 0; k != s.length; k++) {
|
||||
s[k] += matrix[j][k];
|
||||
cur += s[k];
|
||||
max = Math.max(max, cur);
|
||||
cur = cur < 0 ? 0 : cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static int[][] generateMatrix(int N, int M, int V) {
|
||||
int n = (int) (Math.random() * N) + 1;
|
||||
int m = (int) (Math.random() * M) + 1;
|
||||
int[][] matrix = new int[n][m];
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
matrix[i][j] = (int) (Math.random() * V) - (int) (Math.random() * V);
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int N = 20;
|
||||
int M = 20;
|
||||
int V = 100;
|
||||
int testTime = 50000;
|
||||
System.out.println("test begin");
|
||||
for (int i = 0; i < testTime; i++) {
|
||||
int[][] matrix = generateMatrix(N, M, V);
|
||||
int ans1 = maxSum1(matrix);
|
||||
int ans2 = maxSum2(matrix);
|
||||
if (ans1 != ans2) {
|
||||
System.out.println("Oops!");
|
||||
}
|
||||
}
|
||||
System.out.println("test finish");
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,54 +0,0 @@
|
||||
package class018;
|
||||
|
||||
public class Code02_PowXN {
|
||||
|
||||
public static void main(String[] args) {
|
||||
long a = 2;
|
||||
long n = 13; // 1101
|
||||
long t = a;
|
||||
long res = 1;
|
||||
while (n != 0) {
|
||||
// n = 110111011111
|
||||
// & 1 = 000000000001
|
||||
// 000000000000
|
||||
// n & (n-1)
|
||||
// 把n抹掉最右侧的1
|
||||
// n = 10010101100010000
|
||||
// n-1 = 10010101100001111
|
||||
// & = 10010101100000000
|
||||
if ((n & 1) == 1) {
|
||||
res *= t;
|
||||
}
|
||||
t = t * t;
|
||||
n = n >> 1;
|
||||
}
|
||||
System.out.println(res);
|
||||
System.out.println(Math.pow(2.0, 13));
|
||||
System.out.println(-Integer.MIN_VALUE);
|
||||
System.out.println(Math.abs(Integer.MIN_VALUE));
|
||||
|
||||
}
|
||||
|
||||
// 求x的n次方
|
||||
public static double myPow(double x, int n) {
|
||||
if (n == 0) {
|
||||
return 1D;
|
||||
}
|
||||
if (n == Integer.MIN_VALUE) {
|
||||
return (x == 1D || x == -1D) ? 1D : 0;
|
||||
}
|
||||
// 4.5 -3
|
||||
int pow = Math.abs(n);
|
||||
double t = x;
|
||||
double ans = 1D;
|
||||
while (pow != 0) {
|
||||
if ((pow & 1) != 0) {
|
||||
ans *= t;
|
||||
}
|
||||
pow >>= 1;
|
||||
t = t * t;
|
||||
}
|
||||
return n < 0 ? (1D / ans) : ans;
|
||||
}
|
||||
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package class018;
|
||||
|
||||
public class Code03_ContainerWithMostWater {
|
||||
|
||||
public static int maxArea1(int[] h) {
|
||||
int max = 0;
|
||||
int N = h.length;
|
||||
for (int i = 0; i < N; i++) { // h[i]
|
||||
for (int j = i + 1; j < N; j++) { // h[j]
|
||||
max = Math.max(max, Math.min(h[i], h[j]) * (j - i));
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static int maxArea2(int[] h) {
|
||||
int max = 0;
|
||||
int l = 0;
|
||||
int r = h.length - 1;
|
||||
while (l < r) {
|
||||
max = Math.max(max, Math.min(h[l], h[r]) * (r - l));
|
||||
if (h[l] > h[r]) {
|
||||
r--;
|
||||
} else {
|
||||
l++;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package class019;
|
||||
|
||||
public class Code01_RotateImage {
|
||||
|
||||
public static void rotate(int[][] matrix) {
|
||||
// matrix.length == matrix[0].length
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
int c = matrix.length - 1;
|
||||
int d = matrix[0].length - 1;
|
||||
while (a < c) {
|
||||
rotateEdge(matrix, a++, b++, c--, d--);
|
||||
}
|
||||
}
|
||||
|
||||
public static void rotateEdge(int[][] m, int a, int b, int c, int d) {
|
||||
int tmp = 0;
|
||||
for (int i = 0; i < d - b; i++) {
|
||||
tmp = m[a][b + i];
|
||||
m[a][b + i] = m[c - i][b];
|
||||
m[c - i][b] = m[c][d - i];
|
||||
m[c][d - i] = m[a + i][d];
|
||||
m[a + i][d] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package class019;
|
||||
|
||||
public class Code02_ZigZagPrintMatrix {
|
||||
|
||||
public static void printMatrixZigZag(int[][] matrix) {
|
||||
int aRow = 0;
|
||||
int aCol = 0;
|
||||
int bRow = 0;
|
||||
int bCol = 0;
|
||||
// A和B一定会共同走到右下角的位置
|
||||
int endR = matrix.length - 1;
|
||||
int endC = matrix[0].length - 1;
|
||||
// fromUp = true 斜线打印方向应该从右上走到左下
|
||||
// fromUp = false 斜线打印方向应该从左下走到右上
|
||||
boolean fromUp = false;
|
||||
while (aRow != endR + 1) {
|
||||
printLevel(matrix, aRow, aCol, bRow, bCol, fromUp);
|
||||
aRow = aCol == endC ? aRow + 1 : aRow;
|
||||
aCol = aCol == endC ? aCol : aCol + 1;
|
||||
bCol = bRow == endR ? bCol + 1 : bCol;
|
||||
bRow = bRow == endR ? bRow : bRow + 1;
|
||||
fromUp = !fromUp;
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void printLevel(int[][] m, int aRow, int aCol, int bRow, int bCol, boolean f) {
|
||||
if (f) {
|
||||
while (aRow != bRow + 1) {
|
||||
System.out.print(m[aRow++][aCol--] + " ");
|
||||
}
|
||||
} else {
|
||||
while (bRow != aRow - 1) {
|
||||
System.out.print(m[bRow--][bCol++] + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[][] matrix = {
|
||||
{ 1, 2, 3, 4 },
|
||||
{ 5, 6, 7, 8 },
|
||||
{ 9, 10, 11, 12 } };
|
||||
printMatrixZigZag(matrix);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
package class019;
|
||||
|
||||
public class Code03_PrintStar {
|
||||
|
||||
public static void printStar(int N) {
|
||||
int leftUp = 0;
|
||||
int rightDown = N - 1;
|
||||
char[][] m = new char[N][N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
m[i][j] = ' ';
|
||||
}
|
||||
}
|
||||
while (leftUp <= rightDown) {
|
||||
set(m, leftUp, rightDown);
|
||||
leftUp += 2;
|
||||
rightDown -= 2;
|
||||
}
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
System.out.print(m[i][j] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static void set(char[][] m, int leftUp, int rightDown) {
|
||||
for (int col = leftUp; col <= rightDown; col++) {
|
||||
m[leftUp][col] = '*';
|
||||
}
|
||||
for (int row = leftUp + 1; row <= rightDown; row++) {
|
||||
m[row][rightDown] = '*';
|
||||
}
|
||||
for (int col = rightDown - 1; col > leftUp; col--) {
|
||||
m[rightDown][col] = '*';
|
||||
}
|
||||
for (int row = rightDown - 1; row > leftUp + 1; row--) {
|
||||
m[row][leftUp + 1] = '*';
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
printStar(8);
|
||||
}
|
||||
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
package class020;
|
||||
|
||||
public class Code01_RotateString {
|
||||
|
||||
public static String rotate1(String s, int leftSize) {
|
||||
if (leftSize <= 0 || leftSize >= s.length()) {
|
||||
return s;
|
||||
}
|
||||
return process1(s.toCharArray(), 0, leftSize - 1, s.length() - 1);
|
||||
}
|
||||
|
||||
public static String process1(char[] str, int L, int M, int R) {
|
||||
reverse(str, L, M);
|
||||
reverse(str, M + 1, R);
|
||||
reverse(str, L, R);
|
||||
return String.valueOf(str);
|
||||
}
|
||||
|
||||
public static void reverse(char[] str, int L, int R) {
|
||||
while (L < R) {
|
||||
char tmp = str[L];
|
||||
str[L++] = str[R];
|
||||
str[R--] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
public static String rotate2(String s, int leftSize) {
|
||||
if (leftSize <= 0 || leftSize >= s.length()) {
|
||||
return s;
|
||||
}
|
||||
char[] str = s.toCharArray();
|
||||
int L = 0;
|
||||
int R = str.length - 1;
|
||||
int lpart = leftSize;
|
||||
int rpart = str.length - leftSize;
|
||||
int same = Math.min(lpart, rpart);
|
||||
int diff = lpart - rpart;
|
||||
exchange(str, L, R, same);
|
||||
while (diff != 0) {
|
||||
if (diff > 0) {
|
||||
L += same;
|
||||
lpart = diff;
|
||||
} else {
|
||||
R -= same;
|
||||
rpart = -diff;
|
||||
}
|
||||
same = Math.min(lpart, rpart);
|
||||
diff = lpart - rpart;
|
||||
exchange(str, L, R, same);
|
||||
}
|
||||
return String.valueOf(str);
|
||||
}
|
||||
|
||||
public static void exchange(char[] chas, int start, int end, int size) {
|
||||
int i = end - size + 1;
|
||||
char tmp = 0;
|
||||
while (size-- != 0) {
|
||||
tmp = chas[start];
|
||||
chas[start] = chas[i];
|
||||
chas[i] = tmp;
|
||||
start++;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// for test
|
||||
public static String getRandomString(int possibilities, int strMaxSize) {
|
||||
char[] ans = new char[(int) (Math.random() * strMaxSize) + 1];
|
||||
for (int i = 0; i < ans.length; i++) {
|
||||
ans[i] = (char) ((int) (Math.random() * possibilities) + 'a');
|
||||
}
|
||||
return String.valueOf(ans);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int possibilities = 5;
|
||||
int strMaxSize = 10;
|
||||
int testTimes = 5000000;
|
||||
System.out.println("test begin, test time : " + testTimes);
|
||||
for (int i = 0; i < testTimes; i++) {
|
||||
String str = getRandomString(possibilities, strMaxSize);
|
||||
int leftSize = (int) (Math.random() * (str.length() + 1));
|
||||
String ans1 = rotate1(str, leftSize);
|
||||
String ans2 = rotate2(str, leftSize);
|
||||
if (!ans1.equals(ans2)) {
|
||||
System.out.println("Oops!");
|
||||
}
|
||||
}
|
||||
System.out.println("test finish");
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package class021;
|
||||
|
||||
public class Code01_SwapWithoutTmp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int a = 123;
|
||||
int b = -898121;
|
||||
System.out.println(a);
|
||||
System.out.println(b);
|
||||
a = a ^ b;
|
||||
b = a ^ b;
|
||||
a = a ^ b;
|
||||
System.out.println(a);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
package class022;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Stack;
|
||||
|
||||
public class Code02_CountFiles {
|
||||
|
||||
// 注意这个函数也会统计隐藏文件
|
||||
public static int getFileNumber(String folderPath) {
|
||||
// File (文件夹、文件)
|
||||
File root = new File(folderPath);
|
||||
if (!root.isDirectory() && !root.isFile()) {
|
||||
return 0;
|
||||
}
|
||||
if (root.isFile()) {
|
||||
return 1;
|
||||
}
|
||||
// File 文件夹 文件 stack只放文件夹
|
||||
Stack<File> stack = new Stack<>();
|
||||
stack.add(root);
|
||||
int files = 0;
|
||||
while (!stack.isEmpty()) {
|
||||
File folder = stack.pop();
|
||||
for (File next : folder.listFiles()) {
|
||||
if (next.isFile() && next.getName().endsWith("java")) {
|
||||
files++;
|
||||
}
|
||||
if (next.isDirectory()) {
|
||||
stack.push(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 你可以自己更改目录
|
||||
String path = "/Users/zuochengyun/Desktop/";
|
||||
System.out.println(getFileNumber(path));
|
||||
}
|
||||
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
package class024;
|
||||
|
||||
public class Code02_ZigZagPrintMatrix {
|
||||
|
||||
public static void printMatrixZigZag(int[][] matrix) {
|
||||
// x -> (a,b) 先往右,再往下
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
|
||||
// y -> (c,d) 先往下,再往右
|
||||
int c = 0;
|
||||
int d = 0;
|
||||
// (endR, endC) 是最右下角的位置
|
||||
int endR = matrix.length - 1;
|
||||
int endC = matrix[0].length - 1;
|
||||
// fromUp = true 斜线打印方向应该从右上走到左下
|
||||
// fromUp = false 斜线打印方向应该从左下走到右上
|
||||
boolean fromUp = false;
|
||||
while (a != endR + 1) {
|
||||
// (a,b) (c,d) 方向
|
||||
printLevel(matrix, a, b, c, d, fromUp);
|
||||
a = b == endC ? a + 1 : a;
|
||||
b = b == endC ? b : b + 1;
|
||||
d = c == endR ? d + 1 : d;
|
||||
c = c == endR ? c : c + 1;
|
||||
fromUp = !fromUp;
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void printLevel(int[][] m, int aRow, int aCol, int bRow, int bCol, boolean f) {
|
||||
if (f) {
|
||||
while (aRow != bRow + 1) {
|
||||
System.out.print(m[aRow++][aCol--] + " ");
|
||||
}
|
||||
} else {
|
||||
while (bRow != aRow - 1) {
|
||||
System.out.print(m[bRow--][bCol++] + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[][] matrix = {
|
||||
{ 1, 2, 3, 4 },
|
||||
{ 5, 6, 7, 8 },
|
||||
{ 9, 10, 11, 12 } };
|
||||
printMatrixZigZag(matrix);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
package class024;
|
||||
|
||||
public class Code03_PrintStar {
|
||||
|
||||
public static void printStar(int N) {
|
||||
int leftUp = 0;
|
||||
int rightDown = N - 1;
|
||||
char[][] m = new char[N][N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
m[i][j] = ' ';
|
||||
}
|
||||
}
|
||||
while (leftUp <= rightDown) {
|
||||
set(m, leftUp, rightDown);
|
||||
leftUp += 2;
|
||||
rightDown -= 2;
|
||||
}
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
System.out.print(m[i][j] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static void set(char[][] m, int leftUp, int rightDown) {
|
||||
for (int col = leftUp; col <= rightDown; col++) {
|
||||
m[leftUp][col] = '*';
|
||||
}
|
||||
for (int row = leftUp + 1; row <= rightDown; row++) {
|
||||
m[row][rightDown] = '*';
|
||||
}
|
||||
for (int col = rightDown - 1; col > leftUp; col--) {
|
||||
m[rightDown][col] = '*';
|
||||
}
|
||||
for (int row = rightDown - 1; row > leftUp + 1; row--) {
|
||||
m[row][leftUp + 1] = '*';
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
printStar(8);
|
||||
}
|
||||
|
||||
}
|
@ -1,83 +0,0 @@
|
||||
package class026;
|
||||
|
||||
public class Code03_SubMatrixMaxSum {
|
||||
|
||||
public static int maxSum1(int[][] matrix) {
|
||||
int n = matrix.length;
|
||||
int m = matrix[0].length;
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (int ai = 0; ai < n; ai++) {
|
||||
for (int aj = 0; aj < m; aj++) {
|
||||
for (int bi = ai; bi < n; bi++) {
|
||||
for (int bj = aj; bj < m; bj++) {
|
||||
max = Math.max(max, sum(matrix, ai, aj, bi, bj));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static int sum(int[][] matrix, int ai, int aj, int bi, int bj) {
|
||||
int sum = 0;
|
||||
for (int i = ai; i <= bi; i++) {
|
||||
for (int j = aj; j <= bj; j++) {
|
||||
sum += matrix[i][j];
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static int maxSum2(int[][] matrix) {
|
||||
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
|
||||
return 0;
|
||||
}
|
||||
int max = Integer.MIN_VALUE;
|
||||
int cur = 0;
|
||||
int[] s = null;
|
||||
for (int i = 0; i != matrix.length; i++) {
|
||||
s = new int[matrix[0].length];
|
||||
for (int j = i; j != matrix.length; j++) {
|
||||
cur = 0;
|
||||
for (int k = 0; k != s.length; k++) {
|
||||
s[k] += matrix[j][k];
|
||||
cur += s[k];
|
||||
max = Math.max(max, cur);
|
||||
cur = cur < 0 ? 0 : cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
public static int[][] generateMatrix(int N, int M, int V) {
|
||||
int n = (int) (Math.random() * N) + 1;
|
||||
int m = (int) (Math.random() * M) + 1;
|
||||
int[][] matrix = new int[n][m];
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < m; j++) {
|
||||
matrix[i][j] = (int) (Math.random() * V) - (int) (Math.random() * V);
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int N = 20;
|
||||
int M = 20;
|
||||
int V = 100;
|
||||
int testTime = 50000;
|
||||
System.out.println("test begin");
|
||||
for (int i = 0; i < testTime; i++) {
|
||||
int[][] matrix = generateMatrix(N, M, V);
|
||||
int ans1 = maxSum1(matrix);
|
||||
int ans2 = maxSum2(matrix);
|
||||
if (ans1 != ans2) {
|
||||
System.out.println("Oops!");
|
||||
}
|
||||
}
|
||||
System.out.println("test finish");
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
package class027;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Stack;
|
||||
|
||||
public class Code02_CountFiles {
|
||||
|
||||
// 注意这个函数也会统计隐藏文件
|
||||
public static int getFileNumber(String folderPath) {
|
||||
File root = new File(folderPath);
|
||||
if (!root.isDirectory() && !root.isFile()) {
|
||||
return 0;
|
||||
}
|
||||
if (root.isFile()) {
|
||||
return 1;
|
||||
}
|
||||
// File分为文件夹和文件
|
||||
// 而stack只放文件夹
|
||||
Stack<File> stack = new Stack<>();
|
||||
stack.add(root);
|
||||
int files = 0;
|
||||
while (!stack.isEmpty()) {
|
||||
File folder = stack.pop();
|
||||
for (File next : folder.listFiles()) {
|
||||
if (next.isFile()) {
|
||||
files++;
|
||||
}
|
||||
if (next.isDirectory()) {
|
||||
stack.push(next);
|
||||
}
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 你可以自己更改目录来查看这个目录下的文件个数
|
||||
String path = "/Users/zuochengyun/Desktop";
|
||||
System.out.println(getFileNumber(path));
|
||||
}
|
||||
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
package class028;
|
||||
|
||||
public class Code01_RotateString {
|
||||
|
||||
public static String rotate1(String s, int leftSize) {
|
||||
if (leftSize <= 0 || leftSize >= s.length()) {
|
||||
return s;
|
||||
}
|
||||
return process1(s.toCharArray(), 0, leftSize - 1, s.length() - 1);
|
||||
}
|
||||
|
||||
public static String process1(char[] str, int L, int M, int R) {
|
||||
reverse(str, L, M);
|
||||
reverse(str, M + 1, R);
|
||||
reverse(str, L, R);
|
||||
return String.valueOf(str);
|
||||
}
|
||||
|
||||
public static void reverse(char[] str, int L, int R) {
|
||||
while (L < R) {
|
||||
char tmp = str[L];
|
||||
str[L++] = str[R];
|
||||
str[R--] = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
public static String rotate2(String s, int leftSize) {
|
||||
if (leftSize <= 0 || leftSize >= s.length()) {
|
||||
return s;
|
||||
}
|
||||
char[] str = s.toCharArray();
|
||||
int L = 0;
|
||||
int R = str.length - 1;
|
||||
int lpart = leftSize;
|
||||
int rpart = str.length - leftSize;
|
||||
int same = Math.min(lpart, rpart);
|
||||
int diff = lpart - rpart;
|
||||
exchange(str, L, R, same);
|
||||
while (diff != 0) {
|
||||
if (diff > 0) {
|
||||
L += same;
|
||||
lpart = diff;
|
||||
} else {
|
||||
R -= same;
|
||||
rpart = -diff;
|
||||
}
|
||||
same = Math.min(lpart, rpart);
|
||||
diff = lpart - rpart;
|
||||
exchange(str, L, R, same);
|
||||
}
|
||||
return String.valueOf(str);
|
||||
}
|
||||
|
||||
public static void exchange(char[] chas, int start, int end, int size) {
|
||||
int i = end - size + 1;
|
||||
char tmp = 0;
|
||||
while (size-- != 0) {
|
||||
tmp = chas[start];
|
||||
chas[start] = chas[i];
|
||||
chas[i] = tmp;
|
||||
start++;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// for test
|
||||
public static String getRandomString(int possibilities, int strMaxSize) {
|
||||
char[] ans = new char[(int) (Math.random() * strMaxSize) + 1];
|
||||
for (int i = 0; i < ans.length; i++) {
|
||||
ans[i] = (char) ((int) (Math.random() * possibilities) + 'a');
|
||||
}
|
||||
return String.valueOf(ans);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int possibilities = 5;
|
||||
int strMaxSize = 10;
|
||||
int testTimes = 5000000;
|
||||
System.out.println("test begin, test time : " + testTimes);
|
||||
for (int i = 0; i < testTimes; i++) {
|
||||
String str = getRandomString(possibilities, strMaxSize);
|
||||
int leftSize = (int) (Math.random() * (str.length() + 1));
|
||||
String ans1 = rotate1(str, leftSize);
|
||||
String ans2 = rotate2(str, leftSize);
|
||||
if (!ans1.equals(ans2)) {
|
||||
System.out.println("Oops!");
|
||||
}
|
||||
}
|
||||
System.out.println("test finish");
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package class031;
|
||||
|
||||
public class Code01_SwapWithoutTmp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int test1 = 23;
|
||||
int test2 = 15;
|
||||
System.out.println(test1 ^ test2);
|
||||
|
||||
|
||||
|
||||
int a = -111;
|
||||
int b = 343242111;
|
||||
System.out.println(a);
|
||||
System.out.println(b);
|
||||
a = a ^ b;
|
||||
b = a ^ b;
|
||||
a = a ^ b;
|
||||
System.out.println(a);
|
||||
System.out.println(b);
|
||||
}
|
||||
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
package class031;
|
||||
|
||||
public class Code02_AddMinus {
|
||||
|
||||
public static int add(int a, int b) {
|
||||
int t = 0;
|
||||
while (b != 0) {
|
||||
t = a;
|
||||
a = a ^ b; // a -> a' 无进位相加信息
|
||||
b = ((t & b) << 1); // b -> b' 进位信息
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
public static int minus(int a, int b) {
|
||||
return add(a, add(~b, 1));
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int a = 8739284;
|
||||
int b = 7348472;
|
||||
System.out.println(a + b);
|
||||
System.out.println(add(a, b));
|
||||
|
||||
System.out.println(a - b);
|
||||
System.out.println(minus(a, b));
|
||||
}
|
||||
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
package class033;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class Code01_FindHalfMajority {
|
||||
|
||||
public static int halfMajor(int[] arr) {
|
||||
int target = 0;
|
||||
int HP = 0;
|
||||
for (int i = 0; i != arr.length; i++) {
|
||||
if (HP == 0) {
|
||||
target = arr[i];
|
||||
HP = 1;
|
||||
} else if (arr[i] == target) {
|
||||
HP++;
|
||||
} else {
|
||||
HP--;
|
||||
}
|
||||
}
|
||||
if (HP == 0) {
|
||||
return -1;
|
||||
}
|
||||
HP = 0;
|
||||
for (int i = 0; i != arr.length; i++) {
|
||||
if (arr[i] == target) {
|
||||
HP++;
|
||||
}
|
||||
}
|
||||
return HP > arr.length / 2 ? target : -1;
|
||||
}
|
||||
|
||||
// for test
|
||||
public static int right(int[] arr) {
|
||||
HashMap<Integer, Integer> map = new HashMap<>();
|
||||
for (int cur : arr) {
|
||||
if (!map.containsKey(cur)) {
|
||||
map.put(cur, 0);
|
||||
}
|
||||
map.put(cur, map.get(cur) + 1);
|
||||
}
|
||||
for (Entry<Integer, Integer> entry : map.entrySet()) {
|
||||
if (entry.getValue() > arr.length / 2) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// for test
|
||||
public static int[] genareteRandomArray(int len, int max) {
|
||||
int[] ans = new int[(int) (Math.random() * len) + 1];
|
||||
for (int i = 0; i < ans.length; i++) {
|
||||
ans[i] = (int) (Math.random() * max) + 1;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int len = 100;
|
||||
int max = 10;
|
||||
int testTime = 100000;
|
||||
System.out.println("test begin");
|
||||
for (int i = 0; i < testTime; i++) {
|
||||
int[] arr = genareteRandomArray(len, max);
|
||||
int ans1 = halfMajor(arr);
|
||||
int ans2 = right(arr);
|
||||
if (ans1 != ans2) {
|
||||
System.out.println("Oops!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
System.out.println("test end");
|
||||
}
|
||||
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
package class033;
|
||||
|
||||
public class Code03_ZigZagPrintMatrix {
|
||||
|
||||
public static void printMatrixZigZag(int[][] m) {
|
||||
// (a,b) A 先往右,再往下
|
||||
int a = 0;
|
||||
int b = 0;
|
||||
// (c,d) B 先往下,在往右
|
||||
int c = 0;
|
||||
int d = 0;
|
||||
int er = m.length - 1;
|
||||
int ec = m[0].length - 1;
|
||||
boolean fromUp = false;
|
||||
while (a != er + 1) {
|
||||
printLevel(m, a, b, c, d, fromUp);
|
||||
// A 先往右,再往下
|
||||
a = b == ec ? a + 1 : a;
|
||||
b = b == ec ? b : b + 1;
|
||||
// B 先往下,在往右
|
||||
d = c == er ? d + 1 : d;
|
||||
c = c == er ? c : c + 1;
|
||||
fromUp = !fromUp;
|
||||
}
|
||||
}
|
||||
|
||||
public static void printLevel(int[][] m, int a, int b, int c, int d, boolean f) {
|
||||
if (f) {
|
||||
while (a != c + 1) {
|
||||
System.out.print(m[a++][b--] + " ");
|
||||
}
|
||||
} else {
|
||||
while (c != a - 1) {
|
||||
System.out.print(m[c--][d++] + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[][] matrix = {
|
||||
{ 1, 2, 3, 4 },
|
||||
{ 5, 6, 7, 8 },
|
||||
{ 9, 10, 11, 12 } };
|
||||
printMatrixZigZag(matrix);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
package class033;
|
||||
|
||||
public class Code04_PrintStar {
|
||||
|
||||
public static void printStar(int N) {
|
||||
int leftUp = 0;
|
||||
int rightDown = N - 1;
|
||||
char[][] m = new char[N][N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
m[i][j] = ' ';
|
||||
}
|
||||
}
|
||||
while (leftUp <= rightDown) {
|
||||
set(m, leftUp, rightDown);
|
||||
leftUp += 2;
|
||||
rightDown -= 2;
|
||||
}
|
||||
for (int i = 0; i < N; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
System.out.print(m[i][j] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static void set(char[][] m, int leftUp, int rightDown) {
|
||||
for (int col = leftUp; col <= rightDown; col++) {
|
||||
m[leftUp][col] = '*';
|
||||
}
|
||||
for (int row = leftUp + 1; row <= rightDown; row++) {
|
||||
m[row][rightDown] = '*';
|
||||
}
|
||||
for (int col = rightDown - 1; col > leftUp; col--) {
|
||||
m[rightDown][col] = '*';
|
||||
}
|
||||
for (int row = rightDown - 1; row > leftUp + 1; row--) {
|
||||
m[row][leftUp + 1] = '*';
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
printStar(8);
|
||||
}
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue