• 剑指Offer题解面试题解最终章


    剑指Offer最终章

    求1+2+…+n

    题链
    题解:按照题目的规则其实没啥能用了哈哈.但我们可以用一些等价的方式,比如可以用递归去代替循环,而循环又必须有边界条件,所以可以利用与运算的就近原则去代替判断.

       public int sumNums(int n) {
            boolean flag = n > 0 && (n += sumNums(n-1)) > 0;
            return n;
        }
    
    • 1
    • 2
    • 3
    • 4

    不用加减乘除作加法

    题链
    题解:利用位运算实现加法.其中与运算负责进位加法,而异或运算负责不进位加法,两者结合可以实现加法.

       public int add(int a, int b) {
            
            while(b != 0){
                int aa = a ^ b;
                int bb = a & b;
                a = aa;
                b = bb << 1;
            }
            return a;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    构建乘积数组

    题链
    题解:这道题的思路就是构造两个乘积数组,lArr,rArr.lArr[i]表示第i个之前的所有数的乘积(不包括i),rArr[i]表示第i个之后所有数的乘积(不包括i).
    所以B[i] = lArr[i]*rArr[i]

    public int[] constructArr(int[] a) {
            int n = a.length;
            int[] leftM = new int[n];
            int[] rightM = new int[n];
            int[] ans = new int[n];
            if(n == 0){
                return ans;
            }
            leftM[0] = 1;
            rightM[n-1] = 1;
            for(int i = 1; i < n; i++){
                leftM[i] = a[i-1]*leftM[i-1];
            }
            for(int i = n-2; i >= 0; i--){
                rightM[i] = rightM[i+1] * a[i+1];
            }
    
            for(int i = 0; i < n; i++){
                ans[i] = leftM[i] * rightM[i];
            }
            return ans;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    把字符串转换成整数

    题链
    题解:字符串模拟.首先将字符串开头的空格去掉.然后判断第一个字符是否合法:为+,-或0~9,如果为符号位则idx+1.
    将结果值ans设为long型.
    之后从idx开始遍历字符串,每次遍历都要判断字符是否在0~9内.在的话ans*10+该数.
    最后判断一下符号位是否是负号,如果是则取相反数.然后判断ans的值是否在int范围之内,如果没有按照题目规则返回结果.

     public int strToInt(String str) {
              int n = str.length();
            char start = ' ';
            int idx = 0;
            for(int i = 0; i < n; i++){
                if(str.charAt(i) == ' '){
                    continue;
                }
                idx = i;
                start = str.charAt(i);
                if(!((start >= '0' && start <= '9') || start == '-' || start == '+')){
                    return 0;
                }
                break;
            }
            boolean flag = false;
            if(start == '-' || start == '+'){
                idx++;
                if(start == '-'){
                    flag = true;
                }
            }
            long ans = 0;
            for(;idx < n;idx++){
                char ch = str.charAt(idx);
                if(!(ch >= '0' && ch <= '9')){
                    break;
                }
                ans = ans * 10 + (int)(ch-48);
                 if(ans > Integer.MAX_VALUE){
                    if(flag){
                        return Integer.MIN_VALUE;
                    }else{
                        return Integer.MAX_VALUE;
                    }
                }
            }
            if(flag){
                ans = ans * -1;
            }
            if(ans > Integer.MAX_VALUE){
                return Integer.MAX_VALUE;
            }else if(ans < Integer.MIN_VALUE){
                return Integer.MIN_VALUE;
            }else{
                return (int)ans;
            }
        }
    
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    二叉搜索树的最近公共祖先

    题链
    题解:二叉搜索树的性质是根节点的左树上的节点的值小于根节点的值,右树上的节点的值大于根节点的值.根据这个特点我们得知两个节点的最近公共祖先的值一定介于两者之间.利用递归求得结果.

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            if(root == null){
                return null;
            }
            if(root.val > p.val && root.val < q.val || root.val > q.val && root.val < p.val){
                return root;
            }else if(root.val == p.val || root.val == q.val){
                return root;
            }else if(root.val > p.val && root.val > q.val){
                return lowestCommonAncestor(root.left,p,q);
            }else{
                return lowestCommonAncestor(root.right,p,q);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    二叉树的最近公共祖先

    题链
    题解:尽管二叉树没有二叉搜索树那样特殊的性质.但二叉树也可以依靠递归去得到最近公共祖先.首先遍历根节点的左侧和根节点的右侧.如果遍历过程中发现节点是p或q直接返回,如果左右两侧都不为空,说明root即为最近公共祖先,如果一侧为空,一侧不为空说明两个节点在不为空的一侧.

       public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            if(root == null){
                return null;
            }
            TreeNode l = lowestCommonAncestor(root.left,p,q);
            TreeNode r = lowestCommonAncestor(root.right,p,q);
            if(root.val == p.val || root.val == q.val){
                return root;
            }
            if(l != null && r != null){
                return root;
            }else if(l == null){
                return r;
            }else{
                return l;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    机器人的运动范围

    题链
    题解:bfs遍历得到所有在范围内的格子.

    static int[][] dir = {{0,1},{1,0},{0,-1},{-1,0}};
        boolean[][] isV;
        static class Pair{
            int x;
            int y;
            int step;
        }
        public int movingCount(int m, int n, int k) {
            int ans = 0;
            if(k == 0){
                return 1;
            }
            isV = new boolean[m][n];
            Queue<Pair> q = new LinkedList<>();
            Pair pair = new Pair();
            pair.x = 0;
            pair.y = 0;
            pair.step = 0;
            q.offer(pair);
            while(!q.isEmpty()){
                Pair cur = q.poll();
                if(cur.step <= k){
                    ans++;
                }else{
                    continue;
                }
                isV[cur.x][cur.y] = true;
                for(int i = 0; i < 4; i++){
                    int nx = cur.x + dir[i][0];
                    int ny = cur.y + dir[i][1];
                    if(nx < 0 || nx >= m || ny < 0 || ny >= n || isV[nx][ny]){
                        continue;
                    }
                    isV[nx][ny] = true;
                    Pair tmp = new Pair();
                    tmp.x = nx;
                    tmp.y = ny;
                    tmp.step = getStep(nx,ny);
                    q.offer(tmp);
                }
            }
            return ans;
        }
        public int getStep(int x,int y){
            int ans = 0;
            while(x != 0){
                ans += x % 10;
                x /= 10;
            }
            while(y != 0){
                ans += y %10;
                y /= 10;
            }
            return ans;
        }
    
    • 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
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
  • 相关阅读:
    前端面试题集锦(2)
    docker 开发环境搭建
    php mysql摄影网_图片分享网站
    【AutoSAR CAN】03 - 如何使用Davinci Configurator Pro工具配置CAN的波特率
    微信小程序 ---- 慕尚花坊 商品管理
    RMAN备份数据库_使用RMAN做拆分镜像(split mirror)备份
    81-RabbitMQ详解
    Linux学习之epoll代码初学
    007 个人博客系统
    【网站项目】校园商铺系统小程序
  • 原文地址:https://blog.csdn.net/weixin_52477733/article/details/126394447