leetcode原题:

char temp ;
Stack<Character> stack = new Stack<Character>();//error:Stack<char> stack = new Stack<char>();
//思路,因为他要的是顺序要对。所以我先把(,[,{的存入栈,遇到反向的时候看一下最近的一个是不是他的配对
for(int i = 0; i < s.length(); i++)
{
temp = s.charAt(i);
if(temp == '(' || temp == '[' || temp == '{')
{
stack.push(temp);
}
else
{
if(stack.empty())
{
return false;
}//error: not write

if(temp == ')' && stack.pop() != '(') //error: if(temp == ')' && stack.pop() != "(")
{
return false;
}
if(temp == ']' && stack.pop() != '[')
{
return false;
}
if(temp == '}' && stack.pop() != '{')
{
return false;
}
}
}
return stack.empty();//error: return true;

9.5---括号是否有效(CC150)的更多相关文章

  1. [cc150] 括号问题

    Implement an algorithm to print all valid ( properly opened and closed) combinations of n-pairs of p ...

  2. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  3. 面试题目——《CC150》递归与动态规划

    面试题9.1:有个小孩正在上楼梯,楼梯有n个台阶,小孩一次可以上1阶.2阶或者3阶.实现一个方法,计算小孩有多少种上楼梯的方式. 思路:第4个数是前三个数之和 注意:能不能使用递归,能不能建立一个很大 ...

  4. javascript匹配各种括号书写是否正确

    今天在codewars上做了一道题,如下 看上去就是验证三种括号各种嵌套是否正确书写,本来一头雾水,一种括号很容易判断, 但是三种怎么判断! 本人只是个前端菜鸟,,不会什么高深的正则之类的. 于是,在 ...

  5. 明显调用的表达式前的括号必须具有(指针)函数类型 编译器错误 C2064

    看到“明显调用的表达式前的括号必须具有(指针)函数类型”这句时我才发现我的语文水平有多烂,怎么看都看不懂,折腾了半天才知道是哪里出了问题. 举个简单的例子 class CTest { void (CT ...

  6. [LeetCode] Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  7. [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  8. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. [LeetCode] Generate Parentheses 生成括号

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

  10. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

随机推荐

  1. linux 开机启动过程详解

    Linux开机执行内核后会启动init进程,该进程根据runlevel(如x)执行/etc/rcx.d/下的程序,其下的程序是符号链接,真正的程序放在/etc/init.d/下.开机启动的程序(服务等 ...

  2. autofac获取全局Container

    _UserService = ((IContainerProviderAccessor)HttpContext.Current.ApplicationInstance).ContainerProvid ...

  3. 固定IP 正常访问谷歌

    如题 地址栏直接输入 http://173.194.1.150/ 正常使用 ~标记一下~

  4. Android Studio-设置代码自动提示

    None:代表模糊匹配(推荐). First Letter: 根据首字母进行匹配. All:与First Letter类似,不过匹配结果比First Letter多.

  5. css使 同一行内的 文字和图片 垂直居中对齐?

    设置父容器, 使 父容器 div 下的所有元素 都 垂直对齐: father-container *{ vertical-align:middle 找回密码

  6. linuxMint設置窗口最大最小化

    linuxMint下面用键盘快速让窗口最大化和最小化

  7. [歪谈]拽一个贵人出来给你"当炮架子"

    我们在古装神话剧中经常会听到某个“先知”对前来算命的人说:你会在某某时刻遇到你的贵人.而这个贵人会在事业上助你一臂之力. 这里有个问题:贵人到底是什么?我们怎样去寻找我们的贵人. 前几天有个网友来咨询 ...

  8. angularjs controller的两种写法

    在Angular中,Directive.Service.Filter.Controller都是以工厂方法的方式给出,而工厂方法的参数名对应着该工厂方法依赖的Service.如: app.control ...

  9. List<List<double>> lsls = null; 根据double值来重新排序lsls...

    "确定:Node-data = (7,2).具体是:根据x维上的值将数据排序, 6个数据的中值(所谓中值,即中间大小的值)为7, 所以Node-data域位数据点(,).这样, 该节点的分割 ...

  10. iOS之下拉放大,上推缩小,一个方法搞定

    先来看看效果吧. 讲讲大概的实现思路:1、创建头部的视图和tableview,需要注意的是tableview要设置contentInset,contentInsent 的顶部要和头部视图的背景图的高度 ...