力扣爆刷第112天之CodeTop100五连刷46-50

力扣爆刷第112天之CodeTop100五连刷46-50

一、148. 排序链表

题目链接:https://leetcode.cn/problems/sort-list/description/
思路:链表的归并排序,先递归分割链表,然后比较合并,注意分割操作,和边界条件。


class Solution {
    public ListNode sortList(ListNode head) {
        return cutList(head, null);
    }

    ListNode cutList(ListNode head, ListNode end) {
        if(head == null) return head;
        if(head.next == end) {
            head.next = null;
            return head;
        }
        ListNode slow = head, fast = head;
        while(fast != end) {
            slow = slow.next;
            fast = fast.next;
            if(fast != end) {
                fast = fast.next;
            }
        }
        ListNode left = cutList(head, slow);
        ListNode right = cutList(slow, end);
        return merge(left, right);
    }
    
    ListNode merge(ListNode node1, ListNode node2) {
        ListNode root = new ListNode(), temp = root;
        while(node1 != null && node2 != null) {
            if(node1.val <= node2.val) {
                temp.next = node1;
                node1 = node1.next;
            }else{
                temp.next = node2;
                node2 = node2.next;
            }
            temp = temp.next;
        }
        if(node1 != null) {
            temp.next = node1;
        }
        if(node2 != null) {
            temp.next = node2;
        }
        return root.next;
    }
   
}

二、22. 括号生成

题目链接:https://leetcode.cn/problems/generate-parentheses/description/
思路:排列题,但是不需要考虑去重,只需要正常排列,然后收获结果的时候进行判断是否合法。

class Solution {
    char[] source  = {'(', ')'};
    List<String> list = new ArrayList<>();
    StringBuilder builder = new StringBuilder();
    public List<String> generateParenthesis(int n) {
        backTracking(n*2);
        return list;
    }
    
    void backTracking(int n) {
        if(builder.length() == n) {
            if(isTrue(builder)) {
                list.add(builder.toString());
            }
            return;
        }
        for(int i = 0; i < 2; i++) {
            builder.append(source[i]);
            backTracking(n);
            builder.deleteCharAt(builder.length()-1);
        }
    }
    boolean isTrue(StringBuilder builder) {
        int n = 0;
        for (int i = 0; i < builder.length(); i++) {
            if(builder.charAt(i) == '(') {
                n++;
            }else{
                n--;
            }
            if(n < 0) return false;
        }
        return n == 0;
    }
}

三、70. 爬楼梯

题目链接:https://leetcode.cn/problems/climbing-stairs/description/
思路:最简单的动态规划,每一个位置依赖于前两个位置,分别为跨一步和跨两步。

class Solution {
    public int climbStairs(int n) {
        if(n < 4) return n;
        int a = 1, b = 2, c = 0;
        for(int i = 3; i <= n; i++) {
            c = a + b;
            a = b;
            b = c;
        }
        return c;
    }
}

四、2. 两数相加

题目链接:https://leetcode.cn/problems/add-two-numbers/description/
思路:遍历相加,然后用一个变量记录进制值,下次计算时加进去。


class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int res = 0;
        ListNode root = new ListNode(), p = root;
        while(l1 != null || l2 != null) {
            int a = l1 != null ? l1.val : 0;
            int b = l2 != null ? l2.val : 0;
            int sum = a+b+res;
            res = sum / 10;
            ListNode t = new ListNode(sum%10);
            p.next = t;
            p = t;
            l1 = l1 != null ? l1.next : null;
            l2 = l2 != null ? l2.next : null;
        }    
        if(res == 1) {
            p.next = new ListNode(1);
        }
        return root.next;
    }

}

五、165. 比较版本号

题目链接:https://leetcode.cn/problems/compare-version-numbers/description/
思路:利用乘10循环计数的方法,前导0乘10后还是0,剩下的就简单轻松了,不等就判断返回,等就进行下一轮计算。

class Solution {
    public int compareVersion(String version1, String version2) {
        int i = 0, j = 0, m = version1.length(), n = version2.length();
        while(i < m || j < n) {
            int x = 0;
            for(; i < m && version1.charAt(i) != '.'; i++) {
                x = x * 10 + version1.charAt(i) - '0';
            }
            i++;
            int y = 0;
            for(; j < n && version2.charAt(j) != '.'; j++) {
                y = y * 10 + version2.charAt(j) - '0';
            }
            j++;
            if(x != y) {
                return x > y ? 1 : -1;
            }
        }
        return 0;
    }
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在VS Code力扣LeetCode)题目,首先需要进行以下几步操作: 1. 安装VS Code插件:在VS Code中搜索并安装LeetCode插件。这个插件可以提供LeetCode题目的在线编写和提交功能,以及自动测试和调试代码的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【史上最强代码编辑器VS Code】之VS Code力扣LeetCode)题目](https://blog.csdn.net/weixin_44553006/article/details/105183522)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [在 vscode力扣 Leetcode 可以这样来](https://blog.csdn.net/u012190388/article/details/121277555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [leetcode答案-algo-study:从零开始力扣(LeetCode),JavaScript语言,算法](https://download.csdn.net/download/weixin_38680764/19920528)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

当年拼却醉颜红

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值