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. ...
随机推荐
- Windows Server 2008 R2 NTP服务器
Server 1.查看服务器信息 w32tm /query /status 2.设置同步地址 w32tm /config /manualpeerlist:time.windows.com /syncf ...
- MySQL安装与基本配置
一.简介 SQL语言 DDL:表.视图.索引.触发器操作等.CREATE/ALTER/DROP语句 DML:数据操作.SELECT/INSERT/UPDATE/DELETE DCL:权限设置.GRAN ...
- win8改win7笔记
内存<=4G,选32位(×86) 内存>=4G,选64位(×64) (非必须) BIOS设置 USB Boot Support Disabled改为Enabled(如 ...
- 堆排序(python实现)
堆排序是利用最大最或最小堆,废话不多说: 先给出几个概念: 二叉树:二叉树是每个节点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树” 完全二叉树:除最后一层外, ...
- Docker--在Docker中运行应用
Docker--在Docker中运行应用 一个交互式的容器 既然在Docker提供的是一个容器,底层支撑着一个基本的操作系统环境,那我们就可以通过Docker进入到容器内部与系统进行交互. 据我理解: ...
- Oracle 获取当前日期及日期格式
http://blog.sina.com.cn/s/blog_6168ee920100l2ye.html Oracle 获取当前日期及日期格式 获取系统日期: SYSDATE() 格式化日期: ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- POJ 2391 Ombrophobic Bovines
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18623 Accepted: 4 ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Redis初识、设计思想与一些学习资源推荐
一.Redis简介 1.什么是Redis Redis 是一个开源的使用ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value 数据库,并提供多种语言的API.从2010 年 ...