string stack操作要注重细节问题
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操作要注重细节问题的更多相关文章
- Scala 深入浅出实战经典 第39讲:ListBuffer、ArrayBuffer、Queue、Stack操作代码实战
王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...
- 还原Stack操作
下午看到一题.给定两个int[]数组,int[] org和int[] res, 分别代表一串数字,和这串数字经过stack的push 和 pop操作之后的数字,让返回一个String, String里 ...
- redis 的使用 (基础, key操作, string类型操作)
使用redis set 类型: 没有重复元素 list 链表类型 有重复累型 sort set 类型 没有重复元素 1.1 存储数据 读取数据 // 数据储存在 内存中 set name laowen ...
- Redis系列-存储篇string主要操作函数小结
通过上两篇的介绍,我们的redis服务器基本跑起来.db都具有最基本的CRUD功能,我们沿着这个脉络,开始学习redis丰富的数据结构之旅,当然先从最简单且常用的string开始. 1.新增 a)se ...
- string的操作
除了顺序容器共有的操作之外,string类型还提供了一些额外的操作.这些操作中的大部分要么是提供string类和C风格字符数组之间的相互转换,要么是增加了允许我们用下标代替迭代器的版本. 构造stri ...
- Library string type(2)——关于String的操作
关于string的定义,请参阅博文http://blog.csdn.net/larry233/article/details/51483827 string的操作 s.empty() //Return ...
- 你所不了解的javascript操作DOM的细节知识点(一)
你所不了解的javascript操作DOM的细节知识点(一) 一:Node类型 DOM1级定义了一个Node接口,该接口是由DOM中的所有节点类型实现.每个节点都有一个nodeType属性,用于表明节 ...
- 022 StringTokenizer替换掉String的操作
一:说明 1.说明 String的操作特别消耗内存,所以可以考虑优化. 二:程序 1.程序修改 这部分程序属于Mapper端的程序,稍微优化一下. 2.程序 //Mapper public stati ...
- Atitit.注重细节还是关注长远??长远优先
Atitit.注重细节还是关注长远??长远优先 1. 注重细节的误区 1 1.1. 如果连aaa都做不好,那么怎么能够相信你ccc 2 1.2. 一屋不扫何以扫天下??但是扫大街的都是保洁员 2 2. ...
随机推荐
- My first win32 application program
#include<afxwin.h>#include<afx.h>#define _AFXDLLclass CHelloApp :public CWinApp{public: ...
- 轻量级C#编辑器RoslynPad
简介 RoslynPad是一个Apache 2.0协议开源的轻量级C#编辑器.支持自动完成,语法提示,修改建议等功能.很适合平时随手写个C#程序看看运行结果. 目前版本:0.10.1,无需保存也可以运 ...
- [LeetCode] Smallest Rectangle Enclosing Black Pixels 包含黑像素的最小矩阵
An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...
- c#与JavaScript实现对用户名、密码进行RSA非对称加密
博主最近手上这个项目呢(就是有上百个万恶的复杂excel需要解析的那个项目,参见博客:http://www.cnblogs.com/csqb-511612371/p/4885930.html),由于是 ...
- C /C++ 语言练习册
/************************************** 整数对应 32 bit 二进制数串中数字1的个数 2016-10-24 liukun ***************** ...
- lumia手机wp系统应用列表如何设置按照拼音
1.安装应用多了就会这样·· 2.想用拼音排列,请把系统设置里的区域语言中的区域和格式改为中国,此时,屏幕壁纸上是"四月十五日". 3.想用笔画排列,请把系统设置里的区域语言中的区 ...
- iOS 设置不同的字体颜色
//设置不同字体颜色 -(void)fuwenbenLabel:(UILabel *)labell FontNumber:(UIFont *)font AndRange:(NSRange)range ...
- bzoj1251
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3776 Solved: 1581[Submit][Status][Discu ...
- Atomikos的使用过程中要注意的事
在使用Atomikos过程中遇到的一些问题,以作记录: MySQL does not support TMJOIN MySQL does not allow for joining an existi ...
- ApacheCommons的Java公共类库(实现如Log这些功能)
Apache Commons是Apache软件基金会的项目,曾隶属于Jakarta项目.Commons的目的是提供可重用的.开源的Java代码. 解释:http://baike.baidu.com/i ...