美文网首页
3.2 leet20|21#括号配对#链表

3.2 leet20|21#括号配对#链表

作者: 反复练习的阿离很笨吧 | 来源:发表于2019-03-12 17:54 被阅读0次

https://leetcode.com/problems/valid-parentheses/

class Solution {
    public boolean isValid(String s) {
        String sta=new String("");

        for(int i=0;i<s.length();i++)
        {
            if(s.charAt(i)=='('||s.charAt(i)=='{'||s.charAt(i)=='[')
                sta+=s.charAt(i);
            if(s.charAt(i)==')')
            {
                if(sta.charAt(sta.length()-1)=='(')
                    sta=sta.substring(0,sta.length()-1);
                else
                    return false;
            }
            if(s.charAt(i)=='}')
            {
                if(sta.charAt(sta.length()-1)=='{')
                    sta=sta.substring(0,sta.length()-1);
                else
                    return false;
            }
            if(s.charAt(i)==']')
            {
                if(sta.charAt(sta.length()-1)=='[')
                    sta=sta.substring(0,sta.length()-1);
                else
                    return false;
            }            
        }
        if(sta=="")
            return true;
        else
            return false;
    }
}

思路就是用栈,但不会写栈,就写了个string。
算法:
循环判断:

  1. 如果是({[,就加入字符串sta中
  2. 如果是})],就在字符串sta中找有没有匹配的左括号,:
    1 sta为空,false
    2 sta中有,则删除
    3 sta中没有,false
  3. 字符串为空,true;否则false。
class Solution {
    public boolean isValid(String s) {
        String sta=new String("");
    
        for(int i=0;i<s.length();i++)
        {
            if(s.charAt(i)=='('||s.charAt(i)=='{'||s.charAt(i)=='[')
                sta+=s.charAt(i);
            if(s.charAt(i)==')')
            {
                if(sta.length()<1)
                    return false;
                else if(sta.charAt(sta.length()-1)=='(')
                    sta=sta.substring(0,sta.length()-1);
                else
                    return false;
            }
            if(s.charAt(i)=='}')
            {
                if(sta.length()<1)
                    return false;
                else if(sta.charAt(sta.length()-1)=='{')
                    sta=sta.substring(0,sta.length()-1);
                else
                    return false;
            }
            if(s.charAt(i)==']')
            {
                if(sta.length()<1)
                    return false;
                else if(sta.charAt(sta.length()-1)=='[')
                    sta=sta.substring(0,sta.length()-1);
                else
                    return false;
            }
        }
        if(sta.length()==0)     
            return true;                    
        else        
            return false;   
    }
}
  1. 一开始在步骤2没有考虑到sta为空,会溢出。然后就在判断sta有无左括号前加一个是否为空的判断。
  2. 在最后判断字符串为空时,一开始用的是 但是判断的结果不对,是空的也会判成不是。??
    https://leetcode.com/problems/merge-two-sorted-lists/
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null)
            return l2;
        else if(l2==null)
            return l1;
        else if(l1.val<=l2.val)
        {
            l1.next=mergeTwoLists(l1.next,l2);
            return l1;
        }
        else{        
            l2.next=mergeTwoLists(l1,l2.next);
            return l2;
        }
    }
}

怎么写链表:其实很简单
class node {node next;}
node now = new node();
now.next = new node();
new就完事

相关文章

  • 3.2 leet20|21#括号配对#链表

    https://leetcode.com/problems/valid-parentheses/ 思路就是用栈,但...

  • 每天一道算法题4

    【括号有效配对】括号有效配对是指:1) 任何一个左括号都能找到和其正确配对的右括号2)任何一个右括号都能找到和其正...

  • 每天一道算法题5

    【动态规划-有效子串长度】括号有效配对是指:1) 任何一个左括号都能找到和其正确配对的右括号2)任何一个右括号都能...

  • 编程练习-字符串展开

    题目来源:华为实习笔试题 给定一个字符串,其中含有括号(大括号,中括号,小括号),括号可以嵌套,且保证括号是配对的...

  • 1.3.4 判断括号配对

  • 其余代码题

    链表是否有环-done 表达式计算-done 括号匹配-done 最长有效括号长度 链表是否有环 提示:快慢指针 ...

  • ACM5

    括号配对问题 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现在,有一行括号序列,请你...

  • C语言——第五次笔记

    学生管理系统1.明确功能2.数据存储3.准备知识3.1 枚举3.2 链表 (单链表,循环链表,双向链表,双向循环链...

  • [26无验证]牛牛的括号匹配-京东2018秋

    1.题目描述 如果一个括号序列中的每个左括号都有一个右括号与之完成配对,这个序列就是一个合法的括号匹配序列。例如"...

  • week 2019-07-14

    四数之和 括号生成 合并K个链表

网友评论

      本文标题:3.2 leet20|21#括号配对#链表

      本文链接:https://www.haomeiwen.com/subject/ahjgpqtx.html