题目:

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)的更多相关文章

  1. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  2. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  3. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  4. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

  5. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  6. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  7. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  8. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

  9. LeetCode: String to Integer (atoi) 解题报告

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  10. 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

随机推荐

  1. ajax对象属性withCredentials

    默认情况下,ajax跨源请求不提供凭据(cookie.HTTP认证及客户端SSL证明等).通过将设置ajax的withCredentials属性设置为true,可以指定某个请求应该发送凭据.如果服务器 ...

  2. Android平板电脑开发— — —碎片

    碎片是一种可以嵌入在活动中的UI片段,它能让程序更加合理与充分地使用大屏幕的空间,碎片通常都是在平板电脑开发中才会使用 简单实例 左碎片布局 <?xml version="1.0&qu ...

  3. eclipse新建安卓项目点击finish后窗口无法关闭

    eclipse新建安卓项目点击finish后窗口不会自动关闭,而且工程会有很多报错: 如图,这个页面点击finish一直无法关闭,后来试了试,才发现是因为新建项目的第一个页面的sdk版本的问题: 这里 ...

  4. POJ 1637 Sightseeing tour (混合图欧拉路判定)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6986   Accepted: 2901 ...

  5. HDU 5358 尺取法+枚举

    题意:给一个数列,按如下公式求和. 分析:场上做的时候,傻傻以为是线段树,也没想出题者为啥出log2,就是S(i,j) 的二进制表示的位数.只能说我做题依旧太死板,让求和就按规矩求和,多考虑一下就能发 ...

  6. HDU 4768 (二分区间---涨姿势)

    题意:告诉n组A,B,C,按照A + k * C生成等差数列,问这n组数列中哪个数字出现了奇数次以及出现了几次,题目保证最多只会出现一个这种数字. 分析:读完题并没有思路,后来知道是二分区间,枚举是哪 ...

  7. Unity3D 发布无边框exe

    关于:Unity3D 发布无边框exe,Unity3D Build exe无边框 Unity发布windows版本 总是带着边框,很想给它去掉,笔者在网上查了一番,常见的有3中. 1:通过unity3 ...

  8. php通用安装程序,导入数据文件(.sql)的安装程序

    php通用安装程序,导入数据文件(.sql)的安装程序 该程序只需要1个php文件 和 1个数据文件,很方便调用.install/index.php         程序文件install/mycms ...

  9. android 镜像源

    Android SDK在线更新镜像服务器 中国科学院开源协会镜像站地址: IPV4/IPV6: mirrors.opencas.cn 端口:80 IPV4/IPV6: mirrors.opencas. ...

  10. C# 导出到Excel

    一个DataGrid里有两张表的数据,导出成一张表 protected void btnExcel_Click(object sender, EventArgs e) { InfoExport(); ...