IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 剑指offer 2021/8/6 -> 正文阅读

[数据结构与算法]剑指offer 2021/8/6

52. 两个链表的第一个公共节点

代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //若其中一个链表为空,则没有公共节点,返回null
        if(headA==null || headB==null)
            return null;
        //设置两个指针分别指向两个链表的头节点
        ListNode pA=headA, pB=headB;
        //当两个指针指向同一个节点或者同为空时跳出循环
        while(pA!=pB){
            pA= pA==null ? headB : pA.next;
            pB= pB==null ? headA : pB.next;
        }
        return pA;
    }
}

改进:

双指针法。
假设两个链表相交之前的节点数目分别为a,b,相交之后的节点数目为c。若a==b,显然两个指针指向同一个节点时该节点为所求节点;否则,当第一个指针指向空时,指针重新指向B的首节点,当第二个指针指向空时,指针重新指向A的首节点。若两个链表相交,则他们最终都会经过a+b+c步指向相交的节点。

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //若其中一个链表为空,则没有公共节点,返回null
        if(headA==null || headB==null)
            return null;
        //设置两个指针分别指向两个链表的头节点
        ListNode pA=headA, pB=headB;
        //当两个指针指向同一个节点或者同为空时跳出循环
        while(pA!=pB){
            //pA为空时,指向B的首节点
            pA= pA==null ? headB : pA.next;
            //pB为空时,指向A的首节点
            pB= pB==null ? headA : pB.next;
        }
        return pA;
    }
}

68 - I. 二叉搜索树的最近公共祖先

代码:

递归。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==p||root==q||root==null)    return root;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if(left==null)  return right;
        if(right==null) return left;
        return root;
    }
}

方法二:

迭代。

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        //保证p.val<q.val,这样可以在下面的循环中减少判断
        if(p.val>q.val){
            TreeNode tmp = p;
            p = q;
            q = tmp;
        }
        while(root != null){
            //root.val>q.val代表root的值大于两个节点中较大的节点,则两个节点都在左子树中
            if(root.val > q.val)
                root=root.left;
            //root.val<p.val代表root的值大于两个节点中较小的节点,则两个节点都在右子树中
            else if(root.val<p.val)
                root=root.right;
            //否则,说明root的值介于两个节点之间,或者root的值等于其中一个节点的值,此时root即为最近公共祖先
            else    break;
        }
        return root;
    }
}

方法三:

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        //root.val大于两个节点的val,说明两节点在root的左子树中
        if(root.val>p.val&&root.val>q.val)
            return lowestCommonAncestor(root.left,p,q);
        //root.val大于两个节点的val,说明两节点在root的左子树中
        if(root.val<p.val&&root.val<q.val)
            return lowestCommonAncestor(root.right,p,q);
        //否则,说明两节点在root两侧/其中一个节点的值等于root/root==null
        return root;
    }
}

44. 数字序列中某一位的数字

代码:

解题思路

class Solution {
    public int findNthDigit(int n) {
        //设置位数为1,从1开始计数,该位数共有9个数字
        int digit=1;
        long start=1, count=9;
        //当num<=count时,说明num位于该位数区间
        while(n>count){
            n-=count;
            digit+=1;
            start*=10;
            count = 9*start*digit;
        }
        //计算对应的数字
        long num =start + (n-1)/digit;
        //计算对应该数字的第几位
        return Long.toString(num).charAt((n-1)%digit)-'0';
    }
}

53 - I. 在排序数组中查找数字 I

代码:

class Solution {
    public int search(int[] nums, int target) {
        int left = 0, right = nums.length-1;
        while(left<=right){
            int mid = (left+right)/2;
            if(nums[mid]<target)
                left=mid+1;
            //为了找到最左边的target
            else
                right=mid-1;
        }
        //边界条件:
        //1.nums数组为空
        //2.target大于数组中所有元素
        //3.数组中不存在target(其中包括了target小于数组中所有元素的情况)
        if(nums.length ==0 || left==nums.length || nums[left]!=target)
            return 0;
        int res=0;
        //计算target个数
        while(++right<nums.length && nums[right]==target)
            res++;
        return res;
    }
}

法二:

class Solution {
    public int search(int[] nums, int target) {
        //分别查找左边界left和右边界right,出现的次数即为right-left-1
        int i=0, j=nums.length-1;
        //查找右边界,保存在i中
        while(i<=j){
            int m=(i+j)/2;
            if(nums[m]>target)
                j=m-1;
            else
                i=m+1;
        }
        //若nums中不存在target,则提前返回
        if(j>=0 && nums[j]!=target)
            return 0;
        int right=i;
        i=0;
        j=nums.length-1;
        //查找左边界,保存在j中
        while(i<=j){
            int m=(i+j)/2;
            if(nums[m]<target)
                i=m+1;
            else
                j=m-1;
        }
        int left=j;
        return right-left-1;
    }
}

法三:

class Solution {
    public int search(int[] nums, int target) {
        //分别找到target和target-1的右边界,两者相减,即target出现的次数
        return helper(nums,target)-helper(nums,target-1);
    }
    int helper(int[] nums, int target){
        int i = 0, j=nums.length-1;
        while(i<=j){
            int m = (i+j)/2;
            if(nums[m]>target)
                j=m-1;
            else
                i=m+1;
        }
        return j;
    }
}

31. 栈的压入、弹出序列

代码:

创建一个栈模拟进栈出栈操作。

class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int i=0;
        for(int num:pushed){
            //将pushed数组中的元素放入堆栈中
            stack.push(num);
            //当栈顶元素与popped数组中元素相等时,执行出栈操作,popped指针后移
            while(!stack.isEmpty() && stack.peek()==popped[i]){
                stack.pop();
                i++;
            }
        }
        //栈为空,第二个序列是该栈的弹出顺序
        return stack.isEmpty();
    }
}
  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-08-07 12:20:33  更:2021-08-07 12:20:53 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 23:55:10-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码