首页 > 编程语言 >算法--2023.1.9

算法--2023.1.9

时间:2023-01-09 15:01:56浏览次数:51  
标签:遍历 temp -- res 算法 int 2023.1 right 节点

1.力扣128-最长连续序列

class Solution {
    public int longestConsecutive(int[] nums) {
        //通过hashset保存去重复后的所有数据
        int n = nums.length;
        int res = 0;
        HashSet<Integer> set = new HashSet<>();
        for(int temp : nums){
            set.add(temp);
        }
        //一次遍历整个hashset,判断是否存在比当前值小一的值,若没有该值就是头节点,然后遍历他的后继、
        //该方法保证了每个节点只遍历一次,即时间复杂度O(n)
        for(int temp : set){
            if(set.contains(temp)&&!set.contains(temp-1)){
                int cur = temp+1;
                while(set.contains(cur)){
                    cur++;
                }
                res = Math.max(res,cur-temp);
            }
        }
        return res;
    }
}

2.力扣121-买卖股票的最佳时机

class Solution {
    public int maxProfit(int[] prices) {
        //注意,本题只有一次买卖机会,所有可以这样做
        int res = 0, min_price = prices[0];
        //res保存遍历到当前节点买卖的最大值,min_price保存遍历到当前节点的最小值
        int n = prices.length;
        for(int i = 1; i < n; i++){
            //在每次遍历中比较当前节点卖出最大的价格与之前最大差价的价格
            res = Math.max(prices[i]-min_price,res);
            min_price = Math.min(min_price,prices[i]);
        }
        return res;
    }
}

3.力扣114-二叉树展开为链表

class Solution {
    public void flatten(TreeNode root) {
        //从根节点开始,迭代遍历
        TreeNode temp = root;
        while(temp!=null){
            //如果该节点存在左子树,则将左子树放到该节点右子树的位置
            if(temp.left!=null){
                TreeNode cur = temp.right;
                temp.right = temp.left;
                temp.left = null;
                //找到根节点的左子树最后一个右节点的位置,将其后继节点设置为根结点之前的右节点
                TreeNode t = temp;
                while(t.right!=null){
                    t = t.right;
                }
                //将根结点的右节点,作为下一个根节点,迭代进行遍历。
                t.right = cur;
            }
            temp = temp.right;
        }

    }
}

4.力扣105--从前序与中序遍历构建二叉树  

class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeNode(preorder, 0, preorder.length-1, inorder, 0, inorder.length-1);
        
    }
    public TreeNode buildTreeNode(int[] preorder, int left, int right, int[] inorder, int l, int r){
        if(left > right){
            return null;
        }
        int i = 0;
        for(;l+i<r;i++){
            if(inorder[l+i] == preorder[left]){
                break;
            }
        }
        TreeNode root = new TreeNode(preorder[left]);
        root.left = buildTreeNode(preorder,left+1,left+i,inorder,l,l+i-1);
        root.right = buildTreeNode(preorder,left+i+1,right,inorder,l+i+1,r);
        return root;
    }
}

  

 

标签:遍历,temp,--,res,算法,int,2023.1,right,节点
From: https://www.cnblogs.com/lyjps/p/17037063.html

相关文章

  • 爬取兰州市房价,看到结果让我很舒适!
    《兰州市落实强省会战略进一步优化营运环境若干措施》于2022年4月1日会议通过,兰州印发1号通知进一步放宽购房政策,包括降低个人购买住房门槛、减轻个人住房消费负担、加大住......
  • 英语学习方法(二)
    01.忘记你在学习英语当你学习英语时,你看着看着就哈哈大笑,你忘记了你在学习英语,此时就是学习英语的最好方法.学习英语就是怎么爽,你就怎么来做.因此背单词,学语......
  • MySQL 同步多张表到目标库
    vi/etc/my.cnf#目标库[mysql]host=172.16.1.10user=rootpassword=xxxxxxxx#源数据库[mysqldump]host=127.0.0.1user=rootpassword=xxxxxxxxssl-mode=DISABLED=====......
  • 金三银四,今年招聘季有多难?
    听了很多人讲如何投递简历,可是今年的金三银四有点难,实话实说,不仅跳槽难,搞不好连自己的本职工作都会丢,然后光荣毕业,涌现出来开猿节流等网络名词,用意项目稳定后,公司利润不再增......
  • Python安装libsvm
    最近在学习机器学习,听的是浙大胡浩基老师的课。胡老师用的是MatLab,我平时用Python比较多,在网上找Python安装libsvm的教程发现比较少,花了几个小时终于成功安装了libsvm,现......
  • CSS - 动画,动画的定义,动画全部属性
    1.动画的定义<style>/*只定义起始与结束*/@keyframes动画名称1{from{}to{}}/*用百分比定义动画*/@keyframes动画......
  • 国内育种服务商
    博奥​​http://www.capitalbiotech.com/territory.html?id=159​​。育种子业务主要做分子标记检测和芯片开发。中玉金标记​​http://www.cgmb.com.cn/​​(网站服务器不行......
  • 基于Flink CDC的现代数据栈 (Modern Data Stack)实现
    ......
  • 网络运维实用小工具
    常用工具一:tcping.exe我们需要测试tcp端口,ping命令虽然好用,但​不能测试端口,因为ping基于ICMP协议,属于IP层协议,所以无法测试传输层的TCP/UDP端口。1、WIN+R按键,输入CMD打开......
  • 支付宝微信扫H5网页展示二维码
    一、场景描述1.支付宝、微信扫一扫功能,扫描网页二维码(格式:https://www.iloveyou.com?token=1314521),进行支付。(聚合二维码功能)2.需要抓包工具代理手机网络,进行手机APP扫......