A string S consisting of N characters is considered to be properly nested if any of the following conditions is true:

  • S is empty;
  • S has the form "(U)" or "[U]" or "{U}" where U is a properly nested string;
  • S has the form "VW" where V and W are properly nested strings.

For example, the string "{[()()]}" is properly nested but "([)()]" is not.

Write a function:

int solution(char *S);

that, given a string S consisting of N characters, returns 1 if 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..200,000];
  • string S consists only of the following characters: "(", "{", "[", "]", "}" and/or ")".

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(N) (not counting the storage required for input arguments).

Copyright 2009–2015 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

1. 需要注意的细节:

a.每次peek后,需要检查时候为NULL,如果缺少这一步检查而直接进行 myNode->x 操作,会产生段错误。

b.注意str的对称性问题,并不是每一次循环正常结束都是正确的,有可能存在右括号少的情况,需要判断如果stack不为空,则返回false;

c.要注意temp必须每一次都++,否则循环不能正常进行。

2.代码:

 // you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
#include <stdlib.h> typedef struct node{
char x;
struct node* next;
}tnode; typedef struct stack{
tnode *top;
tnode *bottom;
}tStack; void push(tnode *N,tStack *S)
{
if(S->top == NULL)
{
S->top = N;
S->bottom = N;
}
else
{
N->next = S->top;
S->top = N;
}
} tnode *peek(tStack *S)
{
if(S->top == NULL)
{
return NULL;
}
else
{
return S->top;
}
} tnode *pop(tStack *S)
{
if(S->top == NULL)
{
return NULL;
}
if(S->top == S->bottom)
{
tnode *temp = S->top;
S->top = NULL;
S->bottom = NULL;
return temp;
}
else
{
tnode *temp = S->top;
S->top = S->top->next;
return temp;
}
} int solution(char *S) {
// write your code in C99 tStack *myStack = malloc(sizeof(tStack));
myStack->top = NULL;
myStack->bottom = NULL; // int len = strlen(S);
char *temp = S;
tnode *myNode = NULL;
// int i;
while(*temp)
{
// printf("%c\n",*temp);
if(*temp == '(' || *temp == '[' ||*temp == '{')
{
myNode = malloc(sizeof(tnode));
myNode->x = *temp;
push(myNode,myStack);
}
else
{
myNode = peek(myStack);
if(myNode == NULL)
{
return ;
}
if(*temp == ')')
{
if(myNode->x == '(')
{
myNode = pop(myStack);
}
else
{
return ;
}
}
if(*temp == ']')
{
if(myNode->x == '[')
{
myNode = pop(myStack);
}
else
{
return ;
}
}
if(*temp == '}')
{
if(myNode->x == '{')
{
myNode = pop(myStack);
}
else
{
return ;
}
}
} temp++;
}
myNode = peek(myStack);
if(myNode == NULL)
{
return ;
}
else
{
return ;
} }

string stack操作要注重细节问题的更多相关文章

  1. Scala 深入浅出实战经典 第39讲:ListBuffer、ArrayBuffer、Queue、Stack操作代码实战

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  2. 还原Stack操作

    下午看到一题.给定两个int[]数组,int[] org和int[] res, 分别代表一串数字,和这串数字经过stack的push 和 pop操作之后的数字,让返回一个String, String里 ...

  3. redis 的使用 (基础, key操作, string类型操作)

    使用redis set 类型: 没有重复元素 list 链表类型 有重复累型 sort set 类型 没有重复元素 1.1 存储数据 读取数据 // 数据储存在 内存中 set name laowen ...

  4. Redis系列-存储篇string主要操作函数小结

    通过上两篇的介绍,我们的redis服务器基本跑起来.db都具有最基本的CRUD功能,我们沿着这个脉络,开始学习redis丰富的数据结构之旅,当然先从最简单且常用的string开始. 1.新增 a)se ...

  5. string的操作

    除了顺序容器共有的操作之外,string类型还提供了一些额外的操作.这些操作中的大部分要么是提供string类和C风格字符数组之间的相互转换,要么是增加了允许我们用下标代替迭代器的版本. 构造stri ...

  6. Library string type(2)——关于String的操作

    关于string的定义,请参阅博文http://blog.csdn.net/larry233/article/details/51483827 string的操作 s.empty() //Return ...

  7. 你所不了解的javascript操作DOM的细节知识点(一)

    你所不了解的javascript操作DOM的细节知识点(一) 一:Node类型 DOM1级定义了一个Node接口,该接口是由DOM中的所有节点类型实现.每个节点都有一个nodeType属性,用于表明节 ...

  8. 022 StringTokenizer替换掉String的操作

    一:说明 1.说明 String的操作特别消耗内存,所以可以考虑优化. 二:程序 1.程序修改 这部分程序属于Mapper端的程序,稍微优化一下. 2.程序 //Mapper public stati ...

  9. Atitit.注重细节还是关注长远??长远优先

    Atitit.注重细节还是关注长远??长远优先 1. 注重细节的误区 1 1.1. 如果连aaa都做不好,那么怎么能够相信你ccc 2 1.2. 一屋不扫何以扫天下??但是扫大街的都是保洁员 2 2. ...

随机推荐

  1. Linux 查看命令源码

    一.简介 有时候想看看ls.cat.more等命令的源代码,本文介绍相应查看方法. 二.方法 参考: http://blog.csdn.net/silentpebble/article/details ...

  2. 【码在江湖】前端少侠的json故事(上)日月第一击

    日月第一击 这是我前端生涯第一次和后台对接,其经历真是苦不堪言,多次绝处逢生,柳暗花明,可就是迟迟见不到那条村子.当然,最后我还是完成了这次对接.下面来聊一聊我这白痴一般的经历. 序章 话说天下大势, ...

  3. Windows下虚拟机安装Ubuntu15.10 Destop简易操作过程

    一.前提环境: 1.vmware12.1,若您的系统是32位,请使用vmware10以下版本. 2.至少双核处理器,2G以上可用内存. 3.Ubuntu安装包(.iso后缀). 注:请尽量支持正版. ...

  4. HTML 学习笔记 JavaScript(call方法详解)

    http://www.cnblogs.com/f-dream/p/4950918.html

  5. DeveloperExceptionPageMiddleware中间件如何呈现“开发者异常页面”

    DeveloperExceptionPageMiddleware中间件如何呈现"开发者异常页面" 在<ASP.NET Core应用的错误处理[1]:三种呈现错误页面的方式&g ...

  6. jquery-leonaScroll-1.3-自定义竖向自适应滚动条插件

    下载链接地址:https://share.weiyun.com/9ac3ca3fb29648bb1aad1b83a76b123c (密码:4y9t)[含mini版] 欢迎使用leonaScroll-1 ...

  7. uicode编码解码

    .版本 2.支持库 dp1 bydess = 字节集_还原 (到文本 (bytes)) ' HEX解码返回 (到文本 (解密数据 (bydess, “debugme?”, #RC4算法))) impo ...

  8. 用 ElementTree 在 Python 中解析 XML

    用 ElementTree 在 Python 中解析 XML 原文: http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python- ...

  9. Android环境变量配置

    第一步: 把这些东西全部准备好!然后jdk怎么安装我相信大家都知道.安装好jdk之后,我们来配置环境变量. 我的电脑—右键—属性—高级系统设置—环境变量 JAVA_HOME环境变量.它指向jdk的安装 ...

  10. SmartAssembly使用失败记录

    目标:将指定程序集混淆,禁止反编译 1.下载软件 www.red-gate.com 官网下载 因为不常用,所以用的14天试用版 2.前面的照着下面的教程,一直到第四步 http://www.cnblo ...