Codility---Nesting
A string S consisting of N characters is called properly nested if:
- S is empty;
- S has the form "(U)" where U is a properly nested string;
- S has the form "VW" where V and W are properly nested strings.
For example, string "(()(())())" is properly nested but string "())" isn't.
Write a function:
class Solution { public int solution(String S); }
that, given a string S consisting of N characters, returns 1 if string S is properly nested and 0 otherwise.
For example, given S = "(()(())())", the function should return 1 and given S = "())", the function should return 0, as explained above.
Assume that:
- N is an integer within the range [0..1,000,000];
- string S consists only of the characters "(" and/or ")".
Complexity:
- expected worst-case time complexity is O(N);
- expected worst-case space complexity is O(1) (not counting the storage required for input arguments).
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.Stack;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
Stack<Character> st = new Stack<Character>();
if(S == null)
return 1;
for(int i=0; i<S.length();i++){
if(S.charAt(i) == '(')
st.push('(');
else if(S.charAt(i) == ')' && !st.empty())
st.pop();
else if(S.charAt(i) == ')' && st.empty())
return 0;
}
if(st.empty())
return 1;
else
return 0;
}
}
https://codility.com/demo/results/trainingUYAFS5-NWU/
Codility---Nesting的更多相关文章
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- 译文:Nesting Your BEM?
原文链接:http://csswizardry.com/2016/11/nesting-your-bem/ 在我开始这篇文章之前,我得说这不是一个建议或者是新的"练习指南".这只是 ...
- Latex 编译错误: ! pdfTeX error (ext4): \pdfendlink ended up in different nesting level than \pd fstartlink. 解决方法
最近写 AAAI 的文章,下载了其模板,但是蛋疼的是,总是提示错误,加上参考文献总是出错: 如下: ! pdfTeX error (ext4): \pdfendlink ended up in dif ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
随机推荐
- 云主机CentOS 7新环境命令行搭建node工程步骤
1.用Node官网提供的命令安装node https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-d ...
- 【25.23%】【codeforces 731C】Socks
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- poj 3090 Visible Lattice Points 法利系列||通过计
因为图像关于对角线对称.所以我们仅仅看下三角区域. 将x轴看做分母,被圈的点看成分子 依次是{1/2},{1/3,1/2},{1/4,3/4},{1/5,2/5,3/5,4/5} 写成前缀和的形式就是 ...
- 利用伪元素before实现自定义checkbox样式
原理:label标签的for属性可以将文字与复选框进行关联,点击文字可以同时将复选框选中. 隐藏checkbox标签,利用label标签的before伪元素来实现自定义样式.设置before伪元素的 ...
- Redis中的管道(PipeLine)与事物(Transactions)
Redis中的管道(PipeLine)与事物(Transactions) 序言 Redis中的管道(PipeLine)特性:简述一下就是,Redis如何从客户端一次发送多个命令,服务端到客户端如何一次 ...
- Bean行为破坏之前,
而在自定义初始化阶段的行为之一似.Spring此外,它提供了两种方法来定制Bean具体的行为破坏之前. 例如下列: 1.采用destroy-method属性. 2.达到DisposableBean介面 ...
- cordova打包之android应用签名
原文:cordova打包之android应用签名 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/mate_ge/article/details/78 ...
- WPF的逻辑树与视觉树(1)基本概念
原文:WPF的逻辑树与视觉树(1)基本概念 一.摘要 逻辑树与视觉树属于WPF的基本概念,学过WPF或者Silverlight的朋友一定会对其有所耳闻,这篇文章将来探讨逻辑树与视觉树的特质以及 ...
- Android官方教程翻译(4)——启动另一个Activity
Starting Another Activity 启动另一个Activity PREVIOUSNEXT THIS LESSON TEACHES YOU TO 这节课教你 1. Respond t ...
- uwp - 上滑隐藏导航栏下滑显示
原文:uwp - 上滑隐藏导航栏下滑显示 好久没写博客了,因为忙着工作.昨天周末填坑需要做一个上滑列表数据时隐藏导航栏下滑时显示的效果,下面分享一下我的做法,希望能给你带来帮助. 思路是通过判断滚动条 ...