Task description

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).
 
Solution

 
Programming language used: Java
Total time used: 23 minutes
Effective time used: 23 minutes
Code: 15:25:23 UTC, java, final, score:  100
// 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的更多相关文章

  1. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  2. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  3. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  4. 译文:Nesting Your BEM?

    原文链接:http://csswizardry.com/2016/11/nesting-your-bem/ 在我开始这篇文章之前,我得说这不是一个建议或者是新的"练习指南".这只是 ...

  5. Latex 编译错误: ! pdfTeX error (ext4): \pdfendlink ended up in different nesting level than \pd fstartlink. 解决方法

    最近写 AAAI 的文章,下载了其模板,但是蛋疼的是,总是提示错误,加上参考文献总是出错: 如下: ! pdfTeX error (ext4): \pdfendlink ended up in dif ...

  6. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  7. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  8. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  9. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  10. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

随机推荐

  1. 【38.24%】【codeforces 621E】 Wet Shark and Blocks

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. Python 第三方库的安装

    1. pip 进入命令行,使用 pip install pip install numpy 2. 含有 setup.py 文件的第三方库 切换到 setup.py 所在的目录: python setu ...

  3. C#3 分部方法,简单标记一下

    如果我问你,如果一个普通的类实现了一个接口方法,但是这个类的实例却访问不到这个接口的方法,这种情况你遇到过吗?有时候,你可能在使用分部方法时就会发现这么一个现象. C#3 中出现了 “分部方法” ,工 ...

  4. git记不住用户名和密码

    以前我是用svn的 , 我也是最近才用的git 虽然git 有GUI界面  , 但是我觉得还是不如svn 最开始使用git的时候我们直接clone项目的时候可能会设置全局的账号和密码 , 但是我重装系 ...

  5. webpack之font-awesome

    1.安装font-awesome和font-awesome-loader及依赖 git:https://github.com/shakacode/font-awesome-loader npm ins ...

  6. 使用google自带包实现下拉刷新功能

    android 实现下拉刷新有非常多开源的源代码能够用 比方 :PullToRefreshListView  使用起来也非常方便 如今还能够直接使用google libs以下的 android-sup ...

  7. 图灵机(Turing Machine)

    图灵机,又称图灵计算.图灵计算机,是由数学家阿兰·麦席森·图灵(1912-1954)提出的一种抽象计算模型,即将人们使用纸笔进行数学运算的过程进行抽象,由一个虚拟的机器替代人们进行数学运算. 所谓的图 ...

  8. ASP.NET Core Razor 视图起始页 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core Razor 视图起始页 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Razor 视图起始页 上一章节中我们介绍了布局视图, ...

  9. EPPlus简单使用

    在使用之前需要在项目中添加对EEPULS.dll的引用 1,创建excel 2,创建sheet 3,添加内容 4,修改 5,保存 FileInfo newFile = new FileInfo(fil ...

  10. Python+Django+SAE系列教程10-----Django模板

    在本章中,我们开始模板,在前面的章节,您可能已经注意到,我们回到文本的方式有点特别的示例视图. 那.HTML直接在硬编码 Python 其中代码. 这的确是一个小BT. def current_dat ...