力扣爆刷第108天之CodeTop100五连刷26-30

力扣爆刷第108天之CodeTop100五连刷26-30

文章目录

      • 力扣爆刷第108天之CodeTop100五连刷26-30
      • [一、15. 字符串相加](#一、15. 字符串相加)
      • [二、300. 最长递增子序列](#二、300. 最长递增子序列)
      • [三、42. 接雨水](#三、42. 接雨水)
      • [四、43. 重排链表](#四、43. 重排链表)
      • [五、142. 环形链表 II](#五、142. 环形链表 II)

一、15. 字符串相加

题目链接:https://leetcode.cn/problems/add-strings/description/

思路:留一个位置记录余数,每次取一个数下来进行计算,然后反向拼接,然后翻转即可。

java 复制代码
class Solution {
    public String addStrings(String num1, String num2) {
        StringBuilder res = new StringBuilder();
        int i = num1.length()-1, j = num2.length()-1, up = 0;
        while(i >= 0 || j >= 0 || up != 0) {
            int a = i >= 0 ? num1.charAt(i)-'0' : 0;
            int b = j >= 0 ? num2.charAt(j)-'0' : 0;
            int sum = a + b + up;
            res.append(sum % 10);
            up = sum / 10;
            i--;
            j--;
        }
        res.reverse();
        return res.toString();
    }
}

二、300. 最长递增子序列

题目链接:https://leetcode.cn/problems/longest-increasing-subsequence/

思路:求最长递增子序列的长度,是非连续的,一层for是解决不了的,定义dpi表示区间0, i以numsi为结尾的最长递增子序列的长度,求dpi就要遍历0,i,每一个位置利用定义进行推导,numsj<numsi,dpj是以numsj为结尾的区间的最大长度,那么dpi=dpj+1。

java 复制代码
class Solution {
    public int lengthOfLIS(int[] nums) {
        int[] dp = new int[nums.length];
        Arrays.fill(dp, 1);
        int max = 1;
        for(int i = 1; i < nums.length; i++) {
            for(int j = 0; j < i; j++) {
                if(nums[j] < nums[i]) {
                    dp[i] = Math.max(dp[i], dp[j]+1);
                }
            }
            max = Math.max(dp[i], max);
        }
        return max;
    }
}

三、42. 接雨水

题目链接:https://leetcode.cn/problems/trapping-rain-water/description/

思路:利用单调栈,构造凹槽即可,栈内递减,当前元素大于栈顶,栈顶出栈,出来的这个为凹槽底部,然后利用当前元素为右边界,当前栈顶为左边界,结合出栈的凹槽底部进行雨水容量计算。

java 复制代码
class Solution {
   public int trap(int[] height) {
       Deque<Integer> stack = new LinkedList<>();
       int sum = 0;
       for(int i = 0; i < height.length; i++) {
           while(!stack.isEmpty() && height[i] > height[stack.peek()]) {
               int mid = height[stack.pop()];
               if(!stack.isEmpty()) {
                   int w = i - stack.peek() - 1;
                   int h = Math.min(height[stack.peek()], height[i]) - mid;
                   sum += w*h;
               }
           }
           stack.push(i);
       }
       return sum;
    }
}

四、43. 重排链表

题目链接:https://leetcode.cn/problems/reorder-list/description/

思路:这次写的有点丑陋,下次可以拆分成三个函数来写。具体步骤为三步:

第一步:利用快慢指针寻找链表中点。

第二步:翻转后半部分链表。

第三步:前半部分和后边部分合并即可。

java 复制代码
class Solution {
    public void reorderList(ListNode head) {
        ListNode slow = head, fast = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode p = slow.next, pre = p;
        slow.next = null;
        while(p != null) {
            pre = p.next;
            p.next = slow.next;
            slow.next = p;
            p = pre;
        }
        fast = slow.next;
        slow.next = null;
        slow = head;
        while(fast != null) {
            ListNode spre = slow.next;
            ListNode fpre = fast.next;
            slow.next = fast;
            slow = spre;

            fast.next = spre;
            fast = fpre;
        }
    }
}

五、142. 环形链表 II

题目链接:https://leetcode.cn/problems/linked-list-cycle-ii/description/

思路:先快慢指针判断是否有环,相遇即有环,然后一个指针从头开始,一个指针从相遇处开始,同步走,相遇便是环的入口。

java 复制代码
public class Solution {
   public ListNode detectCycle(ListNode head) {
        ListNode slow = head, fast = head;
        while(fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast) {
                slow = head;
                while(slow != fast) {
                    slow = slow.next;
                    fast = fast.next;
                }
                return slow;
            }
        }
        return null;
    }
}
相关推荐
find1star2 小时前
LeetCode 141:环形链表
java·算法·leetcode·链表
迅翼SwiftWing3 小时前
编队系列(总)|固定翼编队如何实现稳定飞行?通信架构、协同算法与工程约束深度解析
算法·架构
LB21123 小时前
力扣160 21 86
算法·leetcode·职场和发展
小智老师PMP4 小时前
2026深度解析|PMP第八版与NPDP核心侧重点本质区别(管理类证书怎么选)
开发语言·分布式·算法·职场和发展·产品经理
HugoStudio_SWAN4 小时前
洛谷 B4450 / B3867 / B3923 智慧购物、储蓄与做题——从程序到生活
c++·学习·程序人生·算法·生活
a187927218314 小时前
【算法】动态规划第二篇:双序列 DP 三课——继承、计步与断链
算法·leetcode·动态规划·dp·回溯·暴力·算法讲解
江畔柳前堤5 小时前
前台·中台·后台:2026年AI原生时代的架构全景图
开发语言·人工智能·算法·机器学习·架构·scala·ai-native
职场的momo5 小时前
字节生活服务海量内推,挑战亿级订单与AI交易中台
人工智能·程序人生·面试·职场和发展·跳槽·生活·业界资讯
文心快码BaiduComate5 小时前
2026 OPC x AI Coding 趋势案例交流会圆满落幕:一个人就一支研发团队
算法
ACM-moran6 小时前
条件显化(二分单峰函数+构造数列)
算法·推公式·整数三分