• 【面试突击算法第一天】剑指offer + Leetcode Hot100


    2022年6月25日亮剑计划正式启动,直到8月初,每天回顾5道算法题,我选择的题目是剑指offer和leetcodehot100,因为这些题目基本上都是面试常考题,后面在面试之前可以多看看面经,熟悉一下每个公司对应的考过的算法题就行了

    剑指 Offer 03. 数组中重复的数字

    • 题意:找出数组中重复的数字。在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。
    • 示例:
      输入:[2, 3, 1, 0, 2, 5, 3]
      输出:23 
      
      • 1
      • 2
    • 思路:很容易想到的是利用一个hashset来存元素,每次遍历元素时,判断一下是否已经存在于hashset中,如果存在就返回当前元素;第二种方法就是原地交换,我们把元素放到正确的位置上去(0放到0号,1放到1号,…),如果当前位置已经有正确的元素,那么就说明重复了。
    • 代码:
      class Solution {
          public int findRepeatNumber(int[] nums) {
              int i = 0;
              while (i < nums.length) {
                  if (nums[i] == i) {
                      i++;
                      continue;
                  }
                  // 有重复的元素
                  if (nums[nums[i]] == nums[i]) 
                      return nums[i];
                  // 放到正确的位置上
                  swap(nums, nums[i], i);
              }
              return -1;
          }
      
          private void swap(int[] nums, int i, int j) {
              int tmp = nums[i];
              nums[i] = nums[j];
              nums[j] = tmp;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23

    剑指 Offer 04. 二维数组中的查找

    • 题意:在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
    • 示例:
      [
        [1,   4,  7, 11, 15],
        [2,   5,  8, 12, 19],
        [3,   6,  9, 16, 22],
        [10, 13, 14, 17, 24],
        [18, 21, 23, 26, 30]
      ]
      输出:给定 target = 5,返回 true
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
    • 思路:技巧题,从右上角开始查找
    • 代码:
      class Solution {
          public boolean findNumberIn2DArray(int[][] matrix, int target) {
              if (matrix.length == 0)
                  return false;
              int m = matrix.length, n = matrix[0].length;
              int i = 0, j = n - 1;
              while (i < m && j >= 0) {
                  if (matrix[i][j] > target) {// 目标小 列-1
                      j--;
                  } else if (matrix[i][j] < target) {// 目标大 行+1
                      i++;
                  } else {
                      return true;
                  }
              }
              return false;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18

    剑指 Offer 05. 替换空格

    • 题意:请实现一个函数,把字符串 s 中的每个空格替换成"%20"。
    • 示例:
      输入:s = "We are happy."
      输出:"We%20are%20happy."
      
      • 1
      • 2
    • 思路:直接调用API.replace(" ", "%20");也可以采用遍历字符串的方式,只是需要创建一个StringBuidler来存放元素
    • 代码:
      class Solution {
          public String replaceSpace(String s) {
              StringBuilder sb = new StringBuilder();
              for (char ch : s.toCharArray()) {
                  if(ch == ' '){
                      sb.append("%20");
                  }else{
                      sb.append(ch);
                  }
              }
              return sb.toString();
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13

    剑指 Offer 06. 从尾到头打印链表

    • 题意:输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
    • 示例:
      输入:head = [1,3,2]
      输出:[2,3,1]
      
      • 1
      • 2
    • 思路:最开始的想法是先翻转然后再遍历,但是这样就需要遍历两次链表;其实本题是想考察一个数据结构的,那就是,利用它先进后出的特点可以解决这个倒序输出的问题。
    • 代码:
      class Solution {
          public int[] reversePrint(ListNode head) {
              Deque<Integer> stack = new LinkedList<>();
              while (head != null) {
                  stack.push(head.val);
                  head = head.next;
              }
              int[] res = new int[stack.size()];
              for (int i = 0; i < res.length; i++) 
                  res[i] = stack.pop();
              return res;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13

    剑指 Offer 07. 重建二叉树

    • 题意:输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
    • 示例:
      Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
      Output: [3,9,20,null,null,15,7]
      
      • 1
      • 2
    • 思路:很明显本题是想要从 前序和中序 推出 后序遍历的结果,需要使用递归手法,最好我们在草稿纸上演练一遍这个过程
    • 代码:
      /**
       * Definition for a binary tree node.
       * public class TreeNode {
       *     int val;
       *     TreeNode left;
       *     TreeNode right;
       *     TreeNode(int x) { val = x; }
       * }
       */
      class Solution {
          public TreeNode buildTree(int[] preorder, int[] inorder) {
              return dfs(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
          }
          // preLeft/inLeft 前/中序遍历的左边界
          // preRight/inRight 前/中序遍历的右边界
          private TreeNode dfs(int[] preorder, int preLeft, int preRight, int[] inorder, int inLeft, int inRight) {
              if (preLeft > preRight || inLeft > inRight)
                  return null;
              int rootVal = preorder[preLeft];
              int index = -1;
              for (int i = 0; i < inorder.length; i++) {
                  if (inorder[i] == rootVal)
                      index = i;
              }
              // 根节点
              TreeNode root = new TreeNode(rootVal);
              // 左子树
              root.left = dfs(preorder, preLeft + 1, preLeft + (index - inLeft), inorder, inLeft, index - 1);
              // 右子树
              root.right = dfs(preorder, preLeft + (index - inLeft) + 1, preRight, inorder, index + 1, inRight);
              return root;
          }
      }
      
      • 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
  • 相关阅读:
    web前端期末大作业——基于html+css+javascript学生宿舍管理系统网站
    MVC第三波书店图书类型获取和实现下拉框功能
    arm 内核版本编译记录
    2022软件测试技能 Robotframework + SeleniumLibrary + Jenkins web关键字驱动自动化实战教程
    人工智能、机器学习概述
    字体文件的处理 iconfont 的处理
    前端笔记_OAuth规则机制下实现个人站点接入qq三方登录
    Elasticsearch实战(五)---高级搜索 Match/Match_phrase/Term/Must/should 组合使用
    什么是漂亮排序算法:一顿操作很装逼,一看性能二点七
    pygame2 画点线
  • 原文地址:https://blog.csdn.net/qq_42397330/article/details/125456684