parent
b12972232d
commit
049a6113b6
@ -0,0 +1,48 @@
|
||||
package 第03期.mca_01;
|
||||
|
||||
public class Code01_Swap {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int a = 16;
|
||||
int b = 603;
|
||||
|
||||
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);
|
||||
|
||||
int[] arr = { 3, 1, 100 };
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
|
||||
arr[i] = arr[i] ^ arr[j];
|
||||
arr[j] = arr[i] ^ arr[j];
|
||||
arr[i] = arr[i] ^ arr[j];
|
||||
|
||||
System.out.println(arr[i] + " , " + arr[j]);
|
||||
|
||||
System.out.println(arr[0]);
|
||||
System.out.println(arr[2]);
|
||||
|
||||
swap(arr, 0, 0);
|
||||
|
||||
System.out.println(arr[0]);
|
||||
System.out.println(arr[2]);
|
||||
|
||||
}
|
||||
|
||||
public static void swap(int[] arr, int i, int j) {
|
||||
// arr[0] = arr[0] ^ arr[0];
|
||||
arr[i] = arr[i] ^ arr[j];
|
||||
arr[j] = arr[i] ^ arr[j];
|
||||
arr[i] = arr[i] ^ arr[j];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package 第03期.mca_01;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Code04_IsStepSum {
|
||||
|
||||
public static boolean isStepSum(int stepSum) {
|
||||
int L = 0;
|
||||
int R = stepSum;
|
||||
int M = 0;
|
||||
int cur = 0;
|
||||
while (L <= R) {
|
||||
M = L + ((R - L) >> 1);
|
||||
cur = stepSum(M);
|
||||
if (cur == stepSum) {
|
||||
return true;
|
||||
} else if (cur < stepSum) {
|
||||
L = M + 1;
|
||||
} else {
|
||||
R = M - 1;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static int stepSum(int num) {
|
||||
int sum = 0;
|
||||
while (num != 0) {
|
||||
sum += num;
|
||||
num /= 10;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
// for test
|
||||
public static HashMap<Integer, Integer> generateStepSumNumberMap(int numMax) {
|
||||
HashMap<Integer, Integer> map = new HashMap<>();
|
||||
for (int i = 0; i <= numMax; i++) {
|
||||
map.put(stepSum(i), i);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
// for test
|
||||
public static void main(String[] args) {
|
||||
int max = 1000000;
|
||||
int maxStepSum = stepSum(max);
|
||||
HashMap<Integer, Integer> ans = generateStepSumNumberMap(max);
|
||||
System.out.println("测试开始");
|
||||
for (int i = 0; i <= maxStepSum; i++) {
|
||||
if (isStepSum(i) ^ ans.containsKey(i)) {
|
||||
System.out.println("出错了!");
|
||||
}
|
||||
}
|
||||
System.out.println("测试结束");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package 第03期.mca_01;
|
||||
|
||||
// leetcode 875题
|
||||
public class Code05_KokoEatingBananas {
|
||||
|
||||
public static int minEatingSpeed(int[] piles, int h) {
|
||||
int L = 1;
|
||||
int R = 0;
|
||||
for (int pile : piles) {
|
||||
R = Math.max(R, pile);
|
||||
}
|
||||
int ans = 0;
|
||||
int M = 0;
|
||||
while (L <= R) {
|
||||
M = L + ((R - L) >> 1);
|
||||
if (hours(piles, M) <= h) {
|
||||
ans = M;
|
||||
R = M - 1;
|
||||
} else {
|
||||
L = M + 1;
|
||||
}
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
public static int hours(int[] piles, int speed) {
|
||||
int ans = 0;
|
||||
int offset = speed - 1;
|
||||
for (int pile : piles) {
|
||||
ans += (pile + offset) / speed;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,204 @@
|
||||
package 第03期.mca_02;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class Code02_IsPalindromeList {
|
||||
|
||||
public static class Node {
|
||||
public int value;
|
||||
public Node next;
|
||||
|
||||
public Node(int data) {
|
||||
this.value = data;
|
||||
}
|
||||
}
|
||||
|
||||
// need n extra space
|
||||
public static boolean isPalindrome1(Node head) {
|
||||
Stack<Node> stack = new Stack<Node>();
|
||||
Node cur = head;
|
||||
while (cur != null) {
|
||||
stack.push(cur);
|
||||
cur = cur.next;
|
||||
}
|
||||
while (head != null) {
|
||||
if (head.value != stack.pop().value) {
|
||||
return false;
|
||||
}
|
||||
head = head.next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// need n/2 extra space
|
||||
public static boolean isPalindrome2(Node head) {
|
||||
if (head == null || head.next == null) {
|
||||
return true;
|
||||
}
|
||||
Node right = head.next;
|
||||
Node cur = head;
|
||||
while (cur.next != null && cur.next.next != null) {
|
||||
right = right.next;
|
||||
cur = cur.next.next;
|
||||
}
|
||||
Stack<Node> stack = new Stack<Node>();
|
||||
while (right != null) {
|
||||
stack.push(right);
|
||||
right = right.next;
|
||||
}
|
||||
while (!stack.isEmpty()) {
|
||||
if (head.value != stack.pop().value) {
|
||||
return false;
|
||||
}
|
||||
head = head.next;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// need O(1) extra space
|
||||
public static boolean isPalindrome3(Node head) {
|
||||
if (head == null || head.next == null) {
|
||||
return true;
|
||||
}
|
||||
Node n1 = head;
|
||||
Node n2 = head;
|
||||
while (n2.next != null && n2.next.next != null) { // find mid node
|
||||
n1 = n1.next; // n1 -> mid
|
||||
n2 = n2.next.next; // n2 -> end
|
||||
}
|
||||
// n1 中点
|
||||
|
||||
|
||||
n2 = n1.next; // n2 -> right part first node
|
||||
n1.next = null; // mid.next -> null
|
||||
Node n3 = null;
|
||||
while (n2 != null) { // right part convert
|
||||
n3 = n2.next; // n3 -> save next node
|
||||
n2.next = n1; // next of right node convert
|
||||
n1 = n2; // n1 move
|
||||
n2 = n3; // n2 move
|
||||
}
|
||||
n3 = n1; // n3 -> save last node
|
||||
n2 = head;// n2 -> left first node
|
||||
boolean res = true;
|
||||
while (n1 != null && n2 != null) { // check palindrome
|
||||
if (n1.value != n2.value) {
|
||||
res = false;
|
||||
break;
|
||||
}
|
||||
n1 = n1.next; // left to mid
|
||||
n2 = n2.next; // right to mid
|
||||
}
|
||||
n1 = n3.next;
|
||||
n3.next = null;
|
||||
while (n1 != null) { // recover list
|
||||
n2 = n1.next;
|
||||
n1.next = n3;
|
||||
n3 = n1;
|
||||
n1 = n2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void printLinkedList(Node node) {
|
||||
System.out.print("Linked List: ");
|
||||
while (node != null) {
|
||||
System.out.print(node.value + " ");
|
||||
node = node.next;
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Node head = null;
|
||||
printLinkedList(head);
|
||||
System.out.print(isPalindrome1(head) + " | ");
|
||||
System.out.print(isPalindrome2(head) + " | ");
|
||||
System.out.println(isPalindrome3(head) + " | ");
|
||||
printLinkedList(head);
|
||||
System.out.println("=========================");
|
||||
|
||||
head = new Node(1);
|
||||
printLinkedList(head);
|
||||
System.out.print(isPalindrome1(head) + " | ");
|
||||
System.out.print(isPalindrome2(head) + " | ");
|
||||
System.out.println(isPalindrome3(head) + " | ");
|
||||
printLinkedList(head);
|
||||
System.out.println("=========================");
|
||||
|
||||
head = new Node(1);
|
||||
head.next = new Node(2);
|
||||
printLinkedList(head);
|
||||
System.out.print(isPalindrome1(head) + " | ");
|
||||
System.out.print(isPalindrome2(head) + " | ");
|
||||
System.out.println(isPalindrome3(head) + " | ");
|
||||
printLinkedList(head);
|
||||
System.out.println("=========================");
|
||||
|
||||
head = new Node(1);
|
||||
head.next = new Node(1);
|
||||
printLinkedList(head);
|
||||
System.out.print(isPalindrome1(head) + " | ");
|
||||
System.out.print(isPalindrome2(head) + " | ");
|
||||
System.out.println(isPalindrome3(head) + " | ");
|
||||
printLinkedList(head);
|
||||
System.out.println("=========================");
|
||||
|
||||
head = new Node(1);
|
||||
head.next = new Node(2);
|
||||
head.next.next = new Node(3);
|
||||
printLinkedList(head);
|
||||
System.out.print(isPalindrome1(head) + " | ");
|
||||
System.out.print(isPalindrome2(head) + " | ");
|
||||
System.out.println(isPalindrome3(head) + " | ");
|
||||
printLinkedList(head);
|
||||
System.out.println("=========================");
|
||||
|
||||
head = new Node(1);
|
||||
head.next = new Node(2);
|
||||
head.next.next = new Node(1);
|
||||
printLinkedList(head);
|
||||
System.out.print(isPalindrome1(head) + " | ");
|
||||
System.out.print(isPalindrome2(head) + " | ");
|
||||
System.out.println(isPalindrome3(head) + " | ");
|
||||
printLinkedList(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(1);
|
||||
printLinkedList(head);
|
||||
System.out.print(isPalindrome1(head) + " | ");
|
||||
System.out.print(isPalindrome2(head) + " | ");
|
||||
System.out.println(isPalindrome3(head) + " | ");
|
||||
printLinkedList(head);
|
||||
System.out.println("=========================");
|
||||
|
||||
head = new Node(1);
|
||||
head.next = new Node(2);
|
||||
head.next.next = new Node(2);
|
||||
head.next.next.next = new Node(1);
|
||||
printLinkedList(head);
|
||||
System.out.print(isPalindrome1(head) + " | ");
|
||||
System.out.print(isPalindrome2(head) + " | ");
|
||||
System.out.println(isPalindrome3(head) + " | ");
|
||||
printLinkedList(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(2);
|
||||
head.next.next.next.next = new Node(1);
|
||||
printLinkedList(head);
|
||||
System.out.print(isPalindrome1(head) + " | ");
|
||||
System.out.print(isPalindrome2(head) + " | ");
|
||||
System.out.println(isPalindrome3(head) + " | ");
|
||||
printLinkedList(head);
|
||||
System.out.println("=========================");
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue