剑指offer题目--面试中的各项能力章
38 数字在排序数组中出现的次数
方法1:二分法:
public class Solution { public int GetNumberOfK(int [] array , int k) { int number = 0; int len = array.length; if(array != null && len >0){ int first = GetFirstK(array, len,k, 0,len-1); int last = GetLastK(array, len,k,0,len-1); if(first > -1 && last > -1){ number = last-first+1; } } return number; } int GetFirstK(int[] array, int length, int k, int start, int end){ if(start > end){ return -1; } int midIndex = (start + end)/2; int midData = array[midIndex]; if(midData == k){ if((midIndex > 0 && array[midIndex -1] != k) || midIndex==0){ return midIndex; }else{ end = midIndex -1; } }else if(midData > k){ end = midIndex - 1; }else{ start = midIndex +1; } return GetFirstK(array, length, k, start, end); } int GetLastK(int[] array, int length, int k, int start, int end){ if(start > end){ return -1; } int midIndex = (start + end)/2; int midData = array[midIndex]; if(midData == k){ if((midIndex <length-1 && array[midIndex + 1] != k) || midIndex==length-1){ return midIndex; }else{ start = midIndex + 1; } }else if(midData < k){ start = midIndex +1; }else{ end = midIndex - 1; } return GetLastK(array, length, k, start, end); } }
39 二叉树的深度
public class Solution { public int TreeDepth(TreeNode root) { if(root == null){ return 0; } int left = TreeDepth(root.left); int right = TreeDepth(root.right); return (left>right)?(left+1):(right+1); } }
****拓展:平衡二叉树
二叉树中任意结点的左右子树的深度相差不超过1就是平衡二叉树。
解法1:
public class Solution { public boolean IsBalanced_Solution(TreeNode root) { if(root == null){ return true; } int left = TreeDepth(root.left); int right = TreeDepth(root.right); int diff = left-right; if(diff > 1 || diff < -1){ return false; } return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right); } public int TreeDepth(TreeNode root) { if(root == null){ return 0; } int left = TreeDepth(root.left); int right = TreeDepth(root.right); return (left>right)?(left+1):(right+1); } }
解法2:每个结点遍历一次 ---未通过所有用例
public class Solution { public boolean IsBalanced_Solution(TreeNode root) { int depth = 0; return IsBalanced_Solution(root, depth); } public boolean IsBalanced_Solution(TreeNode root, int depth) { if(root == null){ depth = 0; return true; } int left=depth; int right=depth; if(IsBalanced_Solution(root.left, left) && IsBalanced_Solution(root.right, right)){ int diff = left - right; if(diff <= 1 && diff >= -1){ depth = 1+(left>right ? left:right); return true; } } return false; } }
40 数组中只出现一次的数字
--未通过
//num1,num2分别为长度为1的数组。传出参数 //将num1[0],num2[0]设置为返回结果 public class Solution { public void FindNumsAppearOnce(int [] array,int num1[] , int num2[]) { if(array.length < 2){ return; } if(array.length == 2){ num1[0] = array[0]; num2[0] = array[1]; return; } int resultExclusiveOR = array[0]; for(int i=0; i<array.length; ++i){ resultExclusiveOR ^= array[i]; } int indexOf1 = FindFirstBitIs1(resultExclusiveOR); for(int j=0; j<array.length;++j){ if(IsBit1(array[j],indexOf1)){ num1[j] ^= array[j]; }else{ num2[j] ^= array[j]; } } } int FindFirstBitIs1(int num){ int indexBit = 0; while((num &1)==0 && indexBit < 32){ num = num >>1; indexBit++ ; } return indexBit; } boolean IsBit1(int num, int indexBit){ num = num >> indexBit; return (num&1) == 1; } }
41 和为s的两个数字 VS 和为s的连续正数序列
42 反转单词顺序 VS 左旋转字符串
43 n个骰子的点数
44 扑克牌的顺子
45 圆圈中最后剩下的数字
46 求1+2+...+n
47 不用加减乘除做加法
48 不能被继承的类