剑指offer-day2

剑指 Offer 17. 打印从1到最大的n位数
输入数字 n,按顺序打印出从 1 到最大的 n 位十进制数。比如输入 3,则打印出 1、2、3 一直到最大的 3 位数 999。

class Solution {
    public int[] printNumbers(int n) {
        int end = (int)Math.pow(10, n) - 1;
        int[] res = new int[end];
        for(int i = 0; i < end; i++)
            res[i] = i + 1;
        return res;
    }
}

DFS+全排列:

class Solution {
    //生成的列表实际上是n位0-9的全排列
    int[] res;
    char[] num, loop = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    int nine = 0, count = 0, start, n;
    public int[] printNumbers(int n) {
        this.n = n;
        res = new int[(int)Math.pow(10, n) - 1]; //res大小10^n-1
        num = new char[n]; //定义长度为n的字符列表
        start = n - 1;
        dfs(0);
        return res;
    }
    void dfs(int x) {
        if(x == n) {//x为全排列数字的个数,终止条件:已固定完所有位
        //String.valueOf(char[] data) : 将char数组data转换成字符串
        //public String substring(int beginIndex)
            String s = String.valueOf(num).substring(start);
            if(!s.equals("0")) //“0”则直接跳过
                res[count++] = Integer.parseInt(s);//字符串转成int类型的数字
            //当输出数字的所有位都是9时,则下个数字需要向更高位进1,此时左边界start需要减1(即高位多余的0减少一个)
            if(n - start == nine) 
                start--;
            return;
        }
        for(char i : loop) {
            if(i == '9') nine++;//nine记录9的个数
            num[x] = i;// 固定第x位为i
            dfs(x + 1); // 开启固定第x+1位,for循环n个dfs(x + 1),所以第x位哪个数都有可能性
        }
        nine--;//在回溯前恢复nine = nine - 1。
    }
}

剑指 Offer 18. 删除链表的节点
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
返回删除后的链表的头节点。
注意:此题对比原题有改动
双指针:

class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        if(head.val == val) return head.next;//如果头节点就是那个值则返回head.next
        ListNode pre = head, cur = head.next;
        while(cur != null && cur.val != val) {//找到那个值的位置
            pre = cur;
            cur = cur.next;
        }
        if(cur != null) pre.next = cur.next;//链接到那个值位置之后(删除节点)
        return head;//返回链表头部节点head
    }
}

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数在数组的前半部分,所有偶数在数组的后半部分。
首尾双指针:

class Solution {
    //首尾双指针
    //指针i从左向右寻找偶数;指针j从右向左寻找奇数;将偶数nums[i]和奇数nums[j]交换。
    public int[] exchange(int[] nums) {
        int i = 0, j = nums.length-1, temp;
        while(i < j){
            while((nums[i] & 1) == 1 && i < j){
                i++;
            }
            while((nums[j] & 1) == 0 && j > 0){
                j--;
            }
            if(i < j){
                temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        }
        return nums;
    }
}

剑指 Offer 22. 链表中倒数第k个节点
输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。
例如,一个链表有 6 个节点,从头节点开始,它们的值依次是 1、2、3、4、5、6。这个链表的倒数第 3 个节点是值为 4 的节点。
顺序查找:

class Solution {
    public ListNode getKthFromEnd(ListNode head, int k) {
        int n = 0;
        ListNode node = null;
        for (node = head; node != null; node = node.next) {
            n++;
        }
        for (node = head; n > k; n--) {
            node = node.next;
        }
        return node;
    }
}

快慢指针:

class Solution {
    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode fast = head, slow = head;
        for(int i = 0; i < k; i++)
            fast = fast.next;
        while(fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}

剑指 Offer 24. 反转链表
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
反转链表(迭代 / 递归,清晰图解)@Krahets⭐
leetcode 206. 反转链表
迭代:

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode cur = head, pre = null;
        while(cur != null) {
            ListNode tmp = cur.next; // 暂存后继节点cur.next
            cur.next = pre;          // 修改next引用指向
            pre = cur;               // pre暂存cur
            cur = tmp;               // cur访问下一节点
        }
        return pre;
    }
}

递归:

class Solution {
    public ListNode reverseList(ListNode head) {
        return recur(head, null);    // 调用递归并返回,传入null是因为反转链表后,head节点指向null
    }
    private ListNode recur(ListNode cur, ListNode pre) {// 根据输入从前往后遍历
        if (cur == null) return pre; // 终止条件
        ListNode res = recur(cur.next, cur);  // 递归后继节点
        cur.next = pre;              // 修改节点引用指向
        return res;                  // 返回反转链表的头节点
    }
}

剑指 Offer 25. 合并两个排序的链表
输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。
leetcode 21. 合并两个有序链表(略)

剑指 Offer 26. 树的子结构
输入两棵二叉树A和B,判断B是不是A的子结构。(约定空树不是任意一个树的子结构)
B是A的子结构, 即 A中有出现和B相同的结构和节点值。
面试题26. 树的子结构(先序遍历 + 包含判断,清晰图解)@Krahets⭐

class Solution {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        //当树A为空或树B为空时,直接返回false;
        //题目中“约定空树不是任意一个树的子结构”
        //第一个节点依次移动
        return (A != null && B != null) && (recur(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B));
    }
    //recur(TreeNode A, TreeNode B)从一个节点开始依次判断:
    boolean recur(TreeNode A, TreeNode B) {
        //当节点B为空:说明树B已匹配完成(越过叶子节点),因此返回true 
        if(B == null) return true;
        //当节点A为空:说明已经越过树A叶子节点,即匹配失败,返回false;
        //当节点A和B的值不同:说明匹配失败,返回false;
        if(A == null || A.val != B.val) return false;
        return recur(A.left, B.left) && recur(A.right, B.right);
    }
}

剑指 Offer 27. 二叉树的镜像
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
递归:

class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        //递归终止条件:当节点root为空时(即越过叶节点),说明都交换完成了,则返回null
        if(root == null) return null;
        TreeNode tmp = root.left;
        root.left = mirrorTree(root.right);
        root.right = mirrorTree(tmp);
        return root;
    }
}

辅助栈(或队列):

class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        if(root == null) return null;
        Stack<TreeNode> stack = new Stack<>(); 
        stack.add(root);
        while(!stack.isEmpty()) {
            TreeNode node = stack.pop(); //此方法返回出现在堆栈顶部的元素,然后将其删除。
            if(node.left != null) stack.add(node.left);
            if(node.right != null) stack.add(node.right);
            //交换
            TreeNode tmp = node.left;
            node.left = node.right;
            node.right = tmp;
        }
        return root;
    }
}

剑指 Offer 28. 对称的二叉树
请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。
leetcode 101. 对称二叉树(略)
递归:

class Solution {
    public boolean isSymmetric(TreeNode root) {
        return root == null ? true : recur(root.left, root.right);
    }
    boolean recur(TreeNode L, TreeNode R) {
        //L和R均为空
        if(L == null && R == null) return true;
        //L和R一个为空或者值不相同
        if(L == null || R == null || L.val != R.val) return false;
        //继续递归,注意recur谁和谁
        return recur(L.left, R.right) && recur(L.right, R.left);
    }
}

剑指 Offer 29. 顺时针打印矩阵
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。
模拟、设定边界:

class Solution {
    public int[] spiralOrder(int[][] matrix) {
        if(matrix.length == 0) return new int[0];
        //t上边界;r右边界;b下边界;l左边界
        int l = 0, r = matrix[0].length - 1, t = 0, b = matrix.length - 1, num = 0;
        int[] res = new int[(r + 1) * (b + 1)];
        while(true) {
            for(int i = l; i <= r; i++) res[num++] = matrix[t][i]; // left to right.
            t++;
            if(t > b) break;
            for(int i = t; i <= b; i++) res[num++] = matrix[i][r]; // top to bottom.
            r--;
            if(l > r) break;
            for(int i = r; i >= l; i--) res[num++] = matrix[b][i]; // right to left.
            b--;
            if(t > b) break;
            for(int i = b; i >= t; i--) res[num++] = matrix[i][l]; // bottom to top.
            l++;
            if(l > r) break;
        }
        return res;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值