├── .gitignore ├── LICENSE ├── Q10_NumberOf1.java ├── Q11_Power.java ├── Q14_ReOrderArray.java ├── Q15_FindKthToTail.java ├── Q16_ReverseList.java ├── Q17_MergeSortedLists.java ├── Q17_MergeSortedLists_Recursive.java ├── Q18_SubstructureInTree.java ├── Q19_MirrorOfBinaryTree.java ├── Q3_FindMatrix.java ├── Q5_PrintListReversingly.java ├── Q6_ReConstructBinaryTree.java ├── Q7_QueueWithTwoStack.java ├── Q8_MinNumberInRotateArray.java ├── Q9.1_JumpFloor.java ├── Q9.2_JumpFloorII.java ├── Q9.3_RectCover.java ├── Q9_Fibonacci.java ├── README.md └── _config.yml /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Mobile Tools for Java (J2ME) 4 | .mtj.tmp/ 5 | 6 | # Package Files # 7 | *.jar 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 holten 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Q10_NumberOf1.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by holten on 2016-07-04. 3 | * Email: holten.ko@gmail.com 4 | */ 5 | 6 | public class Solution { 7 | public int NumberOf1(int n) { 8 | int count = 0; 9 | while (n != 0) { 10 | n = n & (n - 1); 11 | count++; 12 | } 13 | return count; 14 | } 15 | } -------------------------------------------------------------------------------- /Q11_Power.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by holten on 2016-07-04. 3 | * Email: holten.ko@gmail.com 4 | */ 5 | 6 | public class Solution { 7 | public double Power(double base, int exponent) { 8 | if(base==0&&exponent<=0) return 1; 9 | double result; 10 | if(exponent<0) 11 | result=1/PowerUnsigned(base, -exponent); 12 | else 13 | result=PowerUnsigned(base, exponent); 14 | return result; 15 | } 16 | 17 | public double PowerUnsigned(double base, int exponent) { 18 | if(exponent==0) return 1; 19 | if(exponent==1) return base; 20 | double result=PowerUnsigned(base,exponent/2); 21 | result*=result; 22 | if(exponent%2==1) result*=base; 23 | return result; 24 | } 25 | } -------------------------------------------------------------------------------- /Q14_ReOrderArray.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by holten on 2018-03-14. 3 | * Email: holten.ko@gmail.com 4 | */ 5 | 6 | public class Solution { 7 | public void reOrderArray(int [] array) { 8 | int[] oddArray = new int[array.length]; 9 | int[] evenArray = new int[array.length]; 10 | int odd = 0; 11 | int even = 0; 12 | for (int i : array) { 13 | if (i % 2 == 0) { 14 | evenArray[even++] = i; 15 | } else { 16 | oddArray[odd++] = i; 17 | } 18 | } 19 | for (int i = 0; i < odd; i++) { 20 | array[i] = oddArray[i]; 21 | } 22 | for (int i = 0; i < even; i++) { 23 | array[odd + i] = evenArray[i]; 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Q15_FindKthToTail.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by holten on 2018-03-22. 3 | * Email: holten.ko@foxmail.com 4 | */ 5 | 6 | /* 7 | public class ListNode { 8 | int val; 9 | ListNode next = null; 10 | 11 | ListNode(int val) { 12 | this.val = val; 13 | } 14 | }*/ 15 | public class Solution { 16 | public ListNode FindKthToTail(ListNode head,int k) { 17 | if (head == null) { 18 | return null; 19 | } 20 | ListNode forward = head; 21 | if (k < 1) { 22 | return null; 23 | } 24 | for (int i = 1; i < k; i++) { 25 | if(forward.next==null){ 26 | return null; 27 | } 28 | forward = forward.next; 29 | } 30 | while (forward.next!=null){ 31 | forward=forward.next; 32 | head=head.next; 33 | } 34 | return head; 35 | } 36 | } -------------------------------------------------------------------------------- /Q16_ReverseList.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by holten on 2018-07-11. 3 | * Email: holten.ko@foxmail.com 4 | */ 5 | 6 | /* 7 | public class ListNode { 8 | int val; 9 | ListNode next = null; 10 | 11 | ListNode(int val) { 12 | this.val = val; 13 | } 14 | }*/ 15 | public class Solution { 16 | public ListNode ReverseList(ListNode head) { 17 | if(head==null) return null; 18 | if(head.next==null) return head; 19 | ListNode reverseHead = null; 20 | ListNode now = head; 21 | ListNode prev = null; 22 | while(now!=null){ 23 | ListNode next = now.next; 24 | if(next==null) reverseHead = now; 25 | now.next = prev; 26 | prev = now; 27 | now = next; 28 | } 29 | return reverseHead; 30 | } 31 | } -------------------------------------------------------------------------------- /Q17_MergeSortedLists.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by holten on 2019-02-12. 3 | * Email: holten.ko@foxmail.com 4 | */ 5 | 6 | /* 7 | public class ListNode { 8 | int val; 9 | ListNode next = null; 10 | 11 | ListNode(int val) { 12 | this.val = val; 13 | } 14 | } 15 | */ 16 | public class Solution { 17 | public ListNode Merge(ListNode list1, ListNode list2) { 18 | ListNode newListHead = null; 19 | ListNode newListTail = null; 20 | if (list1 == null) 21 | return list2; 22 | if (list2 == null) 23 | return list1; 24 | while (list1 != null && list2 != null) { 25 | if (list1.val <= list2.val) { 26 | if (newListHead == null) { 27 | newListHead = list1; 28 | newListTail = list1; 29 | } else { 30 | newListTail.next = list1; 31 | newListTail = newListTail.next; 32 | } 33 | list1 = list1.next; 34 | } else { 35 | if (newListHead == null) { 36 | newListHead = list2; 37 | newListTail = list2; 38 | } else { 39 | newListTail.next = list2; 40 | newListTail = newListTail.next; 41 | } 42 | list2 = list2.next; 43 | } 44 | } 45 | if (list1 == null) { 46 | newListTail.next = list2; 47 | } else { 48 | newListTail.next = list1; 49 | } 50 | return newListHead; 51 | } 52 | } -------------------------------------------------------------------------------- /Q17_MergeSortedLists_Recursive.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by holten on 2019-02-12. 3 | * Email: holten.ko@foxmail.com 4 | */ 5 | 6 | /* 7 | public class ListNode { 8 | int val; 9 | ListNode next = null; 10 | 11 | ListNode(int val) { 12 | this.val = val; 13 | } 14 | } 15 | */ 16 | public class Solution { 17 | public ListNode Merge(ListNode list1, ListNode list2) { 18 | if (list1 == null) { 19 | return list2; 20 | } 21 | if (list2 == null) { 22 | return list1; 23 | } 24 | if (list1.val <= list2.val) { 25 | list1.next = Merge(list1.next, list2); 26 | return list1; 27 | } else { 28 | list2.next = Merge(list1, list2.next); 29 | return list2; 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Q18_SubstructureInTree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by holten on 2019-02-14. 3 | * Email: holten.ko@foxmail.com 4 | */ 5 | 6 | /** 7 | public class TreeNode { 8 | int val = 0; 9 | TreeNode left = null; 10 | TreeNode right = null; 11 | 12 | public TreeNode(int val) { 13 | this.val = val; 14 | } 15 | } 16 | */ 17 | public class Solution{ 18 | public boolean HasSubtree(TreeNode root1,TreeNode root2) { 19 | if(root2==null) return false; 20 | if(root1==null && root2!=null) return false; 21 | boolean flag = false; 22 | if(root1.val==root2.val){ 23 | flag = isSubTree(root1,root2); 24 | } 25 | if(!flag){ 26 | flag = HasSubtree(root1.left, root2); 27 | if(!flag){ 28 | flag = HasSubtree(root1.right, root2); 29 | } 30 | } 31 | return flag; 32 | } 33 | 34 | private boolean isSubTree(TreeNode root1, TreeNode root2) { 35 | if(root2==null) return true; 36 | if(root1==null && root2!=null) return false; 37 | if(root1.val==root2.val){ 38 | return isSubTree(root1.left, root2.left) && isSubTree(root1.right, root2.right); 39 | }else{ 40 | return false; 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /Q19_MirrorOfBinaryTree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by holten on 2019-02-14. 3 | * Email: holten.ko@foxmail.com 4 | */ 5 | 6 | /** 7 | public class TreeNode { 8 | int val = 0; 9 | TreeNode left = null; 10 | TreeNode right = null; 11 | 12 | public TreeNode(int val) { 13 | this.val = val; 14 | 15 | } 16 | 17 | } 18 | */ 19 | public class Solution { 20 | public void Mirror(TreeNode root) { 21 | TreeNode temp; 22 | if(root!=null){ 23 | temp=root.left; 24 | root.left=root.right; 25 | root.right=temp; 26 | if(root.left!=null) Mirror(root.left); 27 | if(root.right!=null) Mirror(root.right); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Q3_FindMatrix.java: -------------------------------------------------------------------------------- 1 | public class Q3_FindMatrix{ 2 | public static boolean Find(int[][] matrix,int number){ 3 | boolean found=false; 4 | int rows=matrix.length; 5 | int columns=matrix[0].length; 6 | if(matrix!=null && rows>0 && columns>0){ 7 | int row=0; 8 | int column=columns-1; 9 | while(row=0){ 10 | if(matrix[row][column]==number){ 11 | found=true; 12 | break; 13 | }else if(matrix[row][column]>number){ 14 | column--; 15 | }else{ 16 | row++; 17 | } 18 | } 19 | } 20 | return found; 21 | } 22 | 23 | public static void main(String[] args){ 24 | int[][] matrix={{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}}; 25 | int number=20; 26 | System.out.println("Is there "+number+" in the Matrix?"); 27 | System.out.println(Find(matrix,number)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Q5_PrintListReversingly.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | 3 | public class Q5_PrintListReversingly{ 4 | static class Node{ 5 | Node next; 6 | int data; 7 | } 8 | public static void printListReversingly_Stack(Node head){ 9 | Stack nodeStack=new Stack(); 10 | Node node=head; 11 | while(node!=null){ 12 | nodeStack.push(node); 13 | node=node.next; 14 | } 15 | while(!nodeStack.isEmpty()){ 16 | System.out.println(nodeStack.pop().data); 17 | } 18 | } 19 | 20 | public static void printListReversingly_Re(Node head){ 21 | Node node=head; 22 | if(head!=null){ 23 | if(node.next!=null){ 24 | printListReversingly_Re(node.next); 25 | } 26 | System.out.println(node.data); 27 | } 28 | } 29 | 30 | public static void main(String[] args){ 31 | Node node1=new Node(); 32 | Node node2=new Node(); 33 | Node node3=new Node(); 34 | node1.data=1; 35 | node1.next=node2; 36 | node2.data=2; 37 | node2.next=node3; 38 | node3.data=3; 39 | printListReversingly_Re(node1); 40 | printListReversingly_Stack(node1); 41 | Node node0=new Node(); 42 | node0.data=0; 43 | printListReversingly_Re(node0); 44 | printListReversingly_Stack(node0); 45 | Node nodenull=null; 46 | printListReversingly_Stack(nodenull); 47 | printListReversingly_Re(nodenull); 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /Q6_ReConstructBinaryTree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public TreeNode reConstructBinaryTree(int [] pre,int [] in) { 12 | TreeNode root=reConstructBinaryTree(pre,0,pre.length-1,in,0,in.length-1); 13 | return root; 14 | } 15 | 16 | private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) { 17 | if(startPre>endPre||startIn>endIn) 18 | return null; 19 | TreeNode root=new TreeNode(pre[startPre]); 20 | 21 | for(int i=startIn;i<=endIn;i++) 22 | if(in[i]==pre[startPre]){ 23 | root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1); 24 | root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn); 25 | } 26 | return root; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Q7_QueueWithTwoStack.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/holtenko/CodingInterviews/3f3a14935df4a16eb70fb2971f269a3491f45b74/Q7_QueueWithTwoStack.java -------------------------------------------------------------------------------- /Q8_MinNumberInRotateArray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int minNumberInRotateArray(int [] array) { 3 | if(array.length==0||array==null) return 0; 4 | if(array.length==1) return array[0]; 5 | int min=array[0]; 6 | for(int i=0;i Solutions to the book《Coding Interviews》with Java. 4 | 5 | >《剑指offer—名企面试官精讲典型编程题》题目的Java实现。 6 | 7 | | No | Title | Question | Solution | 8 | |:--------|:--------:|--------|:--------:| 9 | |01|赋值运算符函数||:no_entry:| 10 | |02|实现Singleton模式||:no_entry:| 11 | |03|FindInPartiallySortedMatrix
二维数组中的查找|在一个二维数组中,每一行按照从左到右递增的顺序排序,每一列按照从上到下递增的顺序排序。请完成一个函数,输入上述二维数组和一个整数,判断数组中是否含有该整数。|[Q3_FindMatrix.java](Q3_FindMatrix.java)| 12 | |05|PrintListReversingly
从尾到头打印链表|输入一个链表的头节点,从尾到头反过来打印每个节点的值。链表节点定义见[Q5_PrintListReversingly.java](Q5_PrintListReversingly.java)|[Q5_PrintListReversingly.java](Q5_PrintListReversingly.java)| 13 | |06|ReConstructBinaryTree
重建二叉树|输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列`{1,2,4,7,3,5,6,8}`和中序遍历序列`{4,7,2,1,5,3,8,6}`,则重建二叉树并返回。|[Q6_ReConstructBinaryTree.java](Q6_ReConstructBinaryTree.java)| 14 | |07|QueueWithTwoStack
用两个栈实现队列|用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。|[Q7_QueueWithTwoStack.java](Q7_QueueWithTwoStack.java)| 15 | |08|MinNumberInRotateArray
旋转数组的最小数字|把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增序列(可能存在重复值)的一个旋转,输出旋转数组的最小元素。例如数组`{3,4,5,1,2}`为`{1,2,3,4,5}`的一个旋转,该数组的最小值为1。|[Q8_MinNumberInRotateArray.java](Q8_MinNumberInRotateArray.java)| 16 | |09|Fibonacci
斐波那契数列|大家都知道斐波那契数列,要求输入一个整数n,请你输出斐波那契数列的第n项。|[Q9_Fibonacci.java](Q9_Fibonacci.java)| 17 | |09.1|JumpFloor
跳台阶|一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个n级的台阶总共有多少种跳法。|[Q9.1_JumpFloor.java](Q9.1_JumpFloor.java)| 18 | |09.2|JumpFloorII
变态跳台阶|一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。|[Q9.2_JumpFloorII.java](Q9.2_JumpFloorII.java)| 19 | |09.3|RectCover
矩形覆盖|我们可以用`2*1`的小矩形横着或者竖着去覆盖更大的矩形。请问用n个`2*1`的小矩形无重叠地覆盖一个`2*n`的大矩形,总共有多少种方法?|[Q9.3_RectCover.java](Q9.3_RectCover.java)| 20 | |10|NumberOf1
二进制中1的个数|输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。|[Q10_NumberOf1.java](Q10_NumberOf1.java)| 21 | |11|Power
数值的整数次方|给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。|[Q11_Power.java](Q11_Power.java)| 22 | |12|打印1到最大的n位数||:flags:| 23 | |13|在O(1)时间删除链表结点||:flags:| 24 | |14|调整数组顺序使奇数位于偶数前面|输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。|[Q14_ReOrderArray.java](Q14_ReOrderArray.java)| 25 | |15|链表中倒数第k个结点|输入一个链表,输出该链表中倒数第k个结点。|[Q15_FindKthToTail.java](Q15_FindKthToTail.java)| 26 | |16|反转链表|输入一个链表,反转链表后,输出新链表的表头。|[Q16_ReverseList.java](Q16_ReverseList.java)| 27 | |17|合并两个排序的链表|输入两个递增排序的链表,合并这两个链表并使新链表的结点仍然是按照递增排序的。|[Q17_MergeSortedLists.java](Q17_MergeSortedLists.java)
[Q17_MergeSortedLists_Recursive.java](Q17_MergeSortedLists_Recursive.java)| 28 | |18|树的子结构|输入两棵二叉树A,B,判断B是不是A的子结构。(空树不是任意一个树的子结构)|[Q18_SubstructureInTree.java](Q18_SubstructureInTree.java)| 29 | |19|二叉树的镜像|请完成一个函数,输入一个二叉树,该函数输出它的镜像。|[Q19_MirrorOfBinaryTree.java](Q19_MirrorOfBinaryTree.java)| 30 | |20|顺时针打印矩阵||:soon:| 31 | |21|包含min函数的栈||:soon:| 32 | |22|栈的压入、弹出序列||:soon:| 33 | |23|从上往下打印二叉树||:soon:| 34 | |24|二叉搜索树的后序遍历序列||:soon:| 35 | |25|二叉树中和为某一值的路径||:soon:| 36 | |26|复杂链表的复制||:soon:| 37 | |27|二叉搜索树与双向链表||:soon:| 38 | |28|字符串的排列||:soon:| 39 | |29|数组中出现次数超过一半的数字||:soon:| 40 | |30|最小的k个数||:soon:| 41 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman --------------------------------------------------------------------------------