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. 696. Count Binary Substrings统计配对的01个数

    [抄题]: Give a string s, count the number of non-empty (contiguous) substrings that have the same numb ...

  2. SpringMVC 课程第一天

    SpringMVC第一天   框架课程 1. 课程计划 第一天 1.SpringMVC介绍 2.入门程序 3.SpringMVC架构讲解 a) 框架结构 b) 组件说明 4.SpringMVC整合My ...

  3. c语言学习笔记 多级else if 和switch case有什么区别

    ; ) { dosth(); } ) { dosth2(); } else if(opion==) { dosth3(); } else dosth4(); 如果给option的一个值是2的话,那么程 ...

  4. Part10-C语言环境初始化-一跃进入C大门lesson3

    1.跳转到c代码 因为内存中的代码来自于垫脚石SRAM,他们是相同的. 采用绝对跳转方式来完成. 因为我们是从汇编代码跳转到c语言的程序,所以我们要提前准备一个main.c文件. 修改makefile ...

  5. scala的futue和promise

    异步操作的有两个经典接口:Future和Promise,其中的 Future 表示一个可能还没有实际完成的异步任务的结果,针对这个结果可以添加 Callback 以便在任务执行成功或失败后做出对应的操 ...

  6. 制作3D旋转视频展示区

    CSS3 3D变形制作视频展示区 <!doctype html> <html lang="en"> <head> <meta charse ...

  7. Responsive设计——meta标签

    media-queries.js(http://code.google.com/p/css3-mediaqueries-js/) respond.js(https://github.com/scott ...

  8. 无法解析的外部符号 _WinMain@16(原)

    原来的控制台程序,想修改为windows程序时,会出现 无法解析的外部符号 WinMain,该符号在函数 __tmainCRTStartup 中被引用 在链接器->高级->入口点输入:ma ...

  9. eWebEditor9.x整合教程-Xproer.WordPaster

    版权所有 2009-2017 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/wordpa ...

  10. Matlab Simulink