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: ...
随机推荐
- jQuery插件——可以动态改动颜色的jQueryColor
1.请选择代码框中所有代码后, 保存为 jquery.color.js /*! * jQuery Color Animations v@VERSION * https://github.com/jqu ...
- Ip和long互转
// <summary> /// 将127.0.0.1形式的IP地址转换成十进制整数 /// </summary> /// <param name="strIp ...
- FFmpeg的H264编码有内存泄漏吗??!!!
靠,内存泄漏好严重.开始怀疑是自己代码问题,调试了半天,又反复改写和优化代码,还是泄漏严重. 拿网上现成的FFMPEG H264编码的范例来测试,同样泄漏很严重. 百度了一下,有很多人遇到同样的问题, ...
- 字符串转换atof atoi atol gcvt strtod strtol strto ul toascii tolower toupper
atof(将字符串转换成浮点型数) 相关函数 atoi,atol,strtod,strtol,strtoul 表头文件 #include <stdlib.h> 定义函数 double at ...
- WebService—CXF整合Spring实现接口发布和调用过程
一.CXF整合Spring实现接口发布 发布过程如下: 1.引入jar包(基于maven管理) <!-- cxf --> <dependency> <groupId> ...
- 在Xcode中显示代码行号
打开一个程序,点击屏幕菜单栏的Xcode,然后选Xcode -> Preferences -> Text Editing -> Show line numbers前面打勾就行了. 如 ...
- @Java类加载器及双亲委派模型
类与类加载器 虚拟机设计团队把类加载阶段的"通过一个类的全限定名来获取此类的二进制字节流"这个动作放到Java虚拟机外部去实现,以便让应用程序自己决定如何去获取所需要的类.实现这个 ...
- QT QCharts QScatterSeries 空心点阵图,鼠标移动到上面显示数值,鼠标移开数值消失
在最近接到的需求是这样的,画一个折线图,关键点使用空心的圆点标识出来,鼠标移动到关键点上,显示出当前数值:鼠标移走数值消失. 我们遇到这个需求的时候,第一时间就会想到使用 QLineSeries 画折 ...
- C# WCF 完整实例,winform 窗体作为 宿主
上一次提到,我们的WCF程序宿主是发布到IIS上面的.虽然这样做未尝不可,不过不便于我们进行“开始”或“停止”WCF服务的操作.所以再次尝试了编写以窗体应用程序作为WCF服务宿主的方式,并取得了成功. ...
- Android -- 重置Bitmap大小&&Bitmap转角度
重置Bitmap大小 Bitmap bitMap = ...