Q1:Valid Parentheses
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的更多相关文章
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 72. Generate Parentheses && Valid Parentheses
Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LintCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码
Longest Valid Parentheses My Submissions Question Solution Total Accepted: 47520 Total Submissions: ...
随机推荐
- C#中结构(struct)的部分初始化和完全初始化
假设有这样一个值类型struct. public struct Size { public int Length; public int Width; public int Area() { retu ...
- 【微信小程序】微信小程序 文本过长,自动换行的问题
小程序开发过程出现的问题: 文本过长,以致于在view中显示不全,没有自动换行的问题 解决方法: 在wxss样式文件中添加样式 .font-break { word-break:break-all; ...
- 聊聊高并发(十四)理解Java中的管程,条件队列,Condition以及实现一个堵塞队列
这篇里面有一些主要的概念,理解概念是件有意义的事情,仅仅有理解概念才干在面对详细问题的时候找到正确的解决思路.先看一下管程的概念 第一次在书上看到管程这个中文名称认为非常迷糊,管程究竟是个什么东东,于 ...
- 更改Mantis的logo
1 准备好自己的logo,例如准备的logo为zhaoxiyu.gif.zxy.gif 2 把上面的两个logo存放到C:/mantis-1.0.0a3/images 3 打开C:/mantis-1. ...
- Mantis邮件服务器配置
1.在apache下的php.ini中修改(这里是以163的邮箱为例子):[mail function];For Win32 only.SMTP = smtp.163.com; //设置邮箱的发送地址 ...
- json的好处-新一代数据传输利器
JSON是一种轻量级的数据交换格式!和xml一样. 为什么不XML XML的冗余太大,不过XML阅读起来比较方面,所以并没有被json完全取代,很多时候都是并存.比如sina微博的开发平台有一个JSO ...
- UVA 156 (13.08.04)
Ananagrams Most crossword puzzle fans are used to anagrams--groupsof words with the same letters i ...
- go语言基础之go猜数字游戏
1. 产生一个随机的4位数 示例1: package main import "fmt" import "math/rand" import "tim ...
- kafka基本原理概述——patition与replication分配
kafka一直在大数据中承受着数据的压力也扮演着对数据维护转换的角色,下面重点介绍kafka大致组成及其partition副本的分配原则: 文章参考:http://www.linkedkeeper.c ...
- Cognos业务洞察力:My First Business Insight
Cognos Dashboard Cognos Dashboard 可以展示具有重要影响力的信息,以监视.衡量和管理企业绩效. IBM Cognos Dashboard(仪表盘)使任何用户能够以支持其 ...