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. 为什么说CLR是类型安全的

    CLR总是知道托管堆上的对象是什么类型,这是CLR类型安全的前提.托管堆上的每个对象都有一个"类型对象指针",指向托管堆上Type对象的一个实例.我们总是可以通过System.Ob ...

  2. 使用jquery加载部分视图02-使用$.ajax()

    本篇体验使用$.ajax()加载部分视图.与加载部分视图相关的包括: RenderPartial和RenderAction区别   使用jquery加载部分视图01-使用$.get()       □ ...

  3. Android中UI设计的一些技巧!!!

    出处:http://blog.csdn.net/android_tutor/article/details/5995759 大家好,今天给大家分享的是Android中UI设计的一些技巧,本节内容主要有 ...

  4. Android - 警告Nested weights are bad for performance

    Android - 警告Nested weights are bad for performance 本文地址: http://blog.csdn.net/caroline_wendy 原因: Lay ...

  5. Spring9 : Autowire(自动装配)机制

    原文出处: 五月的仓颉 为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: 1 2 3 4 5 public interface Animal { ...

  6. 缓存算法:LRU、LFU、FIFO

      LRU全称是Least Recently Used,即最近最久未使用的意思.如果一个数据在最近一段时间没有被访问到,那么在将来它被访问的可能性也很小.也就是说,当限定的空间已存满数据时,应当把最久 ...

  7. fdisk 分区格式化为ext4格式分区

    第一步:添加硬盘/新建分区(fdisk) 第二步:格式化分区(mkfs.ext4) 第三步:加载分区(mount) 1.第一步:添加硬盘/新建分区(fdisk) a.查看当前系统所有硬盘及分区情况:f ...

  8. [leetcode]Single Number II @ Python

    原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...

  9. Bootstrap学习js插件篇之标签页

    简单的标签页 代码: <h1 class="page-header">4.3标签页</h1> <ul class="nav nav-tabs ...

  10. Android -- VelocityTracker

    VelocityTracker 主要应用于touch event, VelocityTracker通过跟踪一连串事件实时计算出当前的速度. 方法 //获取一个VelocityTracker对象, 用完 ...