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. java内存溢出示例(堆溢出、栈溢出)

    堆溢出: /** * @author LXA * 堆溢出 */ public class Heap { public static void main(String[] args) { ArrayLi ...

  2. 微软Silverlight欲攻占iPhone和Android手机

    微软日前表示,该公司正在努力把Silverlight视频技术引入手机市场.微软Silverlight视频技术被誉为“Flash杀手”,该公司前不久刚发布了Silverlight 2.0版. 尽管说苹果 ...

  3. [Android UI] Activity Maintheme (Android 解决程序启动时的黑屏问题)

    <style name="MainTheme" parent="@android:style/Theme"> <item name=" ...

  4. [13] 弧面(Arc)图形的生成算法

    顶点数据的生成 bool YfBuildArcVertices ( Yreal radius, Yreal degree, Yreal height, Yuint slices, Yuint stac ...

  5. crtmpserver实现防盗流和流推送验证

    Protecting your streams from webpage copy&paste flash code, listing or recording 保护流,防止在页面上被复制&a ...

  6. C#操作AD及Exchange Server总结(一)

    这篇博客的目的:根据亲身项目经历,总结对AD及Exchange Server的操作,包括新建AD用户,设置密码,为AD用户创建邮箱等. 本文完全原创,转载请说明出处,希望对大家有用. 文档目录: 测试 ...

  7. WebView 加载网页 加载资源 总结 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. 7 个 Bootstrap 在线编辑器用于快速开发响应式网站

    Bootstrap 已经使响应式网站开发变得简单很多. 但是如果你不必手动写全部代码,事情会如何呢? 如果你可以自由地选择你想要使用的Bootstrap 组件.并可以把它们拖拽到画布中,事情会如何呢? ...

  9. C++11中万能的可调用类型声明std::function<...>

    在C++11中,callable object 包括传统C函数,C++成员函数,函数对象(实现了()运算符的类的实例),lambda表达式(特殊函数对象)共4种.程序设计,特别是程序库设计时,经常需要 ...

  10. python3 UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f9e0' in position 230: illegal multibyte sequence

    最近在保存微博数据到(csv文件)时报错: UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f9e0' in positio ...