Free Talk
      
看了一下实习的面经,发现简单类型的算法题竟然也不少,想刷个十几道练练手。
        
          二叉树的最大深度
      
104. 二叉树的最大深度
        
          递归 + 深度优先搜索
      
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
   | 
 
 
 
 
 
 
 
  class Solution {     public int maxDepth(TreeNode root) {                           return dfs(root, 0);     }
      public int dfs(TreeNode node, int count){         if(node == null) return count;         count ++;         count = Math.max(dfs(node.left, count), dfs(node.right, count));         return count;     } }
 
  | 
 
        
          执行结果
      
        
      
        
          多数元素
      
169. 多数元素
        
          哈希表
      
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | class Solution {     public int majorityElement(int[] nums) {                  
          HashMap<Integer, Integer> map = new HashMap<>();         for(int num: nums){             map.put(num, map.getOrDefault(num, 0) + 1);         }         int m = nums.length / 2;         int res = nums[0];         for(int num: nums){             if(map.get(num) > m){                 res = num;             }         }
          return res;     } }
  | 
 
        
          执行结果
      
        
      
        
          数组排序
      
1 2 3 4 5 6 7
   | class Solution {     public int majorityElement(int[] nums) {                  Arrays.sort(nums);         return nums[nums.length / 2];     } }
  | 
 
        
          执行结果
      
        
      
        
          投票法
      
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   | class Solution {     public int majorityElement(int[] nums) {                  int vote = Integer.MIN_VALUE;         int count = 0;         for(int num: nums){             if(count == 0){                 vote = num;             }             count += (num == vote)? 1 : -1;         }
          return vote;     } }
  | 
 
        
          执行结果
      
        
      
        
          Array.sort()排序原理
      
刚刚写的用数组排序的算法,发现时间复杂度还不错,突然想了解一下,它底层的实现原理。
简单查阅了一下 JDK 1.8 的具体实现,附上源码。
源码真的太多了,想要了解的可以看看这篇文章:https://zhuanlan.zhihu.com/p/37510564
        
          翻转二叉树
      
226. 翻转二叉树
        
          递归
      
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
   | 
 
 
 
 
 
 
 
  class Solution {     public TreeNode invertTree(TreeNode root) {                           if(root == null) return root;
          TreeNode tmp = root.left;         root.left = root.right;         root.right = tmp;
          invertTree(root.left);         invertTree(root.right);
          return root;     } }
 
  | 
 
        
          执行截图
      
        
      
        
          找到所有数组中消失的数字
      
448. 找到所有数组中消失的数字
        
          原地修改
      
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
   | class Solution {     public List<Integer> findDisappearedNumbers(int[] nums) {                  
          List<Integer> res = new ArrayList<>();
          for(int i = 0; i < nums.length ; i ++ ){                          int tmp = Math.abs(nums[i]) - 1;             if(nums[tmp] > 0){                 nums[tmp] *= -1;             }                      }
                   for(int i = 1; i <= nums.length; i ++ ){             if(nums[i - 1] >= 0){                 res.add(i);             }         }
          return res;     } }
  | 
 
        
          执行截图
      
	
        
      
        
          汉明距离
      
461. 汉明距离
        
          二进制运算
      
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
   | class Solution {     public int hammingDistance(int x, int y) {                           
                            int res = x ^ y;         int count = 0;         while(res != 0){             if((res & 1) == 1){                 count ++ ;             }                          res = res >> 1;         }
          return count;     } }
  | 
 
        
          执行结果
      
        
      
        
          二叉树的直径
      
543. 二叉树的直径
        
          转换节点 + 递归
      
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
   | 
 
 
 
 
 
 
 
  class Solution {     int count = 0;     public int diameterOfBinaryTree(TreeNode root) {                                    reverse(root);         return count;     }
      public int reverse(TreeNode root){         if(root == null) return 0;                  int left = reverse(root.left);         int right = reverse(root.right);
          count = Math.max(count, left + right);         return 1 + Math.max(left, right);     } }
 
  | 
 
        
          执行结果
      
        
      
        
          合并二叉树
      
617. 合并二叉树
        
          两个二叉树 + 前序遍历
      
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
   | 
 
 
 
 
 
 
 
  class Solution {     public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {                  if(t1 == null && t2 == null) return null;         return recurse(t1, t2);     }
      public TreeNode recurse(TreeNode t1, TreeNode t2){
                            if(t1 == null || t2 == null){             t1 = (t1 == null) ? t2: t1;             return t1;         }
          t1.val += t2.val;                  t1.left = recurse(t1.left, t2.left);         t1.right = recurse(t1.right, t2.right);                  return t1;     }      }
 
  | 
 
        
          执行结果
      
        
      
        
          完结撒花
      
        
      
简单的题目虽然刷完了,但是我发现自己对于二叉树的递归,了解的远远不够深刻,必须要总结出一个系统的解题方法。