8. String to Integer (atoi)
题目:
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
spoilers alert... click to show requirements for atoi.
Requirements for atoi:The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
解析:注意前后中间(或全是)的空格,注意正负号,注意溢出,(题目要求中没有:注意特殊字符出现)
代码也许还待优化:
- //#define INT_MAX 2147483647
- //#define INT_MIN -2147483648
- double noHeadBlank(const char *str){
- double val = 0;
- if(str[0] == '-' || str[0] == '+'){
- int i = 1;
- while(str[i] == '0') ++i;
- if(str[i] == '\0') return 0;
- else if(str[i] > '0' && str[i] <= '9') {val = str[i] - '0'; ++i;}
- else return 0;
- for(; str[i] != '\0' && str[i] >= '0' && str[i] <= '9'; ++i)
- val = val * 10 + (str[i] - '0');
- if(str[0] == '-') val *= (-1);
- }else if(str[0] > '0' && str[0] <= '9') {
- val = str[0] - '0';
- int i = 1;
- for(; str[i] != '\0' && str[i] >= '0' && str[i] <= '9'; ++i){
- val = val * 10 + (str[i] - '0');
- }
- }else if(str[0] == '0'){
- int i = 1;
- while(str[i] == '0') ++i;
- if(str[i] == '\0') return 0;
- else if(str[i] > '0' && str[i] <= '9') {val = str[i] - '0'; ++i;}
- else return 0;
- for(; str[i] != '\0' && str[i] >= '0' && str[i] <= '9'; ++i)
- val = val * 10 + (str[i] - '0');
- }
- return val;
- }
- class Solution {
- public:
- int atoi(const char *str) {
- double val = 0;
- if(str == NULL) return 0;
- if(str[0] == ' '){
- int i = 1;
- while(str[i] == ' ') ++i;
- if(str[i] == '\0') return 0;
- else val = noHeadBlank(str+i);
- }else val = noHeadBlank(str);
- if(val > (double)INT_MAX) return INT_MAX;
- else if (val < (double)INT_MIN) return INT_MIN;
- else return (int)val;
- }
- };
8. String to Integer (atoi)的更多相关文章
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- ConvertFrom-String 命令研究
-------先上个例子------- $aaa = @'0.027 0.034 0.834 0.1050.346 0.558 0.018 0.0780.001 0.997 0.001 0.0010. ...
- ESXi虚拟磁盘共享
因为项目需要需要一个磁盘共享的环境. 最先想到用iSCSI,两个客户端挂载同一个远端盘:这样确实可行,但是感觉太繁琐,想到既然是虚拟机环境,可以设置虚拟磁盘共享. 于是网上一番搜罗,确实有人这个做过, ...
- web前端基础篇③
1.video视频 audio音频 controls出现控件 loop循环 autoplay自动播放例:<video/audio src=“地址” controls=“controls” loo ...
- 持续集成-sourcetree的安装、使用记录
1.参考 http://blog.sina.com.cn/s/blog_672143a30102vold.html 2.问题-安装sourcetree后,打开时提示下载但是连接不上相应链接 自行下载g ...
- 在MacOX下安装python-opencv
下载好opencv之后 1. 在文件夹下新建一个release或build的文件夹: 2. cmake . make 3.在该build文件夹下 nano .bash_profile 把python的 ...
- cassandra 环境搭建
1 下载安装包 http://www.planetcassandra.org/cassandra/?dlink=http://downloads.datastax.com/community/dsc- ...
- IE6,7 margin-bottom失效bug
问题描述:ie6/7浏览器下,浮动元素贴近父元素的最后一行的元素(单行即指第1行)的margin-bottom值失效! 问题代码: <style type="text/css" ...
- wndows程序设计之书籍知识与代码摘录-获取视屏显示器像素等参数GetsystemMetrics
以下的代码段用于获取视屏显示器的高度宽度,以像素为单位. int sxScreen, cyScreen; cxScreen = GetSystemMetrics (SM_CXSCREEN); cySc ...
- Mongodb在NUMA机器上的优化
10gen在mongodb的部署指南上,提到了在NUMA机器上,mongodb可能会出现问题,参见:http://docs.mongodb.org/manual/administration/prod ...
- [原创] 用两个queue实现stack的功能
#include <iostream> #include <queue> using namespace std; template <class T> class ...