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实现

    纯JS实现无限极分类 <!DOCTYPE html> <html> <head> <title></title>//引入Jquery < ...

  2. Linux修复MBR扇区故障

    给虚拟机增加一块硬盘,用于备份mbr的信息 fdisk -l 查看硬盘系统是否认识 fdisk /dev/sdb 进行分区 fdisk -l 查看分区是否出来 mkfs -t ext4 /dev/sb ...

  3. 2014蓝桥杯B组初赛试题《切面条》

    题目描述: 一根高筋拉面,中间切一刀,可以得到2根面条.     如果先对折1次,中间切一刀,可以得到3根面条.     如果连续对折2次,中间切一刀,可以得到5根面条.     那么,连续对折10次 ...

  4. msql 计算连续签到天数

    刚刚写了一个签到计算天数的sql, 记录下来. 思路如下: 获取当前签到的最后时间(今天或昨天), 定义一个变量@i 对签到时间进行天数自减, 然后查询出当前记录签到时间是否与自减后的时间匹配.   ...

  5. 数字图像处理实验(17):PROJECT 06-04,Color Image Segmentation 标签: 图像处理MATLAB 2017-05-27 21:13

    实验报告: Objective: Color image segmentation is a big issue in image processing. This students need to ...

  6. NEERC17 J Journey from Petersburg to Moscow

    CF上可以提交.   链接 依然是很妙的解法. 我们可以枚举每一个出现过的边权$L$,然后把所有边的边权减掉这个$L$,如果小于$L$就变为$0$,然后跑一遍最短路然后加上$k * L$更新答案即可. ...

  7. hdu 4279 Number(G++提交)

    打表找规律: #include<stdio.h> #include<math.h> #define N 250 bool judge(int i,int j) { ;k< ...

  8. HDU 3724 Encoded Barcodes (Trie)

    题意:给n个字符串,给m个询问,每个询问给k个条形码.每个条形码由8个小码组成,每个小码有相应的宽度,已知一个条形码的宽度只有2种,宽的表示1,窄的表示0.并且宽的宽度是窄的宽度的2倍.由于扫描的时候 ...

  9. Haar-like feature和Haar wavelet

    Haar-like features are digital image features used in object recognition. They owe their name to the ...

  10. txt文本怎么去除重复项

    txt文本怎么去除重复项?做网络推广的朋友经常会遇到这样的问题,txt文本文件里面有许多人名或者电话号码用来发送邮件或者短信,通常有许多是重复的,下面我来介绍两个方法来去除重复项,以人名为范本讲解. ...