LeetCode记录之20——Valid Parentheses
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的更多相关文章
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- leetCode练题——20. Valid Parentheses
1.题目 20. Valid Parentheses——Easy Given a string containing just the characters '(', ')', '{', '}', ...
- <LeetCode OJ> 20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 【leetcode❤python】 20. Valid Parentheses
#-*- coding: UTF-8 -*-#利用栈的思想#如果输入的左测扩则入栈,如果输入为右侧扩,判断其是否与当前栈顶配对,如果匹配则删除这一对,否则return False#'(', ')', ...
- leetcode个人题解——#20 Valid Parentheses
class Solution { public: bool isValid(string s) { stack<char> brackts; ; i < s.size(); i++) ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- 20. Valid Parentheses【leetcode】
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- SqlDataAdapter 批量更新数据库表
在数据库中批量插入数据许多人都已经了解了,就是使用.net 中的SqlBulkCopy对象(MSDN详解).我们在做评教系统的时候使用过这个对象,它能将数据表批量导入到数据库中,效率比单条插入数据效率 ...
- Requests接口测试(一)
接口测试概念 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻辑依赖关 ...
- Haar-like feature和Haar wavelet
Haar-like features are digital image features used in object recognition. They owe their name to the ...
- Bitmap压缩到指定尺寸大小,获取圆角、圆形图片
/** * 使用Matrix将Bitmap压缩到指定大小 * @param bitmap * @param w * @param h * @return */ public static Bitmap ...
- Android 单位dp和px之间相互转换
public class DensityUtil { /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public static int dip2px(Context con ...
- oracle 中用法dual
dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录.我们可以用它来做很多事情. dual是一个虚拟表,用来构成select的语法规则,oracle保证d ...
- .net Reflection(反射)- 一
Reflection 反射需要引用 using System.Reflection; 命名空间. 通过 Assembly 类的 Load( ); 加载指定的 程序集 Assembly 是不能被实例化 ...
- SpringMVC+Hibernate 项目开发之二 (STS整合Maven)
为什么用STS不用Eclipse,主要是Eclipse集成Maven把我整疯了,最后估计原因除在网速上了. 其实用了STS以后发现还真比Eclipse好用点. STS本身集成有Maven的,但是默认的 ...
- Day3作业 .
,))::])]): :-])# 3,使用while和for循环分别打印字符串s=’asdfer’中每个元素. # 4,实现一个整数加法计算器:# 如:content = input(‘请输入内容:’ ...
- 已经上架的app在AppStore上搜不到的解决办法
1.问题呈现 相信很多人都遇到过这个问题,天天刷iTunes connect,终于发现app已经上架了,兴奋的跑过去告诉老板,老板说好,大家都装一个吧! 这时候只能一边不慌不忙地甩锅给苹果,一边快马加 ...