IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]LeetCode -- Valid Parentheses

    csharp25发表于 2015-12-01 09:51:13
    love 0
    题目描述:
    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.




    在一个只包含"()[]{}"的输入中,判断括号的合法性。


    思路:
    栈的基本使用。


    实现代码:


    public class Solution {
        public bool IsValid(string s) 
        {
            var stack = new Stack<char>();
    	
        	for(var i = 0;i < s.Length; i++){
        		switch(s[i]){
        			case '{':
        			case '[':
        			case '(':
        				stack.Push(s[i]);
        				break;
        			case '}':
        				if(stack.Count > 0 && stack.Peek() == '{'){
        					stack.Pop();
        				}
        				else{
        					return false;
        				}
        				break;
        			case ']':
        				if(stack.Count > 0 && stack.Peek() == '['){
        					stack.Pop();
        				}
        				else{
        					return false;
        				}
        				break;
        			case ')':
        				if(stack.Count > 0 && stack.Peek() == '('){
        					stack.Pop();
        				}
        				else{
        					return false;
        				}
        				break;
        			default :
        				throw new ArgumentException(s);
        		}
        	}
        	
        	return stack.Count == 0;
        }
    }




沪ICP备19023445号-2号
友情链接