09.18更新算法采用栈的思想解决,方法①所示。


本题主要是找是否有匹配的字符串,因为还没有复习到栈之类的知识点,只能还是采用暴力方法了,后期会补上更加优化的算法。我的思路就是先遍历一遍找是否有匹配的符号,有的话就删除,然后继续遍历,直至结束。

Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

给定一个字符串,只包含字符“(”、“””、“{”、“}”、“[”和“”),确定输入字符串是否有效。
括号必须以正确的顺序关闭,“()”和“()”{“}”都是有效的,但“()和[([ ] ] ] ]不是。


①利用栈的思想进行解决。

 /*
* 利用栈来进行平衡符号的匹配。
* {[(三种符号是开放字符,)]}是封闭符号。
* 当前字符为开放符号时,入栈。当为封闭符号时,出栈,并与出栈元素进行匹配。
* 当循环结束时,栈为空即所有符号都进行匹配,配对成功。
* */
public boolean isValid(String s){
Stack<Character> chars=new Stack<>();
for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
switch (ch) {
case '(':
chars.push(ch);
break;
case ')':
if(chars.empty())
return false;
else {
char getCh=chars.pop();
if(!(getCh=='('))
return false;
}
break;
case '[':
chars.push(ch);
break;
case ']':
if(chars.empty())
return false;
else {
char getCh=chars.pop();
if(!(getCh=='['))
return false;
}
break;
case '{':
chars.push(ch);
break;
case '}':
if(chars.empty())
return false;
else {
char getCh=chars.pop();
if(!(getCh=='{'))
return false;
}
break;
default:
break;
}
}
if(chars.isEmpty())
return true;
else
return false;
}

②利用暴力方式进行解决(不推荐)

 public boolean isValid(String s){
int length=s.length();
boolean isDelete=false;
if(length==0||length%2!=0)//判断字符串是否为空或者无法匹配
return false;
while (length >0) {
for (int i = 0; i < length - 1; i++) {
if ((s.charAt(i) == '(' && s.charAt(i + 1) == ')') || (s.charAt(i) == '{' && s.charAt(i + 1) == '}')
|| (s.charAt(i) == '[' && s.charAt(i + 1) == ']')) {
if(i+2!=length)
s = s.substring(0, i) + s.substring(i + 2, length);//非最后两位字符串截取
else
s = s.substring(0, i);//最后两位字符串截取
length -= 2;//字符串长度减2
isDelete=true;
i=0;//每次将基数归零重新循环
}
else
isDelete=false;//如果循环一次没有任何匹配直接返回false
}
if (!isDelete)
return false;
}
return true;
}

LeetCode记录之20——Valid Parentheses的更多相关文章

  1. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

  2. leetCode练题——20. Valid Parentheses

    1.题目 20. Valid Parentheses——Easy  Given a string containing just the characters '(', ')', '{', '}',  ...

  3. &lt;LeetCode OJ&gt; 20. Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  4. 【leetcode❤python】 20. Valid Parentheses

    #-*- coding: UTF-8 -*-#利用栈的思想#如果输入的左测扩则入栈,如果输入为右侧扩,判断其是否与当前栈顶配对,如果匹配则删除这一对,否则return False#'(', ')', ...

  5. leetcode个人题解——#20 Valid Parentheses

    class Solution { public: bool isValid(string s) { stack<char> brackts; ; i < s.size(); i++) ...

  6. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  7. 20. Valid Parentheses【leetcode】

    20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  8. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  9. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

随机推荐

  1. js弹出窗口

    function openModalDialog(url, height, width) { var t_height = 400; var t_width = 600; if (!isNaN(hei ...

  2. Luogu 3957 [NOIP2017]普及组 跳房子

    写了好久,感觉自己好菜,唉…… 首先发现这个$g$的取值具有单调性,可以想到二分答案,然后考虑用$dp$来检验,这样子可以写出朴素的转移方程: 设$f_i$表示以$i$结尾的最大价值,那么有$f_i ...

  3. jqentitydetail

    using System;using System.Collections;using System.Collections.Generic;using System.Linq;using Syste ...

  4. javascrip总结12:逻辑运算符与等号运算符

    1 逻辑运算符 逻辑运算的结果只有true 或者 false. 1.1 与&&: 两个表达式为true的时候,结果为true. 1.2 或|| 只要有一个表达式为true,结果为tru ...

  5. HDU 3333 Turing Tree (主席树)

    题意:给定上一个序列,然后有一些询问,求区间 l - r 中有多少个不同的数的和. 析:和求区间不同数的方法是一样,只要用主席树维护就好. 代码如下: #pragma comment(linker, ...

  6. Java 数据结构之数组

    public class Arrays {    //创建一个Integer空数组    public static Integer[] player=null;    //添加球员号码    pri ...

  7. 常用SQL性能统计代码

    1 BEGIN DBMS_STATS.GATHER_TABLE_STATS(OWNNAME=> 'TELEDB', TABNAME=> 'WFGTEST', PARTNAME=> N ...

  8. super() 的入门使用

    在类的继承中,如果重定义某个方法,该方法会覆盖父类的同名方法,但有时,我们希望能同时实现父类的功能,这时,我们就需要调用父类的方法了,可通过使用 super 来实现,比如:     1 2 3 4 5 ...

  9. 七、CommonJS规范和Note.js模块概念的介绍

    在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少,很多 ...

  10. vs2015+opencv3.3.1 实现 c++ 灰度高斯滤波器

    #include <opencv2\highgui\highgui.hpp> #include <iostream> #include<vector> using ...