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.

解题思路:

由于"()","[()]","{{({[]})}}"都会通过,即出现')]}'这种字符就需要判断前面字符是否是对应的字符,如果用指针的话,实现起来相当复杂,考虑到这种情况和栈的后进先出颇为相似,因此采用stack类

JAVA实现如下:

	static public boolean isValid(String s) {
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < s.length(); i++) {
int pos = "()[]{}".indexOf(s.charAt(i));
if (pos % 2 !=0) {
if (stack.isEmpty() || stack.pop() != pos - 1)
return false;
} else
stack.push(pos);
}
return stack.isEmpty();
}

C++

  class Solution {
public:
bool isValid(string s) {
stack<char> stk;
for (int i = ; i < s.length(); i++) {
if (s[i] == '(' || s[i] == '[' || s[i] == '{')
stk.push(s[i]);
else {
if (stk.empty())
return false;
if (stk.top() == '(' && s[i] == ')')
stk.pop();
else if (stk.top() == '[' && s[i] == ']')
stk.pop();
else if (stk.top() == '{' && s[i] == '}')
stk.pop();
else
return false;
}
}
return stk.empty();
}
};

【JAVA、C++】LeetCode 020 Valid Parentheses的更多相关文章

  1. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  2. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 【JAVA、C++】LeetCode 019 Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  5. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  7. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  8. 【JAVA、C++】LeetCode 006 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

随机推荐

  1. 【CodeForces 626E】Simple Skewness

    题意 给出n个数的集合,求一个 (平均数-中位数)最大 (偏度最大)的子集,输出子集元素个数和各个元素(任意顺序). 分析 因为是子集,所以不一定是连续的序列.然后我们有下面几个结论. 1.最大偏度一 ...

  2. 左偏树(DP)问题

    问题:A straight dirt road connects two fields on FJ's farm, but it changes elevation more than FJ woul ...

  3. dijkstra,SPFA,Floyd求最短路

    Dijkstra: 裸的算法,O(n^2),使用邻接矩阵: 算法思想: 定义两个集合,一开始集合1只有一个源点,集合2有剩下的点. STEP1:在集合2中找一个到源点距离最近的顶点k:min{d[k] ...

  4. 轻量级应用开发之(02)UIView

    一 控件 1.屏幕上的所有UI元素都叫做控件(也有叫做视图.组件)比如按钮(UIButton).文本(UILabel)都是控件. 2.控件的共同属性有哪些? 尺寸,位置,背景色 3. 苹果将控件的共同 ...

  5. CentOS 6.5(6.4)安装过程图文教程

    CentOS 6.4是最新的出的系统,这里分享下安装教程,有些设置大部分教程没出现过,特分享下,方便需要的朋友 1.首先,要有一张CentOS 6.4的安装介质,使用介质启动电脑出现如下界面 界面说明 ...

  6. ExtJS入门教程03,form中怎能没有validation

    接上篇内容,我们在学会extjs form的基本用法之后,今天我们来看看extjs form的validation功能. 必填项,就是不能为空(allowBlank) 效果: 代码: { xtype: ...

  7. KxMenu下拉菜单

    + (void)createMenu:(id)sender target:(UIViewController *)t { NSArray *menuItems = @[ [KxMenuItem men ...

  8. Android Studio-设置switch/case代码块自动补齐

    相信很多和我一样的小伙伴刚从Eclipse转到Android Studio的时候,一定被快捷键给搞得头晕了,像Eclipse中代码补齐的快捷键是Alt+/ ,但是在AS中却要自己设置,这还不是问题的关 ...

  9. ssi技术初探

    http://blog.sina.com.cn/s/blog_765941620100wiir.html

  10. 微信内测"微视频" 朋友圈可以发6-8秒短视频

    我们在发朋友圈时现在只能发文字和图片,图片又分为从相册中选择和直接拍摄照片,以后朋友圈拍6-8秒短视频可能是一种新的内容形式,作为文字和图片外的补充.因为微信开始内测新功能“微视频”,其产品理念与微视 ...