《leetCode》:Convert String to Integer
来源:互联网 发布:mac 菜单栏的锁不见了 编辑:程序博客网 时间:2024/06/05 16:46
题目描述
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want achallenge, please do not see below and ask yourself what are thepossible input cases.Notes: It is intended for this problem to be specified vaguely (ie, nogiven input specs). You are responsible to gather all the inputrequirements up front.
题目大意:将字符串转化为整数形式。
/*可能的测试用例如下:1)str="123"2)str="0123"3)str="+123"4)str="-123"5)str="+0123"6)str="-0123"7)特殊输入:NULL 没有考虑到的测试用例1)str=" 010";或str="010 ";含空格的情况。处理方法:将字符串的前后空格去除了 还没有考虑的测试用例1)str=" -012a34";输出-12 还没有考虑到的测试用例1)str="2147483647",正整数中最大值2)str="- 2147483647",最大负整数还没有考虑到的测试用例1) str="9223372036854775809"*/#include<stdio.h>#include<stdlib.h>#include<string.h> #include<math.h>void trimStr(char *str,int *begin,int *end) { *begin=0; *end=0; if(str==NULL||str==""){ return; } int index=0; while(str[index]!='\0'){ if(str[index]!=' '){ break; } index++; } *begin=index; //先判断第一个非数字字符是否为 +/-号 if(str[index]=='+'||str[index]=='-'){ index++; } while(str[index]!='\0'){ if(str[index]==' '){ break; } int temp=str[index]-'0'; if(temp<0||temp>9){ break; } index++; } *end=index;}int myAtoi(char* str) { if(str==NULL||str==""){ return 0; } int begin=0;//用来指示第一个不是空格的索引; int end=0;//用来表示最后一个不是空格后一位的索引 trimStr(str,&begin,&end); //先判断下str的第一个字符是否为“+”或者是“-”号 int symbol=1; int start=begin; if(str[begin]=='+'){ symbol=1; start=begin+1; } if(str[begin]=='-'){ symbol=-1; start=begin+1; } int pow2Val=pow(2,31); if(start==end){//只含有加减号的字符串。 return 0; } if(end-start>11){ if(symbol==1) return pow2Val; else{ return (-1)*pow2Val-1; } } long long result=0;//用来保存结果 ,也要考虑是否溢出,因此设为long型 for(int i=start;i<end;i++){ int temp=str[i]-'0'; if(temp<0||temp>9){//检测每个字符是否在 '0'到'9'之间 return 0; } result=result*10+temp; } if(result>=pow2Val&&symbol==1){//正整数溢出了 ,返回最大正整数值 return pow2Val; } else if(result>pow2Val&&symbol==-1){//负整数溢出了 ,返回小负整数值 return (-1)*pow2Val-1; } else{ return (int)(symbol*result); } }int main(void){// printf("%d",sizeof(long long)); //char str[1000]; //while(gets(str)){ char * str="9223372036854775809";//2^31-1,即int类型最大的正整数 int result=myAtoi(str); printf("%d\n",result);// }}
经过千辛万苦,终于AC了
程序的思想很简单,但是这个题目背后的测试用例比较变态。
例如
str=”9223372036854775809”,返回正整数的最大值
str=”最大正整数“+1,返回正整数的最大值。
本来刚开始写的代码是统一将溢出的,返回零值,但是别人测试平台有这个要求,我们也没有办法。
遇到的一些问题截图如下:
1)没有考虑空格
解决方法:写了一个找到字符串第一个不是空格的索引begin和最后一个字符再后一个位置的索引end。
2)没有考虑需要有这个测试用例
解决方法:在第一个问题的基础上,将获取最后一个字符再后一个位置的索引end。—–》修改为获取第一个字符不是”0“~”9“的索引
3)下面几种就是几种溢出的情况,测试平台要求输出最大正值或负值
解决方法:加几个if判断下然后输出即可。
小结
完成整个题目花的时间也挺长的,测试用例还过于变态。
1 0
- 《leetCode》:Convert String to Integer
- [LeetCode]String to Integer
- 【Leetcode】String to Integer
- [leetcode] string to integer
- leetcode-string to integer
- Leetcode: String to Integer
- [LeetCode]String to Integer
- LeetCode:String to Integer
- LeetCode--String to Integer
- leetcode string to integer
- [leetcode] String to Integer
- LeetCode-String To Integer
- LeetCode String to Integer
- leetcode string to integer
- LeetCode | String to Integer
- leetcode 8 Implement atoi to convert a string to an integer.
- LeetCode:String to Integer (atoi)
- LeetCode: String to Integer (atoi)
- PHP foreach嵌套if else的问题
- iOS中copy 学习笔记
- Cococapods 添加第三方,导入不了头文件
- jquery validate 插件:(1)使用说明
- D-Clustering: A Kernel Based Graph Clustering Algorithm
- 《leetCode》:Convert String to Integer
- hdu 1023 hdu 1131
- JSON 和 List、Map集合结合使用
- 3D数学 4x3矩阵类源代码(附中文注释)
- 菜鸟好文推荐(十九)——提高Java架构师和程序员效率的10个工具
- Excel 宏实现图片按比例缩放
- jquery validate 插件:(2)简单示例
- 菜鸟好文推荐(二十)——14个最佳的HTML/CSS设计和开发框架
- springMVC上传文件