Question:

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.

MyAnswer 1 (C++):

 class Solution {
public:
bool isValid(string s) {
char stack[s.size()];
int i = ;
int j = ; //cout << s; if(s[j] == ')' || s[j] == ']' || s[j] =='}')
return false;
else if(s[j] == '\0')
return true;
else
stack[i++] = s[j++]; while()
{
switch(s[j])
{
case '\0':
if(i == )
return true;
else
return false;
case ')':
if(stack[i-] != '(')
return false;
else
{
i -= ;
j += ;
}
break;
case ']':
if(stack[i-] != '[')
return false;
else
{
i -= ;
j += ;
}
break;
case '}':
if(stack[i-] != '{')
return false;
else
{
i -= ;
j += ;
}
break;
default:
stack[i++] = s[j++];
break;
}
}
}
};

数组模拟栈,匹配到括号则出栈,否则入栈

MyAnswer 2 (Java):

 public class Solution {
public boolean isValid(String s) {
ArrayList<Character> stack = new ArrayList<Character>();
int i = 0;
for(i = 0; i < s.length(); i++){
switch(s.charAt(i)){
case ')':
if(stack.isEmpty() || stack.get(stack.size()-1) != '(')
return false;
else
stack.remove(stack.size()-1);
break;
case ']':
if(stack.isEmpty() || stack.get(stack.size()-1) != '[')
return false;
else
stack.remove(stack.size()-1);
break;
case '}':
if(stack.isEmpty() || stack.get(stack.size()-1) != '{')
return false;
else
stack.remove(stack.size()-1);
break;
default:
stack.add(s.charAt(i));
break;
}
}
if(stack.isEmpty())
return true;
else
return false;
}
}

Q1:Valid Parentheses的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. [LeetCode] Valid Parentheses 验证括号

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

  3. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. 72. Generate Parentheses && Valid Parentheses

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

  5. leetcode 32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  6. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  7. 【leetcode】 Longest Valid Parentheses (hard)★

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. [LintCode] Valid Parentheses 验证括号

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

  9. Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码

    Longest Valid Parentheses My Submissions Question Solution  Total Accepted: 47520 Total Submissions: ...

随机推荐

  1. 【docker】docker启动、重启、关闭命令,附带:docker启动容器报错:docker: Error response from daemon: driver failed programming external connectivity on endpoint es2-node

    在关闭并放置centos 的防火墙重启之后[操作:https://www.cnblogs.com/sxdcgaq8080/p/10032829.html] 启动docker容器就发现开始报错: [ro ...

  2. Kafka分布式环境搭建 (二)赞

    这篇文章将介绍如何搭建kafka环境,我们会从单机版开始,然后逐渐往分布式扩展.单机版的搭建官网上就有,比较容易实现,这里我就简单介绍下即可,而分布式的搭建官网却没有描述,我们最终的目的还是用分布式来 ...

  3. Go 语言简介(上)— 语法

    周末天气不好,只能宅在家里,于是就顺便看了一下Go语言,觉得比较有意思,所以写篇文章介绍一下.我想写一篇你可以在乘坐地铁或公交车上下班时就可以初步了解一门语言的文章.所以,下面的文章主要是以代码和注释 ...

  4. ffmpeg 2.3版本号, 关于ffplay音视频同步的分析

    近期学习播放器的一些东西.所以接触了ffmpeg,看源代码的过程中.就想了解一下ffplay是怎么处理音视频同步的,之前仅仅大概知道通过pts来进行同步,但对于怎样实现却不甚了解,所以想借助这个机会, ...

  5. wiggle-subsequence

    // 参考了:https://discuss.leetcode.com/topic/51893/two-solutions-one-is-dp-the-other-is-greedy-8-lines ...

  6. IOS开发-提升app性能的25条建议和技巧

    前言 这篇文章介绍了作者开发工作中总结的25个iOS开发tips, 多年之前读过这篇文章.收益良多,基本每一个tips在我的应用开发过程中都使用过.今天把这篇文章又一次整理转发下,与大家一起学习,不论 ...

  7. THINKPHP URL模块大小写导致404问题

    最近我使用THINKPHP开发了一个项目在本地的集成开发环境wampserver做开发时并没有出现问题 上传到linux系统也没有出现问题,但当上传到windows平台上就出现了问题"文件4 ...

  8. 微信小程序开发及相关设置小结

    今年过年,主要看了<奇葩说>和<电锯惊魂>,很不错,好东西的确需要留出足够的时间来看,匆匆忙忙走马观花是对作者的不尊重.除此之外,就是研究了一下微信小程序开发,先说对小程序的看 ...

  9. 如何使用千千静听为MP3添加专辑封面和文字信息

    使用千千静听播放器打开某MP3文件,右击该文件,选择属性. 2 点击专辑封面即可添加或更换专辑封面 点击保存到文件再点击重新读取文件即可发现有效了 3 为MP3批量添加添加封面 选中播放列表的所有文件 ...

  10. android studio中使用adb wifi插件无线调试程序

    使用android studio中使用adb wifi插件无线调试程序的前提条件电脑和手机在同一个无线网 1.下载adb wifi插件 File->Settings->Plugins Br ...