├── README.md ├── TestOffer29.java ├── TestOffer32.java ├── TestOffer34.java ├── TestOffer35.java ├── TestOffer36.java ├── TestOffer40.java ├── TestOffer43.java ├── TestOffer44.java ├── TestOffer45.java ├── TestOffer46.java ├── TestOffer52.java ├── TestOffer30_1.java ├── TestOffer55.java ├── TreeNode.java ├── TestOffer39.java ├── TestOffer10.java ├── TestOffer11_1.java ├── TestOffer2.java ├── TestOffer47.java ├── TestOffer10_1.java ├── TestOffer39_2.java ├── TestOffer51.java ├── TestOffer54.java ├── TestOffer58.java ├── TestOffer41.java ├── TestOffer11.java ├── TestOffer56.java ├── TestOffer13.java ├── TestOffer31.java ├── TestOffer57.java ├── TestOffer9.java ├── TestOffer39_1.java ├── TestOffer63.java ├── TestOffer22.java ├── TestOffer14.java ├── TestOffer28.java ├── TestOffer49.java ├── TestOffer16.java ├── TestOffer18.java ├── TestOffer4.java ├── TestOffer17_1.java ├── TestOffer21.java ├── TestOffer3.java ├── TestOffer59.java ├── TestOffer65.java ├── TestOffer8.java ├── TestOffer33.java ├── TestOffer37.java ├── TestOffer5.java ├── TestOffer15.java ├── TestOffer65_01.java ├── TestOffer7.java ├── TestOffer19.java ├── TestOffer12.java ├── TestOffer42.java ├── TestOffer17.java ├── TestOffer61.java ├── TestOffer67.java ├── TestOffer64.java ├── TestOffer53.java ├── TestOffer23.java ├── TestOffer42_1.java ├── TestOffer20.java ├── TestOffer24.java ├── TestOffer25.java ├── TestOffer6.java ├── TestOffer60.java ├── TestOffer29_1.java ├── TestOffer27.java ├── TestOffer7_1.java ├── TestOffer66.java ├── TestOffer41_1.java ├── TestOffer38.java └── TestOffer26.java /README.md: -------------------------------------------------------------------------------- 1 | # SwordOffer 2 | work 3 | -------------------------------------------------------------------------------- /TestOffer29.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deserialization/SwordOffer/master/TestOffer29.java -------------------------------------------------------------------------------- /TestOffer32.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deserialization/SwordOffer/master/TestOffer32.java -------------------------------------------------------------------------------- /TestOffer34.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deserialization/SwordOffer/master/TestOffer34.java -------------------------------------------------------------------------------- /TestOffer35.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deserialization/SwordOffer/master/TestOffer35.java -------------------------------------------------------------------------------- /TestOffer36.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deserialization/SwordOffer/master/TestOffer36.java -------------------------------------------------------------------------------- /TestOffer40.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deserialization/SwordOffer/master/TestOffer40.java -------------------------------------------------------------------------------- /TestOffer43.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deserialization/SwordOffer/master/TestOffer43.java -------------------------------------------------------------------------------- /TestOffer44.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deserialization/SwordOffer/master/TestOffer44.java -------------------------------------------------------------------------------- /TestOffer45.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deserialization/SwordOffer/master/TestOffer45.java -------------------------------------------------------------------------------- /TestOffer46.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deserialization/SwordOffer/master/TestOffer46.java -------------------------------------------------------------------------------- /TestOffer52.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deserialization/SwordOffer/master/TestOffer52.java -------------------------------------------------------------------------------- /TestOffer30_1.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/deserialization/SwordOffer/master/TestOffer30_1.java -------------------------------------------------------------------------------- /TestOffer55.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer55 { 4 | 5 | public boolean isNumeric(char[] str) { 6 | String string = String.valueOf(str); 7 | return string.matches("[\\+-]?[0-9]*(\\.[0-9]*)?([eE][\\+-]?[0-9]+)?"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /TreeNode.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TreeNode{ 4 | int data; 5 | int root; 6 | TreeNode left; 7 | TreeNode right; 8 | TreeNode next = null; 9 | public TreeNode(int data) { 10 | super(); 11 | this.data = data; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /TestOffer39.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer39 { 4 | 5 | public int TreeDeep(TreeNode root){ 6 | if (root == null) { 7 | return 0; 8 | } 9 | int pNodeleft = TreeDeep(root.left); 10 | int pNoderight = TreeDeep(root.right); 11 | //return pNodeleft > pNoderight ? pNodeleft + 1: pNoderight + 1; 12 | return Math.max(pNodeleft, pNoderight) + 1; 13 | } 14 | } -------------------------------------------------------------------------------- /TestOffer10.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | 4 | public class TestOffer10 { 5 | public int numberOf(int n) { 6 | int count = 0; 7 | while(n != 0){ 8 | n &= n-1; 9 | count++; 10 | } 11 | return count; 12 | } 13 | public static void main(String[] args) { 14 | TestOffer10 test=new TestOffer10(); 15 | System.out.println(test.numberOf(9)); 16 | 17 | } 18 | 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /TestOffer11_1.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | 4 | public class TestOffer11_1 { 5 | public static double Power(double base,double exponent) { 6 | double result = base; 7 | double n = exponent; 8 | if (n < 0) { 9 | exponent = -exponent; 10 | }else if (n == 0) { 11 | return 1; 12 | }else{ 13 | for (int i = 1; i < n; i++) { 14 | result *= base; 15 | } 16 | } 17 | return n < 0 ? 1 / result : result; 18 | } 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /TestOffer2.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer2 { 4 | /* private static final TestOffer2 instance = new TestOffer2(); 5 | private TestOffer2(){}; 6 | public static TestOffer2 getInstance() { 7 | return instance; 8 | }*/ 9 | private static TestOffer2 instance = null; 10 | private TestOffer2() { 11 | 12 | } 13 | private static TestOffer2 instance(){ 14 | if (instance == null) { 15 | instance = new TestOffer2(); 16 | } 17 | return instance; 18 | } 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /TestOffer47.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.Arrays; 4 | 5 | 6 | public class TestOffer47 { 7 | public int add(int num1, int num2){ 8 | do { 9 | int sum = num1 ^ num2; 10 | int carry = (num1 & num2) << 1; 11 | num1 = sum; 12 | num2 = carry; 13 | 14 | } while (num2 != 0); 15 | return num1; 16 | } 17 | public static void main(String[] args) { 18 | TestOffer47 p_Test_47 = new TestOffer47(); 19 | System.out.println(p_Test_47.add(10,12)); 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /TestOffer10_1.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | 4 | public class TestOffer10_1 { 5 | 6 | public int numberOf(int n, int m) { 7 | int x = n ^ m; 8 | int count = 0; 9 | /* while(x != 0){ 10 | x &= x-1; 11 | count++; 12 | }*/ 13 | int i; 14 | for (i = 0; x != 0; i++) { 15 | x = x & (x - 1); 16 | } 17 | return i; 18 | } 19 | 20 | public static void main(String[] args) { 21 | TestOffer10_1 test=new TestOffer10_1(); 22 | System.out.println(test.numberOf(9,2)); 23 | } 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /TestOffer39_2.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer39_2 { 4 | 5 | public boolean isBalance(TreeNode root) { 6 | if (root == null) { 7 | return true; 8 | } 9 | if (Math.abs(deepth(root.left) - deepth(root.right)) > 1) { 10 | return false; 11 | }else{ 12 | return isBalance(root.left) && isBalance(root.right); 13 | } 14 | } 15 | 16 | private int deepth(TreeNode root) { 17 | // TODO Auto-generated method stub 18 | if (root == null) { 19 | return 0; 20 | } 21 | return Math.max(deepth(root.left), deepth(root.right)); 22 | } 23 | } -------------------------------------------------------------------------------- /TestOffer51.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | 4 | 5 | public class TestOffer51 { 6 | 7 | public boolean duplicate(int[] numbers, int length, int[] duplicate) { 8 | int temp; 9 | if (length <= 1) { 10 | return false; 11 | } 12 | for (int i = 0; i < length; i++) { 13 | while(numbers[i] != i){ 14 | if (numbers[numbers[i]] != numbers[i]) { 15 | temp = numbers[numbers[i]]; 16 | numbers[numbers[i]] = numbers[i]; 17 | numbers[i] = temp; 18 | }else{ 19 | duplicate[0] = numbers[i]; 20 | return true; 21 | } 22 | } 23 | } 24 | return false; 25 | } 26 | } -------------------------------------------------------------------------------- /TestOffer54.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer54 { 4 | 5 | int count[] = new int[256]; 6 | int index = 1; 7 | 8 | public void Insert(char ch) { 9 | if (count[ch] == 0) { 10 | count[ch] = index++; 11 | } else { 12 | count[ch] = -1; 13 | } 14 | } 15 | 16 | public char FirstAppearingOnce() { 17 | int temp = Integer.MAX_VALUE; 18 | char ch = '#'; 19 | for (int i = 0; i < 256; i++) { 20 | if (count[i] != 0 && count[i] != -1 && count[i] < temp) { 21 | temp = count[i]; 22 | ch = (char) i; 23 | } 24 | } 25 | return ch; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /TestOffer58.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer58 { 4 | 5 | public TreeNode GetNext(TreeNode node) 6 | { 7 | if(node==null)return null; 8 | if(node.right!=null) 9 | { 10 | node=node.right; 11 | while(node.left!=null) 12 | { 13 | node=node.left; 14 | 15 | }return node; 16 | } 17 | while(node.next!=null) 18 | { 19 | if(node.next.left==node)return node.next; 20 | node=node.next; 21 | } 22 | return null; 23 | } 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /TestOffer41.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class TestOffer41 { 6 | 7 | public ArrayList FindNumberWithSum(int[] array, int sum) { 8 | ArrayList list = new ArrayList(); 9 | if (array == null || array.length < 2) { 10 | return list; 11 | } 12 | int i = 0, j = array.length - 1; 13 | while(i < j){ 14 | if (array[i] + array[j] == sum) { 15 | list.add(array[i]); 16 | list.add(array[j]); 17 | }else if (array[i] + array[j] > sum) { 18 | j--; 19 | }else{ 20 | i++; 21 | } 22 | } 23 | return list; 24 | } 25 | } -------------------------------------------------------------------------------- /TestOffer11.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | 4 | public class TestOffer11 { 5 | 6 | public double Power(double base, int n) { 7 | double res = 1, curr = base; 8 | int exponent; 9 | if (n > 0) { 10 | exponent = n; 11 | } else if (n < 0) { 12 | if (base == 0) 13 | throw new RuntimeException("分母不能为0"); 14 | exponent = -n; 15 | } else {// n==0 16 | return 1;// 0的0次方 17 | } 18 | while (exponent != 0) { 19 | if ((exponent & 1) == 1) 20 | res *= curr; 21 | curr *= curr;// 翻倍 22 | exponent >>= 1;// 右移一位 23 | } 24 | return n >= 0 ? res : (1 / res); 25 | } 26 | 27 | } 28 | 29 | 30 | -------------------------------------------------------------------------------- /TestOffer56.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer56 { 4 | public class Solution { 5 | 6 | public Node EntryNodeOfLoop(Node pHead) 7 | { 8 | if (pHead == null || pHead.next == null) 9 | return null; 10 | Node p1 = pHead; 11 | Node p2 = pHead; 12 | while (p2 != null && p2.next != null) { 13 | p1 = p1.next; 14 | p2 = p2.next.next; 15 | if (p1 == p2) { 16 | p1 = pHead; 17 | while (p1 != p2) { 18 | p1 = p1.next; 19 | p2 = p2.next; 20 | } 21 | if (p1 == p2) 22 | return p1; 23 | } 24 | } 25 | return null; 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /TestOffer13.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import javax.xml.transform.Templates; 4 | 5 | public class TestOffer13 { 6 | public static Node deleteNode(Node head,Node delete){ 7 | //特殊情况 8 | if (head == null || delete == null) { 9 | return head; 10 | } 11 | //删除的是头结点,则直接删除 12 | if (head == delete) { 13 | return head.next; 14 | } 15 | //删除的是尾节点 则遍历一遍找到上一个结点,然后在删除 16 | if (delete.next == null) { 17 | Node temp = head; 18 | while(temp.next != delete){ 19 | temp = temp.next; 20 | } 21 | temp.next = null; 22 | }else{ 23 | delete.value = delete.next.value; 24 | delete.next = delete.next.next; 25 | } 26 | 27 | return head; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /TestOffer31.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.Arrays; 4 | 5 | public class TestOffer31 { 6 | 7 | public int FindGreatestSumOfSubArray(int[] array) { 8 | if (array == null || array.length == 0) { 9 | return 0; 10 | } 11 | int sum = array[0]; 12 | int tempSum = array[0]; 13 | for (int i = 1; i < array.length; i++) { 14 | tempSum = (tempSum < 0) ? array[i] : array[i] + tempSum; 15 | sum =(tempSum > sum) ? tempSum : sum; 16 | } 17 | return sum; 18 | } 19 | public static void main(String[] args) { 20 | TestOffer31 p=new TestOffer31(); 21 | int[] array={1,-2,3,10,-4,7,2,-5}; 22 | System.out.println(p.FindGreatestSumOfSubArray(array)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /TestOffer57.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer57 { 4 | 5 | public Node deleteDuplication(Node pHead) { 6 | 7 | if (pHead == null) 8 | return null; 9 | Node p = pHead; 10 | Node n = new Node(0); 11 | Node pre = n; 12 | n.next = pHead; 13 | boolean flag = false; 14 | while (p != null) { 15 | Node q = p.next; 16 | if (q == null) 17 | break; 18 | if (q.value == p.value) { 19 | while (q != null && q.value == p.value) { 20 | q = q.next; 21 | } 22 | pre.next = q; 23 | p = q; 24 | } else { 25 | if (!flag) { 26 | n.next = p; 27 | flag = true; 28 | } 29 | pre = p; 30 | p = q; 31 | } 32 | } 33 | return n.next; 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /TestOffer9.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | 4 | public class TestOffer9 { 5 | 6 | /* public int fib(int n){ 7 | int a = 0; 8 | int b = 1; 9 | int c = 0; 10 | if (n == 0) { 11 | return a; 12 | } 13 | if (n==1) { 14 | return b; 15 | } 16 | for (int i = 2; i <= n; i++) { 17 | c = a + b; 18 | a = b; 19 | b = c; 20 | } 21 | return c; 22 | }*/ 23 | public int fib(int n){ 24 | int a = 1; 25 | int b = 2; 26 | int c = 4; 27 | int d = 0; 28 | if (n == 1) { 29 | return 1; 30 | } 31 | if (n == 2) { 32 | return 2; 33 | } 34 | if (n == 3) { 35 | return 4; 36 | } 37 | for (int i = 4; i <= n; i++) { 38 | d = a + b + c; 39 | a = b; 40 | b = c; 41 | c = d; 42 | } 43 | return d; 44 | } 45 | 46 | 47 | } 48 | 49 | 50 | -------------------------------------------------------------------------------- /TestOffer39_1.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.lang.ProcessBuilder.Redirect; 4 | 5 | public class TestOffer39_1 { 6 | 7 | public boolean isBalance(TreeNode root) { 8 | if (root == null) { 9 | return true; 10 | } 11 | if (Math.abs(getHeight(root.left) - getHeight(root.right)) > 1) { 12 | return false; 13 | } 14 | return isBalance(root.left) && isBalance(root.right); 15 | } 16 | 17 | private int getHeight(TreeNode root) { 18 | // TODO Auto-generated method stub 19 | if (root == null) { 20 | return 0; 21 | } 22 | return max(getHeight(root.left), getHeight(root.right)) + 1; 23 | } 24 | 25 | private int max(int height, int height2) { 26 | // TODO Auto-generated method stub 27 | return (height > height2) ? height :height2; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /TestOffer63.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer63 { 4 | 5 | TreeNode KthNode(TreeNode pRoot, int k) 6 | { 7 | if(pRoot == null || k <= 0){ 8 | return null; 9 | } 10 | TreeNode[] result = new TreeNode[1]; 11 | KthNode(pRoot,k,new int[1],result); 12 | return result[0]; 13 | } 14 | void KthNode(TreeNode pRoot, int k,int[] count,TreeNode[] result){ 15 | if(result[0] != null || pRoot == null){ 16 | return; 17 | } 18 | KthNode(pRoot.left,k,count,result); 19 | count[0]++; 20 | if(count[0] == k){ 21 | result[0] = pRoot; 22 | } 23 | KthNode(pRoot.right,k,count,result); 24 | } 25 | 26 | } 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /TestOffer22.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.Stack; 4 | 5 | public class TestOffer22 { 6 | public boolean isPopOrder(int[] pushA,int[] popA) { 7 | Stack stack = new Stack(); 8 | if (pushA.length == 0 || popA.length == 0) { 9 | return false; 10 | } 11 | int popnum = 0; 12 | for (int i = 0; i < popA.length; i++) { 13 | stack.push(pushA[i]); 14 | while(!stack.isEmpty() && stack.peek() == popA[popnum]){ 15 | stack.pop(); 16 | popnum++; 17 | } 18 | } 19 | return stack.empty(); 20 | } 21 | public static void main(String[] args) { 22 | int[] array1={1,2,3,4,5}; 23 | int[] array2={4,3,5,2,1}; 24 | TestOffer22 test=new TestOffer22(); 25 | System.out.println(test.isPopOrder(array1, array2)); 26 | } 27 | 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /TestOffer14.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer14 { 4 | public static void reOrderArray(int[] array) { 5 | int i = 0; 6 | int j = array.length - 1; 7 | if (array == null ||array.length == 0) { 8 | return; 9 | } 10 | while(i < j){//while不满足的时候就交换,满足就移动 11 | while(i < j && isEven(array[i])){ 12 | i++; 13 | } 14 | while(i < j && isEven(array[j])){ 15 | j--; 16 | } 17 | int temp = array[j]; 18 | array[j] = array[i]; 19 | array[i] = temp; 20 | } 21 | } 22 | 23 | static boolean isEven(int n) { 24 | 25 | return (n % 2 == 1); 26 | } 27 | public static void main(String args[]) { 28 | int[] array = { 1, 2, 3, 4, 5, 6, 7 }; 29 | TestOffer14 test = new TestOffer14(); 30 | test.reOrderArray(array); 31 | for (int item : array) 32 | System.out.print(item); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /TestOffer28.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class TestOffer28 { 6 | public static void perm(char[] buf, int start, int end){ 7 | if (start == end) { 8 | for (int i = 0; i <= end; i++) { 9 | System.out.print(buf[i]); 10 | } 11 | System.out.println(); 12 | }else{ 13 | for (int i = start; i <= end; i++) { 14 | char temp = buf[start]; 15 | buf[start] = buf[i]; 16 | buf[i] = temp; 17 | perm(buf, start + 1, end); 18 | temp = buf[start]; 19 | buf[start] = buf[i]; 20 | buf[i] = temp; 21 | } 22 | } 23 | } 24 | public static void main(String[] args){ 25 | char[ ] buf=new char[10]; 26 | Scanner scanner=new Scanner(System.in); 27 | buf=scanner.next().toCharArray(); 28 | perm(buf, 0, buf.length-1); 29 | } 30 | 31 | 32 | } 33 | 34 | -------------------------------------------------------------------------------- /TestOffer49.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.Arrays; 4 | 5 | 6 | public class TestOffer49 { 7 | public int StrToInt(String string){ 8 | int result = 0; 9 | if (string == null || string.isEmpty()) { 10 | return 0; 11 | } 12 | int i = 0; 13 | if (string.charAt(0) == '+' || string.charAt(0) == '-') { 14 | i = 1; 15 | } 16 | for (; i < string.length(); i++) { 17 | char c = string.charAt(i); 18 | if (c <= '9' && c >= '0') { 19 | int j = c - '0'; 20 | result = result * 10 + j; 21 | }else{ 22 | return 0; 23 | } 24 | } 25 | if (string.charAt(0)=='-') { 26 | result = - result; 27 | } 28 | return result; 29 | } 30 | public static void main(String[] args) { 31 | TestOffer49 p_Test_49 = new TestOffer49(); 32 | System.out.println(p_Test_49.StrToInt("10")); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /TestOffer16.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer16 { 4 | public Node reverseNode(Node head){ 5 | Node pre = null; 6 | Node next = null; 7 | if (head == null) { 8 | return null; 9 | } 10 | while(head != null){ 11 | next = head.next; 12 | if (next == null) { 13 | next = head; 14 | } 15 | head.next = pre; 16 | pre = head; 17 | head = next; 18 | } 19 | return pre; 20 | } 21 | 22 | public static void main(String[] args) { 23 | Node head=new Node(1); 24 | Node second=new Node(2); 25 | Node third=new Node(3); 26 | Node forth=new Node(4); 27 | head.next=second; 28 | second.next=third; 29 | third.next=forth; 30 | TestOffer16 test=new TestOffer16(); 31 | Node resultNode=test.reverseNode(head); 32 | System.out.println(resultNode.value); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /TestOffer18.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer18 { 4 | public boolean subTree(TreeNode root1, TreeNode root2){ 5 | boolean flag = false; 6 | 7 | if (root1!=null && root2 != null) { 8 | if (root1.data == root2.data) { 9 | flag = HaveTree2(root1,root2); 10 | } 11 | if (!flag) { 12 | flag = HaveTree2(root1.left,root2); 13 | }if (!flag) { 14 | flag = HaveTree2(root1.right,root2); 15 | } 16 | } 17 | 18 | return flag; 19 | } 20 | 21 | private boolean HaveTree2(TreeNode root1, TreeNode root2) { 22 | // TODO Auto-generated method stub 23 | if (root1 == null) { 24 | return false; 25 | } 26 | if (root1 != null && root2 == null) { 27 | return true; 28 | } 29 | if (root1.data == root2.data) { 30 | return true; 31 | } 32 | return HaveTree2(root1.left, root2.left) && HaveTree2(root1.right, root2.right); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /TestOffer4.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | 4 | public class TestOffer4 { 5 | public static String fullSpace(StringBuffer stringBuffer){ 6 | int numspace = 0; 7 | char[] ch = stringBuffer.toString().toCharArray(); 8 | for (int i = 0; i < ch.length; i++) { 9 | if (ch[i] == ' ') { 10 | numspace++; 11 | } 12 | } 13 | char[] ch1 = new char[numspace * 2 + stringBuffer.length()]; 14 | int len = numspace * 2 + stringBuffer.length() - 1; 15 | for (int i = ch.length - 1; i >= 0; i--) { 16 | if (ch[i] != ' ') { 17 | ch1[len--] = ch[i]; 18 | }else{ 19 | ch1[len--] = '0'; 20 | ch1[len--] = '2'; 21 | ch1[len--] = '%'; 22 | } 23 | } 24 | return String.valueOf(ch1); 25 | } 26 | public static void main(String[] args) { 27 | StringBuffer stringBuffer = new StringBuffer(); 28 | stringBuffer.append("23 3 43"); 29 | System.out.println(fullSpace(stringBuffer)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /TestOffer17_1.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer17_1 { 4 | public static Node merge(Node node1,Node node2) { 5 | if (node1 == null) { 6 | return node2; 7 | } 8 | if (node2 == null) { 9 | return node1; 10 | } 11 | Node head = null; 12 | Node current = null; 13 | while(node1 != null && node2 != null){ 14 | if (node1.value <= node2.value) { 15 | if (head == null) { 16 | head = current = node1; 17 | }else{ 18 | current.next = node1; 19 | current = current.next; 20 | } 21 | node1 = node1.next; 22 | } 23 | else{ 24 | if (head == null) { 25 | head =current = node2; 26 | }else{ 27 | current.next = node2; 28 | current = current.next; 29 | } 30 | } 31 | node2 = node2.next; 32 | } 33 | if (node1 == null) { 34 | current.next = node2; 35 | }else{ 36 | current.next = node1; 37 | } 38 | return head; 39 | } 40 | } -------------------------------------------------------------------------------- /TestOffer21.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.Iterator; 4 | import java.util.Stack; 5 | 6 | public class TestOffer21 { 7 | Stack stack = new Stack(); 8 | Stack stack1 = new Stack(); 9 | public void push(int node){ 10 | stack.push(node); 11 | } 12 | public void pop(){ 13 | stack.pop(); 14 | } 15 | public int top(){ 16 | return stack.peek(); 17 | } 18 | public int min(){ 19 | int min = stack.peek(); 20 | while(!stack.isEmpty()){ 21 | int node = stack.pop(); 22 | if (node < min) { 23 | min = node; 24 | } 25 | stack1.push(node); 26 | } 27 | while(!stack1.isEmpty()){ 28 | stack.push(stack1.pop()); 29 | } 30 | 31 | return min; 32 | } 33 | public static void main(String[] args) { 34 | TestOffer21 test=new TestOffer21(); 35 | test.push(3); 36 | test.push(2); 37 | test.push(1); 38 | test.push(4); 39 | test.push(5); 40 | 41 | System.out.println(test.min()); 42 | } 43 | } 44 | 45 | 46 | -------------------------------------------------------------------------------- /TestOffer3.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer3 { 4 | public static boolean Find(int[][] array,int target){ 5 | int row = 0; 6 | int col = array[0].length - 1; 7 | while(row <= array.length - 1 && col >= 0){ 8 | if (target == array[row][col]) { 9 | return true; 10 | }else if (target >= array[row][col]) { 11 | row++; 12 | }else { 13 | col--; 14 | } 15 | } 16 | return false; 17 | } 18 | public static void main(String[] args) { 19 | int[][] testarray=new int[4][4]; 20 | testarray[0][0]=1; 21 | testarray[0][1]=2; 22 | testarray[0][2]=8; 23 | testarray[0][3]=9; 24 | testarray[1][0]=2; 25 | testarray[1][1]=4; 26 | testarray[1][2]=9; 27 | testarray[1][3]=12; 28 | testarray[2][0]=4; 29 | testarray[2][1]=7; 30 | testarray[2][2]=10; 31 | testarray[2][3]=13; 32 | testarray[3][0]=6; 33 | testarray[3][1]=8; 34 | testarray[3][2]=11; 35 | testarray[3][3]=15; 36 | System.out.println(Find(testarray, 11)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /TestOffer59.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer59 { 4 | 5 | boolean isSymmetrical(TreeNode pRoot) 6 | { 7 | TreeNode node = getMirror(pRoot); 8 | return isSymmetrical(pRoot,node); 9 | } 10 | boolean isSymmetrical(TreeNode pRoot,TreeNode node) 11 | { 12 | if(pRoot == null && node == null){ 13 | return true; 14 | }else if(pRoot == null || node == null){ 15 | return false; 16 | } 17 | if(pRoot.data == node.data){ 18 | return isSymmetrical(pRoot.left,node.left)&&isSymmetrical(pRoot.right,node.right); 19 | } 20 | return false; 21 | } 22 | 23 | TreeNode getMirror(TreeNode pRoot){ 24 | if (pRoot == null) { 25 | return null; 26 | } 27 | TreeNode root = new TreeNode(pRoot.data); 28 | root.right = getMirror(pRoot.left); 29 | root.left = getMirror(pRoot.right); 30 | return root; 31 | } 32 | 33 | } 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /TestOffer65.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | 6 | public class TestOffer65 { 7 | 8 | public ArrayList maxInWindows(int [] num, int size) 9 | { 10 | if(num==null||size<0){ 11 | return null; 12 | } 13 | ArrayList list=new ArrayList(); 14 | if(size==0){ 15 | return list; 16 | } 17 | ArrayList temp=null; 18 | int length=num.length; 19 | if(length(); 24 | for(int j=i;j>2; 16 | int minValue = array[0]; 17 | while(begin > end){ 18 | if (array[begin] > array[mid]) { 19 | end = mid; 20 | minValue = Math.min(minValue, array[mid]); 21 | }else if (array[begin] < array[mid]) { 22 | begin = mid; 23 | minValue =Math.min(minValue, array[mid]); 24 | }else { 25 | for (int i = begin; i <= end; i++) { 26 | minValue = Math.min(minValue, array[i]); 27 | } 28 | break; 29 | } 30 | 31 | } 32 | 33 | return minValue; 34 | } 35 | public static void main(String[] args) { 36 | int array[]={2,3,4,4}; 37 | int FindMinNum=minNum(array); 38 | System.out.println(FindMinNum); 39 | } 40 | 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- /TestOffer33.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.Arrays; 4 | import java.util.Comparator; 5 | 6 | public class TestOffer33 { 7 | public String printMinNumber(int[] number){ 8 | if (number == null || number.length == 0) { 9 | return null; 10 | } 11 | int len = number.length; 12 | String[] string = new String[len]; 13 | StringBuilder sb = new StringBuilder(); 14 | for (int i = 0; i < string.length; i++) { 15 | string[i] = String.valueOf(number[i]); 16 | } 17 | Arrays.sort(string, new Comparator(){ 18 | @Override 19 | public int compare(String s1, String s2){ 20 | String c1 = s1 + s2; 21 | String c2 = s2 + s1; 22 | return c1.compareTo(c2); 23 | } 24 | 25 | }); 26 | for (int i = 0; i < string.length; i++) { 27 | sb.append(string[i]); 28 | } 29 | 30 | 31 | return sb.toString(); 32 | 33 | } 34 | public static void main(String[] args) { 35 | TestOffer33 test=new TestOffer33(); 36 | int[] array={3,32,321}; 37 | System.out.println(test.printMinNumber(array)); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /TestOffer37.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.List; 4 | 5 | public class TestOffer37 { 6 | 7 | public Node FinFirstCommonNode(Node head1, Node head2){ 8 | int pListLengthOf1 = 0; 9 | int pListLengthOf2 = 0; 10 | Node temp = head1; 11 | while(temp != null){ 12 | pListLengthOf1++; 13 | temp = temp.next; 14 | } 15 | temp = head2; 16 | while(temp != null){ 17 | pListLengthOf2++; 18 | temp = temp.next; 19 | } 20 | int pNodeDif = pListLengthOf1 - pListLengthOf2; 21 | Node pListNodeLong = head1; 22 | Node pListNodeShort = head2; 23 | if (pNodeDif < 0) { 24 | pListNodeLong = head2; 25 | pListNodeShort = head1; 26 | pNodeDif = pListLengthOf2 - pListLengthOf1; 27 | } 28 | for (int i = 0; i < pNodeDif; i++) { 29 | pListNodeLong = pListNodeLong.next; 30 | } 31 | while(pListNodeLong != null && pListNodeShort != null 32 | && pListNodeLong.value != pListNodeShort.value){ 33 | pListNodeLong = pListNodeLong.next; 34 | pListNodeShort = pListNodeShort.next; 35 | } 36 | Node pNode = pListNodeLong; 37 | return pNode; 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /TestOffer5.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Stack; 5 | 6 | 7 | public class TestOffer5 { 8 | 9 | /* public static ArrayList printNode(Node node){ 10 | Stack stack = new Stack<>(); 11 | while(node != null){ 12 | stack.push(node.value); 13 | node = node.next; 14 | } 15 | ArrayList list = new ArrayList<>(); 16 | while(!stack.isEmpty()){ 17 | list.add(stack.pop()); 18 | } 19 | return list; 20 | }*/ 21 | 22 | public void printNode(Node node){ 23 | if (node != null) { 24 | if (node.next != null) { 25 | printNode(node.next); 26 | } 27 | } 28 | System.out.println(node.value); 29 | } 30 | 31 | public static void main(String args[]) { 32 | Node node1 = new Node(1); 33 | Node node2 = new Node(2); 34 | Node node3 = new Node(3); 35 | /* node1.value = 1; 36 | node2.value = 2; 37 | node3.value = 3;*/ 38 | node1.next = node2; 39 | node2.next = node3; 40 | TestOffer5 test = new TestOffer5(); 41 | //System.out.println(test.printNode(node1)); 42 | test.printNode(node1); 43 | } 44 | 45 | } 46 | 47 | 48 | -------------------------------------------------------------------------------- /TestOffer15.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer15 { 4 | public static Node FindToTile(Node head,int k) { 5 | //就是输入的链表头指针为null,那么整个链表也就是为空,所以此时倒数k个节点也就是位空。k=0,也是空,没有意义 6 | if (head == null || k <= 0) { 7 | return null; 8 | } 9 | Node aNode = head; 10 | Node bNode = null; 11 | //就是如果节点少于k,在for循 环中可能就会出现空指针异常。所以在for循环中就应该加一个if判断。 12 | for (int i = 0; i < k - 1; i++) { 13 | if (aNode.next != null) { 14 | aNode = aNode.next; 15 | }else{ 16 | return null; 17 | } 18 | } 19 | bNode = head; 20 | while(aNode.next != null){ 21 | aNode = aNode.next; 22 | bNode = bNode.next; 23 | } 24 | return head; 25 | } 26 | public static void main(String[] args) { 27 | Node head=new Node(0); 28 | Node second=new Node(1); 29 | Node third=new Node(2); 30 | Node forth=new Node(3); 31 | head.next=second; 32 | second.next=third; 33 | third.next=forth; 34 | head.value=1; 35 | second.value=2; 36 | third.value=3; 37 | forth.value=4; 38 | TestOffer15 test=new TestOffer15(); 39 | Node resultNode=test.FindToTile(head, 3); 40 | System.out.println(resultNode.value); 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /TestOffer65_01.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.Arrays; 4 | import java.util.LinkedList; 5 | 6 | public class TestOffer65_01 { 7 | 8 | public static void main(String[] args) { 9 | TestOffer65_01 test = new TestOffer65_01() ; 10 | int []arr = {4 ,3 ,5 ,4 ,3 ,3 ,6 ,7} ; 11 | int w = 3 ; 12 | int []res = test.getMaxWindow(arr, w) ; 13 | System.out.println(Arrays.toString(res)); 14 | } 15 | public int[] getMaxWindow(int[] array, int w){ 16 | if (array == null || w < 1 || array.length < w) { 17 | return null; 18 | } 19 | LinkedList qmax = new LinkedList(); 20 | int[] res = new int[array.length - w + 1]; 21 | int index = 0; 22 | for (int i = 0; i < array.length; i++) { 23 | while(!qmax.isEmpty() && array[qmax.peekLast()] <= array[i]){ 24 | qmax.pollLast();//弹出队尾元素的下标 25 | } 26 | qmax.addLast(i);//放入队尾下标 27 | if (qmax.peekFirst() == i - w) {//队头取值但不移除 28 | qmax.pollFirst();//弹出过期元素 29 | } 30 | if (i > w - 1) { 31 | //从 w -1 位开始保存窗口最大值 32 | res[index++] = array[qmax.peekFirst()]; 33 | } 34 | 35 | } 36 | return res; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /TestOffer7.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.io.PushbackInputStream; 4 | import java.util.Stack; 5 | 6 | /** 7 | 解题步骤:就是先放入一个栈中,然后在倒入另外一个栈中,如果是一个栈中为空,另外一个栈不为空, 8 | 那么就应该有元素的弹入到没有元素里面的栈中,然后在把最终的那个栈弹出。 9 | 10 | * 11 | */ 12 | public class TestOffer7 { 13 | 14 | Stack stack1 = new Stack(); 15 | Stack stack2 = new Stack(); 16 | public void push(int node) { 17 | stack1.push(node); 18 | } 19 | public int pop() { 20 | while(!stack1.isEmpty()){ 21 | stack2.push(stack1.pop()); 22 | } 23 | int first = stack2.pop(); 24 | while(!stack2.isEmpty()){ 25 | stack1.push(stack2.pop()); 26 | } 27 | return first; 28 | } 29 | public static void main(String[] args) { 30 | TestOffer7 demo05 = new TestOffer7(); 31 | demo05.push(1); 32 | demo05.push(2); 33 | demo05.push(5); 34 | demo05.push(4); 35 | System.out.println(demo05.pop()); 36 | System.out.println(demo05.pop()); 37 | demo05.push(3); 38 | System.out.println(demo05.pop()); 39 | System.out.println(demo05.pop()); 40 | System.out.println(demo05.pop()); 41 | } 42 | 43 | } 44 | 45 | 46 | -------------------------------------------------------------------------------- /TestOffer19.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer19 { 4 | /* public static void main(String[] args) { 5 | TreeNode root1=new TreeNode(8); 6 | TreeNode node1=new TreeNode(8); 7 | TreeNode node2=new TreeNode(7); 8 | TreeNode node3=new TreeNode(9); 9 | TreeNode node4=new TreeNode(2); 10 | TreeNode node5=new TreeNode(4); 11 | TreeNode node6=new TreeNode(7); 12 | root1.left=node1; 13 | root1.right=node2; 14 | node1.left=node3; 15 | node1.right=node4; 16 | node4.left=node5; 17 | node4.right=node6; 18 | 19 | TestOffer19 test=new TestOffer19(); 20 | TreeNode rootTreeNode=test.Mirror(root1); 21 | System.out.println(root1.left.data); 22 | System.out.println(root1.right.data); 23 | }*/ 24 | public TreeNode Mirror(TreeNode root1){ 25 | if (root1 == null) { 26 | return null; 27 | } 28 | if (root1.left == null && root1.right == null) { 29 | return null; 30 | } 31 | TreeNode temp = root1.left; 32 | root1.left = root1.right; 33 | root1.right = temp; 34 | if (root1.left != null) { 35 | Mirror(root1.left); 36 | } 37 | if (root1.right != null) { 38 | Mirror(root1.right); 39 | } 40 | return root1; 41 | 42 | } 43 | 44 | } -------------------------------------------------------------------------------- /TestOffer12.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer12 { 4 | 5 | 6 | public static void Print1ToMaxOfNDigits_3(int n){ 7 | if(n < 0){ 8 | return; 9 | } 10 | StringBuffer s = new StringBuffer(n); 11 | for(int i = 0; i < n; i++){ 12 | s.append('0'); 13 | } 14 | for(int i = 0; i < 10; i++){ 15 | 16 | s.setCharAt(0, (char) (i+'0')); 17 | Print1ToMaxOfNDigits_3_Recursely(s, n, 0); 18 | } 19 | 20 | } 21 | public static void Print1ToMaxOfNDigits_3_Recursely(StringBuffer s, int n , int index){ 22 | if(index == n - 1){ 23 | PrintNumber(s); 24 | return; 25 | } 26 | 27 | for(int i = 0; i < 10; i++){ 28 | s.setCharAt(index+1, (char) (i+'0')); 29 | Print1ToMaxOfNDigits_3_Recursely(s, n, index+1); 30 | } 31 | } 32 | public static void PrintNumber(StringBuffer s){//碰到第一个非零的数字后才开始打印 33 | boolean isBeginning0 = true; 34 | for(int i = 0; i < s.length(); i++){ 35 | if(isBeginning0 && s.charAt(i) != '0'){ 36 | isBeginning0 = false; 37 | } 38 | if(!isBeginning0){ 39 | System.out.print(s.charAt(i)); 40 | } 41 | } 42 | System.out.println(); 43 | } 44 | } -------------------------------------------------------------------------------- /TestOffer42.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | import org.omg.PortableServer.ID_ASSIGNMENT_POLICY_ID; 7 | 8 | public class TestOffer42 { 9 | public String ReverseSentence(String string) { 10 | if (string == null) { 11 | return string; 12 | } 13 | char[] cs = string.toCharArray(); 14 | int begin = 0; 15 | int end = cs.length - 1; 16 | reverse(cs, begin, end); 17 | begin = end = 0; 18 | while(begin < cs.length){ 19 | if (cs[begin] == ' ') { 20 | begin++; 21 | end++; 22 | }else if (end == cs.length || cs[end] == ' ') { 23 | reverse(cs, begin, --end); 24 | begin = ++end; 25 | }else { 26 | end++; 27 | } 28 | } 29 | return new String(cs); 30 | } 31 | 32 | private void reverse(char[] string, int begin, int end) { 33 | // TODO Auto-generated method stub 34 | while(begin <= end){ 35 | char temp = string[begin]; 36 | string[begin] = string[end]; 37 | string[end] = temp; 38 | begin++; 39 | end--; 40 | } 41 | } 42 | public static void main(String[] args) { 43 | String str = "hello world"; 44 | TestOffer42 r = new TestOffer42(); 45 | String s = r.ReverseSentence(str); 46 | System.out.println(s); 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /TestOffer17.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer17 { 4 | 5 | public Node merge(Node node1,Node node2){ 6 | if (node1 == null) { 7 | return node2; 8 | } 9 | if (node2 == null) { 10 | return node1; 11 | } 12 | Node head = null; 13 | if (node1.value <= node2.value) { 14 | head = node1; 15 | head.next = merge(node1.next, node2); 16 | } 17 | if (node1.value > node2.value) { 18 | head = node2; 19 | head.next = merge(node1, node2.next); 20 | } 21 | return head; 22 | } 23 | public static void main(String[] args) { 24 | Node list1 = new Node(1); 25 | list1.next = new Node(3); 26 | list1.next.next = new Node(5); 27 | list1.next.next.next = new Node(7); 28 | // Node list2 = null; 29 | Node list2 = new Node(2); 30 | list2.next = new Node(2); 31 | list2.next.next = new Node(6); 32 | list2.next.next.next = new Node(8); 33 | TestOffer17 m = new TestOffer17(); 34 | Node mergeList = m.merge(list1, list2); 35 | m.printList(mergeList); 36 | 37 | } 38 | 39 | void printList(Node mergeList) { 40 | while (mergeList != null) { 41 | System.out.print(mergeList.value + ","); 42 | mergeList = mergeList.next; 43 | } 44 | } 45 | 46 | 47 | } -------------------------------------------------------------------------------- /TestOffer61.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer61 { 4 | 5 | 6 | String Serialize(TreeNode root) { 7 | if(root == null) 8 | return ""; 9 | StringBuilder sb = new StringBuilder(); 10 | Serialize2(root, sb); 11 | return sb.toString(); 12 | } 13 | 14 | void Serialize2(TreeNode root, StringBuilder sb) { 15 | if(root == null) { 16 | sb.append("#,"); 17 | return; 18 | } 19 | sb.append(root.data); 20 | sb.append(','); 21 | Serialize2(root.left, sb); 22 | Serialize2(root.right, sb); 23 | } 24 | 25 | int index = -1; 26 | 27 | TreeNode Deserialize(String str) { 28 | if(str.length() == 0) 29 | return null; 30 | String[] strs = str.split(","); 31 | return Deserialize2(strs); 32 | } 33 | 34 | TreeNode Deserialize2(String[] strs) { 35 | index++; 36 | if(!strs[index].equals("#")) { 37 | TreeNode root = new TreeNode(0); 38 | root.data = Integer.parseInt(strs[index]); 39 | root.left = Deserialize2(strs); 40 | root.right = Deserialize2(strs); 41 | return root; 42 | } 43 | return null; 44 | } 45 | } 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /TestOffer67.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | 4 | public class TestOffer67 { 5 | 6 | public int movingCount(int threshold, int rows, int cols) 7 | { 8 | boolean[] visited=new boolean[rows*cols]; 9 | return movingCountCore(threshold, rows, cols, 0,0,visited); 10 | } 11 | private int movingCountCore(int threshold, int rows, int cols, 12 | int row,int col,boolean[] visited) { 13 | if(row<0||row>=rows||col<0||col>=cols) return 0; 14 | int i=row*cols+col; 15 | if(visited[i]||!checkSum(threshold,row,col)) return 0; 16 | visited[i]=true; 17 | return 1+movingCountCore(threshold, rows, cols,row,col+1,visited) 18 | +movingCountCore(threshold, rows, cols,row,col-1,visited) 19 | +movingCountCore(threshold, rows, cols,row+1,col,visited) 20 | +movingCountCore(threshold, rows, cols,row-1,col,visited); 21 | } 22 | private boolean checkSum(int threshold, int row, int col) { 23 | int sum=0; 24 | while(row!=0){ 25 | sum+=row%10; 26 | row=row/10; 27 | } 28 | while(col!=0){ 29 | sum+=col%10; 30 | col=col/10; 31 | } 32 | if(sum>threshold) return false; 33 | return true; 34 | } 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /TestOffer64.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.LinkedList; 4 | 5 | public class TestOffer64 { 6 | 7 | 8 | LinkedList list = new LinkedList(); 9 | 10 | 11 | public void Insert(Integer num) { 12 | if (list.size()==0||num < list.getFirst()) { 13 | list.addFirst(num); 14 | } else { 15 | boolean insertFlag = false; 16 | for (Integer e : list) { 17 | if (num < e) { 18 | int index = list.indexOf(e); 19 | list.add(index, num); 20 | insertFlag = true; 21 | break; 22 | } 23 | } 24 | if (!insertFlag) { 25 | list.addLast(num); 26 | } 27 | } 28 | 29 | 30 | } 31 | 32 | 33 | public Double GetMedian() { 34 | if (list.size() == 0) { 35 | return null; 36 | } 37 | 38 | 39 | if (list.size() % 2 == 0) { 40 | int i = list.size() / 2; 41 | Double a = Double.valueOf(list.get(i - 1) + list.get(i)); 42 | return a / 2; 43 | } 44 | list.get(0); 45 | Double b = Double.valueOf(list.get((list.size() + 1) / 2 - 1)); 46 | return Double.valueOf(list.get((list.size() + 1) / 2 - 1)); 47 | 48 | 49 | } 50 | } 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /TestOffer53.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer53 { 4 | 5 | public boolean match(char[] str, char[] pattern) 6 | { 7 | return matchTwo(str, 0, str.length, pattern, 0, pattern.length); 8 | 9 | } 10 | 11 | private boolean matchTwo(char[] str, int i, int length1, char[] pattern, int j, int length2) { 12 | if (i == length1 && j == length2) { 13 | return true; 14 | } 15 | if (i == length1 && j != length2) { 16 | while (j != length2) { 17 | if (pattern[j] != '*' && (j + 1 >= length2 || pattern[j + 1] != '*')) { 18 | return false; 19 | } 20 | j++; 21 | } 22 | return true; 23 | } 24 | if (i != length1 && j == length2) { 25 | return false; 26 | } 27 | if (j + 1 == length2) { 28 | if (str[i] == pattern[j] || pattern[j] == '.') 29 | return matchTwo(str, i + 1, length1, pattern, j + 1, length2); 30 | else { 31 | return false; 32 | } 33 | } 34 | if ((str[i] == pattern[j] || pattern[j] == '.') && pattern[j + 1] != '*') 35 | return matchTwo(str, i + 1, length1, pattern, j + 1, length2); 36 | if ((str[i] == pattern[j] || pattern[j] == '.') && pattern[j + 1] == '*') 37 | return matchTwo(str, i, length1, pattern, j + 2, length2) 38 | || matchTwo(str, i + 1, length1, pattern, j, length2); 39 | if (pattern[j + 1] == '*') 40 | return matchTwo(str, i, length1, pattern, j + 2, length2); 41 | return false; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /TestOffer23.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.Queue; 6 | import java.util.Stack; 7 | 8 | public class TestOffer23 { 9 | 10 | public static ArrayList printNode(TreeNode root) { 11 | ArrayList list = new ArrayList(); 12 | if (root == null) { 13 | return list; 14 | } 15 | Queue queue = new LinkedList<>(); 16 | queue.add(root); 17 | while(!queue.isEmpty()){ 18 | TreeNode node = queue.poll(); 19 | list.add(node.data); 20 | if (node.left != null) { 21 | queue.add(node.left); 22 | } 23 | if (node.right != null) { 24 | queue.add(node.right); 25 | } 26 | } 27 | return list; 28 | } 29 | public static void main(String[] args) { 30 | TreeNode root = new TreeNode(8); 31 | TreeNode node1 = new TreeNode(6); 32 | TreeNode node2 = new TreeNode(10); 33 | TreeNode node3 = new TreeNode(5); 34 | TreeNode node4 = new TreeNode(7); 35 | TreeNode node5 = new TreeNode(9); 36 | TreeNode node6 = new TreeNode(11); 37 | 38 | root.left = node1; 39 | root.right = node2; 40 | node1.left = node3; 41 | node1.right = node4; 42 | node2.left = node5; 43 | node2.right = node6; 44 | TestOffer23 test=new TestOffer23(); 45 | System.out.println(test.printNode(root)); 46 | } 47 | 48 | } 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /TestOffer42_1.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.time.OffsetTime; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | import org.omg.PortableServer.ID_ASSIGNMENT_POLICY_ID; 8 | 9 | public class TestOffer42_1 { 10 | public String leftRotateString(String string,int n){ 11 | int len = string.length(); 12 | if (len == 0) { 13 | return ""; 14 | } 15 | n = n % len; 16 | string += string; 17 | return string.substring(n, len + n); 18 | } 19 | public String leftRotateString1(String string,int n){ 20 | char[] chars = string.toCharArray(); 21 | if (chars.length < n) { 22 | return " "; 23 | } 24 | reverse(chars, 0, n - 1); 25 | reverse(chars, n, chars.length - 1); 26 | reverse(chars, 0, chars.length - 1); 27 | StringBuilder sb = new StringBuilder(chars.length); 28 | for (char c : chars) { 29 | sb.append(c); 30 | } 31 | return sb.toString(); 32 | } 33 | private void reverse(char[] chars, int i, int j) { 34 | // TODO Auto-generated method stub 35 | char temp ; 36 | while(i < j){ 37 | temp = chars[i]; 38 | chars[i] = chars[j]; 39 | chars[j] = temp; 40 | i++; 41 | j--; 42 | } 43 | } 44 | public static void main(String[] args) { 45 | TestOffer42_1 p=new TestOffer42_1(); 46 | String string="student"; 47 | //p.leftRotateString(string,2); 48 | System.out.println(p.leftRotateString1(string,2)); 49 | } 50 | 51 | 52 | } -------------------------------------------------------------------------------- /TestOffer20.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.ArrayList; 4 | 5 | 6 | /** 7 | * @author Administrator 8 | *输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字, 9 | *例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 10 | * 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10. 11 | */ 12 | public class TestOffer20 { 13 | public static void main(String[] args) { 14 | int[][] array = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; 15 | TestOffer20 testCircle = new TestOffer20(); 16 | System.out.println(testCircle.printMatrix(array)); 17 | } 18 | 19 | public ArrayList printMatrix(int[][] matrix){ 20 | if (matrix == null) { 21 | return null; 22 | } 23 | ArrayList list = new ArrayList<>(); 24 | int row = matrix.length; 25 | int col = matrix[0].length; 26 | int left = 0; 27 | int top = 0; 28 | int right = col - 1; 29 | int bottom = row - 1; 30 | while(left <= right && top <= bottom){ 31 | //从左向右 32 | for (int i = left; i <= right; i++) { 33 | list.add(matrix[top][i]); 34 | } 35 | //从上到下(从下一行开始向下走) 36 | for (int j = top + 1; j <= bottom; j++) { 37 | list.add(matrix[j][right]); 38 | } 39 | //从右到左 40 | if (top != bottom) { 41 | for (int k = right - 1; k >= left; k--) { 42 | list.add(matrix[bottom][k]); 43 | } 44 | } 45 | //从下到上 46 | if (left != right) { 47 | for (int l = bottom - 1; l > top; l--) { 48 | list.add(matrix[l][left]); 49 | } 50 | } 51 | top++;left++;right--;bottom--; 52 | } 53 | return list; 54 | 55 | } 56 | } 57 | 58 | 59 | -------------------------------------------------------------------------------- /TestOffer24.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.Stack; 4 | 5 | public class TestOffer24 { 6 | public static void findPath(TreeNode root,int sum){ 7 | if (root == null) { 8 | return; 9 | } 10 | Stack stack = new Stack(); 11 | int currentSum = 0; 12 | findPath(root, sum,stack,currentSum); 13 | } 14 | 15 | private static void findPath(TreeNode root, int sum, Stack stack, int currentSum) { 16 | // TODO Auto-generated method stub 17 | currentSum += root.data; 18 | stack.push(root.data); 19 | if (root.left == null && root.right == null) { 20 | if (currentSum == sum) { 21 | System.out.println("找到路径"); 22 | for (int path : stack) { 23 | System.out.print(path +" "); 24 | } 25 | System.out.println(); 26 | } 27 | } 28 | if (root.left != null) { 29 | findPath(root.left, sum, stack, currentSum); 30 | } 31 | if (root.right != null) { 32 | findPath(root.right, sum, stack, currentSum); 33 | } 34 | stack.pop(); 35 | } 36 | public static void main(String[] args) { 37 | { 38 | TreeNode root1 = new TreeNode(10); 39 | TreeNode node1 = new TreeNode(5); 40 | TreeNode node2 = new TreeNode(12); 41 | TreeNode node3 = new TreeNode(4); 42 | TreeNode node4 = new TreeNode(7); 43 | root1.left = node1; 44 | root1.right = node2; 45 | node1.left = node3; 46 | node1.right = node4; 47 | 48 | TestOffer24 testFindPath = new TestOffer24(); 49 | testFindPath.findPath(root1, 22); 50 | } 51 | } 52 | 53 | 54 | } 55 | 56 | 57 | -------------------------------------------------------------------------------- /TestOffer25.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.Stack; 4 | 5 | public class TestOffer25 { 6 | 7 | public static void findPath(TreeNode root,int sum){ 8 | if (root == null) { 9 | return; 10 | } 11 | Stack stack = new Stack(); 12 | int currentSum = 0; 13 | findPath(root, sum, stack, currentSum); 14 | } 15 | private static void findPath(TreeNode root, int sum, Stack stack, int currentSum) { 16 | // TODO Auto-generated method stub 17 | currentSum += root.data; 18 | stack.push(root.data); 19 | if (root.left == null && root.right == null) { 20 | if (sum ==currentSum) { 21 | System.out.println("找到路径"); 22 | for (int path : stack) { 23 | System.out.print(path + " "); 24 | } 25 | System.out.println(); 26 | } 27 | } 28 | if (root.left != null) { 29 | findPath(root.left, sum, stack, currentSum); 30 | } 31 | if (root.right != null) { 32 | findPath(root.right, sum, stack, currentSum); 33 | } 34 | stack.pop(); 35 | } 36 | public static void main(String[] args) { 37 | { 38 | TreeNode root1 = new TreeNode(10); 39 | TreeNode node1 = new TreeNode(5); 40 | TreeNode node2 = new TreeNode(12); 41 | TreeNode node3 = new TreeNode(4); 42 | TreeNode node4 = new TreeNode(7); 43 | root1.left = node1; 44 | root1.right = node2; 45 | node1.left = node3; 46 | node1.right = node4; 47 | 48 | TestOffer25 testFindPath = new TestOffer25(); 49 | testFindPath.findPath(root1, 22); 50 | } 51 | } 52 | 53 | 54 | } 55 | 56 | 57 | -------------------------------------------------------------------------------- /TestOffer6.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | 4 | public class TestOffer6 { 5 | public static TreeNode reConstructBinaryTree(int[] pre,int[] in){ 6 | TreeNode root = reConstructBinaryTree(pre, 0, pre.length - 1,in,0,in.length-1); 7 | return root; 8 | } 9 | 10 | private static TreeNode reConstructBinaryTree(int[] pre, int startPre, int endPre, int[] in, int startIn, 11 | int endIn) { 12 | 13 | // 开始位置大于结束位置说明已经处理到叶节点了 14 | if (startPre > endPre || startIn > endIn) 15 | return null; 16 | TreeNode root = new TreeNode(pre[startPre]);// 寻找根节点 17 | // 最好把迭代写在循环外 18 | for (int i = startIn; i <= endIn; i++) 19 | if (in[i] == pre[startPre]) { 20 | // 我的理解是从startPre位置越过左孩子及其子节点的偏移量(即i-startIn, 中序定位根节点的长度。 21 | // 因为startIn和i之间全是i这个元素的左孩子及其子节点)再往下一个节点走(即右孩子起始点), 22 | // 如果写成startPre+(i-startIn)+1可能要容易看懂些, 23 | root.left = reConstructBinaryTree(pre, startPre + 1, startPre + (i - startIn), in, startIn, i - 1); 24 | // 递归左子树:前序起始位+1,前序起始位+中序定位根节点的长度,中序起始位不变,中序结束位为定位到的坐标 25 | root.right = reConstructBinaryTree(pre, (i - startIn) + startPre + 1, endPre, in, i + 1, endIn); 26 | // 递归右子树:(前序起始位+左子树长度+1(根节点)),前序结束位不变,右子树为中序根节点+1,中序结束位保持不变 27 | 28 | // 递归思想,每次将左右两颗子树当成新的子树进行处理,中序的左右子树索引很好找,前序的开始结束索引通过计算中序中左右子树的大小来计算,然后递归求解, 29 | // 直到startPre>endPre||startIn>endIn说明子树整理完到。方法每次返回左子树活右子树的根节点 30 | } 31 | System.out.print(root.data); 32 | return root; 33 | } 34 | public static void main(String[] args) { 35 | int pre[]= { 1, 2, 4, 7, 3, 5, 6, 8 }; 36 | int in[] = { 4, 7, 2, 1, 5, 3, 8, 6 }; 37 | 38 | TreeNode root = reConstructBinaryTree(pre, in); 39 | 40 | } 41 | 42 | } 43 | 44 | 45 | -------------------------------------------------------------------------------- /TestOffer60.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.Queue; 6 | import java.util.Stack; 7 | 8 | public class TestOffer60 { 9 | 10 | public static void printNode(TreeNode root) { 11 | //一个变量表示在当前的层中还没有打印的结点数, 12 | int current = 1; 13 | //另一个变量表示下一次结点的数目。 14 | int next = 0; 15 | if (root == null) { 16 | return; 17 | } 18 | Queue queue = new LinkedList(); 19 | queue.add(root); 20 | while(!queue.isEmpty()){ 21 | TreeNode node = queue.poll(); 22 | current--; 23 | System.out.print(node.data + " "); 24 | if (node.left != null) { 25 | queue.add(node.left); 26 | next++; 27 | } 28 | if (node.right != null) { 29 | queue.add(node.right); 30 | next++; 31 | } 32 | 33 | if (current == 0) { 34 | System.out.println(); 35 | current = next; 36 | next = 0; 37 | } 38 | } 39 | 40 | } 41 | public static void main(String[] args) { 42 | TreeNode n1 = new TreeNode(1); 43 | TreeNode n2 = new TreeNode(2); 44 | TreeNode n3 = new TreeNode(3); 45 | TreeNode n4 = new TreeNode(4); 46 | TreeNode n5 = new TreeNode(5); 47 | TreeNode n6 = new TreeNode(6); 48 | TreeNode n7 = new TreeNode(7); 49 | TreeNode n8 = new TreeNode(8); 50 | TreeNode n9 = new TreeNode(9); 51 | n1.left = n2; 52 | n1.right = n3; 53 | n2.left = n4; 54 | n2.right = n5; 55 | n3.left = n6; 56 | n3.right = n7; 57 | n4.left = n8; 58 | n4.right = n9; 59 | printNode(n1); 60 | } 61 | } 62 | 63 | 64 | -------------------------------------------------------------------------------- /TestOffer29_1.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer29_1 { 4 | 5 | public static void main(String[] args) { 6 | int[] array = { 1, 2, 3, 2, 2, 2, 5, 4, 2 }; 7 | TestOffer29_1 p = new TestOffer29_1(); 8 | System.out.println(p.moreThanHalfNum(array)); 9 | } 10 | 11 | private int moreThanHalfNum(int[] array) { 12 | // TODO Auto-generated method stub 13 | if (array.length <= 0) 14 | return 0; 15 | int start = 0; 16 | int length = array.length; 17 | int end = length - 1; 18 | int middle = length >> 1; 19 | int index = Partition(array, start, end); 20 | while(index != middle){ 21 | if (index > middle) { 22 | index = Partition(array, start, index - 1); 23 | }else{ 24 | index = Partition(array, index + 1, end); 25 | } 26 | } 27 | int result = array[middle]; 28 | int times = 0; 29 | for (int i = 0; i < array.length; i++) { 30 | if (array[i] == result) { 31 | times++; 32 | } 33 | } 34 | if (times * 2 < length) { 35 | System.out.println(times); 36 | return 0; 37 | } else { 38 | return result; 39 | } 40 | } 41 | 42 | private int Partition(int[] array, int start, int end) { 43 | // TODO Auto-generated method stub 44 | int flag = (array[start] + array[end]) >> 2; 45 | while(start < end){ 46 | while(array[end] > flag){ 47 | end--; 48 | } 49 | swap(array, start, end); 50 | while(array[start] <= flag){ 51 | start++; 52 | } 53 | swap(array, start, end); 54 | } 55 | return start; 56 | } 57 | public void swap(int[] array, int num1, int num2) { 58 | // TODO Auto-generated method stub 59 | int temp = array[num1]; 60 | array[num1] = array[num2]; 61 | array[num2] = temp; 62 | } 63 | } -------------------------------------------------------------------------------- /TestOffer27.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer27 { 4 | public TreeNode Convert(TreeNode pRootOfTree){ 5 | if (pRootOfTree ==null) { 6 | return null; 7 | }if (pRootOfTree.left == null && pRootOfTree.right == null) { 8 | return pRootOfTree; 9 | } 10 | TreeNode pLastNodeOfList = Convert(pRootOfTree.left); 11 | TreeNode pNode = pLastNodeOfList; 12 | while(pNode != null && pNode.right != null){ 13 | pNode = pNode.right; 14 | } 15 | if (pLastNodeOfList != null) { 16 | pNode.right = pRootOfTree; 17 | pRootOfTree.left = pNode; 18 | } 19 | TreeNode pLastNodeRightOfList = Convert(pRootOfTree.right); 20 | if (pLastNodeRightOfList != null) { 21 | pLastNodeRightOfList.left = pRootOfTree; 22 | pRootOfTree.right = pLastNodeRightOfList; 23 | } 24 | if (pLastNodeOfList != null) { 25 | return pLastNodeOfList; 26 | } 27 | 28 | return pRootOfTree; 29 | } 30 | public static void main(String[] args) { 31 | TreeNode pRootOfTree = new TreeNode(10); 32 | TreeNode node1 = new TreeNode(6); 33 | TreeNode node2 = new TreeNode(14); 34 | TreeNode node3 = new TreeNode(4); 35 | TreeNode node4 = new TreeNode(8); 36 | TreeNode node5 = new TreeNode(12); 37 | TreeNode node6 = new TreeNode(16); 38 | 39 | pRootOfTree.left = node1; 40 | pRootOfTree.right = node2; 41 | node1.left = node3; 42 | node1.right = node4; 43 | node2.left = node5; 44 | node2.right = node6; 45 | 46 | TreeNode node = new TestOffer27().Convert(pRootOfTree); 47 | 48 | while(node != null){ 49 | System.out.print(node.data + " "); 50 | node = node.right; 51 | } 52 | } 53 | 54 | } 55 | 56 | -------------------------------------------------------------------------------- /TestOffer7_1.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.ArrayDeque; 4 | import java.util.Queue; 5 | 6 | import javax.imageio.ImageTypeSpecifier; 7 | 8 | /** 9 | * @author Administrator 10 | * 1.q1和q2在任一时刻至少有一个为空,即如果有元素,所以元素只在同一个队列中。 11 | 2.当有元素需要插入时,将插入的元素插入到空的队列中,并将另一非空队列的元素转移到该队列中, 12 | 于是插入的元素添加到了队列头中。 13 | * 14 | */ 15 | public class TestOffer7_1 { 16 | 17 | Queue queue1 = new ArrayDeque<>(); 18 | Queue queue2 = new ArrayDeque<>(); 19 | public void push(int node) { 20 | if (queue1.isEmpty() && queue2.isEmpty()) { 21 | queue1.add(node); 22 | return; 23 | } 24 | if (queue1.isEmpty()) { 25 | queue2.add(node); 26 | return; 27 | } 28 | if (queue2.isEmpty()) { 29 | queue1.add(node); 30 | return; 31 | } 32 | } 33 | public int pop() { 34 | if (queue1.isEmpty() && queue2.isEmpty()) { 35 | try { 36 | throw new Exception("为空"); 37 | } catch (Exception e) { 38 | // TODO: handle exception 39 | } 40 | } 41 | if (queue1.isEmpty()) { 42 | while(queue2.size()>1){ 43 | queue1.add(queue2.poll()); 44 | } 45 | return queue2.poll(); 46 | } 47 | if (queue2.isEmpty()) { 48 | while(queue1.size()>1){ 49 | queue2.add(queue1.poll()); 50 | } 51 | return queue1.poll(); 52 | } 53 | return (Integer) null; 54 | } 55 | public static void main(String[] args) { 56 | TestOffer7_1 demo08 = new TestOffer7_1(); 57 | demo08.push(1); 58 | demo08.push(2); 59 | demo08.push(3); 60 | demo08.push(4); 61 | System.out.println(demo08.pop()); 62 | System.out.println(demo08.pop()); 63 | demo08.push(5); 64 | System.out.println(demo08.pop()); 65 | System.out.println(demo08.pop()); 66 | System.out.println(demo08.pop()); 67 | } 68 | 69 | 70 | } 71 | 72 | 73 | -------------------------------------------------------------------------------- /TestOffer66.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | 6 | public class TestOffer66 { 7 | 8 | public boolean hasPath(char[] matrix, int rows, int cols, char[] str) { 9 | if(matrix==null || matrix.length==0 || str==null || str.length==0 || matrix.length!=rows*cols || rows<=0 || cols<=0 || rows*cols < str.length) { 10 | return false ; 11 | } 12 | 13 | 14 | boolean[] visited = new boolean[rows*cols] ; 15 | int[] pathLength = {0} ; 16 | 17 | 18 | for(int i=0 ; i<=rows-1 ; i++) { 19 | for(int j=0 ; j<=cols-1 ; j++) { 20 | if(hasPathCore(matrix, rows, cols, str, i, j, visited, pathLength)) { return true ; } 21 | } 22 | } 23 | 24 | 25 | return false ; 26 | } 27 | 28 | 29 | public boolean hasPathCore(char[] matrix, int rows, int cols, char[] str, int row, int col, boolean[] visited, int[] pathLength) { 30 | boolean flag = false ; 31 | 32 | 33 | if(row>=0 && row=0 && col> FindContinuousSequence(int sum) { 7 | ArrayList> qlist = new ArrayList>(); 8 | if (sum < 3) { 9 | return qlist; 10 | } 11 | int small = 1; 12 | int big = 2; 13 | int currentSum = small + big; 14 | int middle = (1 + sum) / 2; 15 | while(small < middle){ 16 | ArrayList sq = new ArrayList<>(); 17 | if (currentSum == sum) { 18 | for (int i = small; i <= big; i++) { 19 | //sq.add(i); 20 | System.out.println(sq.add(i)); 21 | } 22 | } 23 | while(currentSum > sum && small < middle){ 24 | currentSum -= small; 25 | small++; 26 | if (currentSum == sum) { 27 | for (int i = small; i <= big; i++) { 28 | 29 | System.out.println(sq.add(i)); 30 | } 31 | } 32 | } 33 | if (sq.size() > 0) { 34 | qlist.add(sq); 35 | big++; 36 | currentSum += big; 37 | } 38 | } 39 | return qlist; 40 | }*/ 41 | 42 | public void findContinuesSequence(int sum) { 43 | if (sum < 3) 44 | return; 45 | int small = 1; 46 | int big = 2; 47 | int middle = (1 + sum) / 2; 48 | int curSum = small + big; 49 | while (small < middle) { 50 | if (curSum == sum) { 51 | printContineNum(small, big); 52 | } 53 | while (curSum > sum && small < middle) { 54 | curSum -= small; 55 | small++; 56 | if (curSum == sum) 57 | printContineNum(small, big); 58 | } 59 | big++; 60 | curSum += big; 61 | } 62 | } 63 | 64 | private void printContineNum(int small, int big) { 65 | for (int i = small; i <= big; i++) { 66 | System.out.print(i + " "); 67 | } 68 | System.out.println(); 69 | } 70 | public static void main(String[] args) { 71 | TestOffer41_1 p=new TestOffer41_1(); 72 | //int[] data={1,2,4,7,11,15}; 73 | int sum=15; 74 | p.findContinuesSequence(sum); 75 | } 76 | } -------------------------------------------------------------------------------- /TestOffer38.java: -------------------------------------------------------------------------------- 1 | package cn.it.test2; 2 | 3 | public class TestOffer38 { 4 | 5 | /* public static int GetNumberofK(int []array,int k){ 6 | int count=0; 7 | for (int i = 0; i < array.length; i++) { 8 | if (array[i]==k) { 9 | count++; 10 | } 11 | } 12 | return count; 13 | } 14 | public static void main(String[] args) { 15 | 16 | int[] array={1,3,3,3,3,4,4,4,5}; 17 | int k=4; 18 | System.out.println(GetNumberofK(array,k)); 19 | }*/ 20 | 21 | 22 | 23 | public int getNumberOfK(int [] array , int k) { 24 | if(array.length == 0) return 0; 25 | int start = getFirst(array,k,0,array.length-1); 26 | int end = getEnd(array,k,0,array.length-1); 27 | if(start>-1&&end>-1) return (end-start+1); 28 | return 0; 29 | } 30 | public int getFirst(int[] array, int k, int start, int end){ 31 | if(start > end) return -1; 32 | int mid = (start+end)/2; 33 | if(array[mid] == k){ 34 | if(mid>0&&array[mid-1]!=k || mid==0){ 35 | return mid; 36 | }else{ 37 | end = mid-1; 38 | } 39 | }else if(array[mid]>k){ 40 | end = mid-1; 41 | }else{ 42 | start = mid+1; 43 | } 44 | return getFirst(array,k,start,end); 45 | } 46 | public int getEnd(int[] array, int k, int start, int end){ 47 | if(start > end) return -1; 48 | int mid = (start+end)/2; 49 | if(array[mid] == k){ 50 | if(mid