力扣题2022.10.3

1 力扣题20:有效的括号

1.1 题目描述

给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。 有效字符串需满足:
左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。 每个右括号都有一个对应的相同类型的左括号。

1.2 思路分析

1.2.1 栈

可借助栈,将(,{,[入栈,遇),},]出栈匹配,不匹配则括号无效,同时需考虑一些特殊情况。

1.3 代码实现

1.3.1 栈

public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        boolean result = true;
        for (int i=0; i<s.length(); i++) {
            if (s.charAt(i)=='('||s.charAt(i)=='{'||s.charAt(i)=='[')
                stack.push(s.charAt(i));
            else {
                if (stack.size()==0) {
                    result = false;
                    break;
                }
                char c = stack.pop();
                if (((s.charAt(i)==')'&&c=='(')||
                (s.charAt(i)=='}'&&c=='{')||
                (s.charAt(i)==']'&&c=='[')))
                    continue;
                else {
                    result = false;
                    break;
                }
            }
        }
        if (stack.size()!=0)
            result = false;
        return result;
    }

2 力扣题21:合并两个链表

2.1 题目描述

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

2.2 思路分析

2.2.1 顺序接

按顺序比较,直到一个链表为空,之后将不为空的链表的后面节点直接接上新链表的后面,注意特殊情况。

2.3 代码实现

2.3.1 顺序接

public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1==null&&list2==null)
            return null;
        ListNode result = new ListNode();
        ListNode tmp = new ListNode();
        boolean first = true;
        while(list1!=null && list2!=null) {
            if (list1.val>=list2.val) {
                if (first) {
                    result = new ListNode();
                    tmp = result;
                    tmp.val = list2.val;
                    first = false;
                } else {
                    ListNode newNode = new ListNode();
                    newNode.val = list2.val;
                    tmp.next = newNode;
                    tmp = newNode;
                }
                list2 = list2.next;
            } else {
                if (first) {
                    result = new ListNode();
                    tmp = result;
                    tmp.val = list1.val;
                    first = false;
                } else {
                    ListNode newNode = new ListNode();
                    newNode.val = list1.val;
                    tmp.next = newNode;
                    tmp = newNode;
                }
                list1 = list1.next;
            }
        }
        if (first) {
            if (list1!=null) {
                result = new ListNode();
                tmp = result;
                tmp.val = list1.val;
                first = false;
                list1 = list1.next;
            }
            if (list2!=null) {
                result = new ListNode();
                tmp = result;
                tmp.val = list2.val;
                first = false;
                list2 = list2.next;
            }
        }
        if (list1!=null) 
            tmp.next = list1;
        if (list2!=null) 
            tmp.next = list2;
        return result;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值