2023/10/10 LeetCode

guardyou3
2023-10-10 / 45 评论 / 186 阅读 / 正在检测是否收录...

LeetCode p11 盛最多水的容器

描述: 给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i])。
找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
返回容器可以储存的最大水量。
lnkfdzb3.png

思路 :既然是要求当前可以存储的最大水量,那么按照常规思路,只需要定义两个指针指向首尾作为容器的边界,我们每次将最小的值的指针向另一指针移动,记录每次移动的容器中的大小值,即可找到最大的容器盛水量。

代码

 /**
     * P11盛最多水的容器
     * @param height
     * @return
     */
    public int maxArea(int[] height) {
        int result = 0;
        int heightMax = 0;
        for (int i=0,j=height.length-1;i<j;)
        {
            heightMax = Math.min(height[i], height[j]); //取最小的高度
            int curMax = (j-i)*heightMax; //求当前面积
            System.out.println("i="+i+",j="+j+",cur="+curMax);
            result = Math.max(result,curMax);
            if (height[i]>height[j]) {
                j--;
            }
            else {
                i++;
            }
        }
        return result;
    }

LeetCode P160 相交链表
描述 :给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null。

lnkga10g.png

思路: 按照题目所需要找到两个链表的交点,那么我们思考一下如果存在交点,那么这个交点后面的节点必是相同节点,既然如此,我们直接将短的一个链表与长链表进行一个右对齐,然后遍历连个链表,只要存在节点相等,即可说明该节点是两个节点的交点,如果链表遍历完成都未找到相等节点,即说明不存在交点,直接返回null值即可。

代码:

/**
     * P160 相交链表
     * @param headA
     * @param headB
     * @return
     */
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode a = headA;
        ListNode b = headB;
        int aLength = 0;
        int bLength = 0;
        // 判断哪个链表比较长
        while (a!=null) {
            aLength++;
            a = a.next;
        }
        while (b!=null){
            bLength++;
            b = b.next;
        }
        ListNode longList;
        ListNode shortList;
        int sub;
        if (aLength>bLength){
            longList = headA;
            shortList = headB;
            sub = aLength-bLength;
        }
        else {
            longList = headB;
            shortList = headA;
            sub = bLength - aLength;
        }
        //移动长链表与短链表进行对齐
        while (sub>0) {
            longList = longList.next;
            sub--;
        }
        while (longList!=null) {
            if (longList==shortList)
                return longList;
            longList = longList.next;
            shortList = shortList.next;
        }
        return null;
    }
0

评论 (45)

取消
  1. 头像
    1
    Windows 10 · Google Chrome

    画图

    回复
  2. 头像
    1
    Windows 10 · Google Chrome

    555

    回复
  3. 头像
    1
    Windows 10 · Google Chrome

    555

    回复
  4. 头像
    1
    Windows 10 · Google Chrome

    表情

    回复
  5. 头像
    1
    Windows 10 · Google Chrome

    表情

    回复
  6. 头像
    1
    Windows 10 · Google Chrome

    表情

    回复
    1. 头像
      1
      Windows 10 · Google Chrome
      @ 1

      表情表情

      回复
  7. 头像
    1
    Windows 10 · Google Chrome

    表情

    回复
  8. 头像
    1
    Windows 10 · Google Chrome

    表情

    回复
  9. 头像
    1
    Windows 10 · Google Chrome

    表情

    回复
  10. 头像
    1
    Windows 10 · Google Chrome

    表情

    回复
  11. 头像
    1
    Windows 10 · Google Chrome

    表情

    回复
  12. 头像
    1
    Windows 10 · Google Chrome

    表情

    回复
  13. 头像
    1
    Windows 10 · Google Chrome

    表情

    回复
  14. 头像
    1
    Windows 10 · Google Chrome

    表情

    回复
  15. 头像
    1
    Windows 10 · Google Chrome

    表情

    回复
  16. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  17. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  18. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  19. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  20. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  21. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  22. 头像
    1
    Windows 10 · Google Chrome

    表情表情

    回复
  23. 头像
    1
    Windows 10 · Google Chrome

    555

    回复
  24. 头像
    1
    Windows 10 · Google Chrome

    555

    回复
  25. 头像
    lvnfacscce
    Windows 10 · Google Chrome

    博主真是太厉害了!!!

    回复
  26. 头像
    ahxaabccok
    Windows 10 · Google Chrome

    叼茂SEO.bfbikes.com

    回复
  27. 头像
    exqtlxomkf
    Windows 10 · Google Chrome

    看的我热血沸腾啊https://www.237fa.com/

    回复
  28. 头像
    dbwgxaqgzh
    Windows 10 · Google Chrome

    想想你的文章写的特别好www.jiwenlaw.com

    回复
  29. 头像
    mdrshprzqq
    Windows 10 · Google Chrome

    文章的确不错啊https://www.cscnn.com/

    回复
  30. 头像
    jlijddwdoq
    Windows 10 · Google Chrome

    《她的家,她的城(经典重制版 )》连续剧高清在线免费观看:https://www.jgz518.com/xingkong/160196.html

    回复
  31. 头像
    lvhmwigmkd
    Windows 10 · Google Chrome

    《她的家,她的城(经典重制版 )》连续剧高清在线免费观看:https://www.jgz518.com/xingkong/160196.html

    回复
  32. 头像
    loeekeqdtd
    Windows 10 · Google Chrome

    《冰血暴第五季》欧美剧高清在线免费观看:https://www.jgz518.com/xingkong/15304.html

    回复
  33. 头像
    cqzlnzxygs
    Windows 10 · Google Chrome

    《胭脂》国产剧高清在线免费观看:https://www.jgz518.com/xingkong/35790.html

    回复
  34. 头像
    rizdmtdgvv
    Windows 10 · Google Chrome

    《大地恩情之古都惊雷粤语》韩国剧高清在线免费观看:https://www.jgz518.com/xingkong/114195.html

    回复
  35. 头像
    cutzewpvpr
    Windows 10 · Google Chrome

    《标价我》剧情片高清在线免费观看:https://www.jgz518.com/xingkong/78902.html

    回复
  36. 头像
    bdysrlngpj
    Windows 10 · Google Chrome

    《密逃星球(高清版)》大陆综艺高清在线免费观看:https://www.jgz518.com/xingkong/165939.html

    回复
  37. 头像
    iqmxwvtfih
    Windows 10 · Google Chrome

    《制胜一击》动作片高清在线免费观看:https://www.jgz518.com/xingkong/9729.html

    回复
  38. 头像
    dwgfzazkwj
    Windows 10 · Google Chrome

    哈哈哈,写的太好了https://www.lawjida.com/

    回复
  39. 头像
    jqvvjymnkb
    Windows 10 · Google Chrome

    作者的观点新颖且实用,让人在阅读中获得了新的思考和灵感。

    回复
  40. 头像
    rrlqwudkbo
    Windows 10 · Google Chrome

    建议增加个人经历分享,增强情感穿透力。

    回复
  41. 头像
    tyemtlbswq
    Windows 10 · Google Chrome

    对生命本质的追问充满哲学思辨。

    回复
  42. 头像
    dxcdaylqlg
    Windows 10 · Google Chrome

    既有宏观视野,又兼顾微观细节。

    回复
  43. 头像
    ongxmbizcf
    Windows 10 · Google Chrome

    作者以简洁明了的语言,传达了深刻的思想和情感。

    回复
  44. 头像
    drusherrfs
    Windows 10 · Google Chrome

    文章的叙述风格独特,用词精准,让人回味无穷。

    回复