题目:

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. 监听器初始化Job、JobTracker相应TaskTracker心跳、调度器分配task源码级分析

    JobTracker和TaskTracker分别启动之后(JobTracker启动流程源码级分析,TaskTracker启动过程源码级分析),taskTracker会通过心跳与JobTracker通信 ...

  2. sql数据库 管理处理问题--维护计划

    问题:SQLServer 错误: 15404,无法获取有关 Windows NT 组/用户 MYPC/Administrator' 的信息,错误代码 0x534. [SQLSTATE 42000] ( ...

  3. html a 链接标签title属性换行鼠标悬停提示内容的换行效果

    鼠标经过悬停于对象时提示内容(title属性内容)换行排版方法,html title 换行方法总结. html的title属性默认是显示一行的.如何换行呢? 这里DIVCSS5总结介绍两种换行方法为大 ...

  4. autotools工具使用记录

    参考 http://blog.chinaunix.net/uid-25100840-id-271131.html http://blog.sina.com.cn/s/blog_4c2bf01a0101 ...

  5. xcode代码提示功能失效的解决方法

    xcode 自动提示很好用 然而大量的工作也是让他吃不消了 结果今天提示功能给我来了个罢工 这当然是不行的 也是万能的搜索帮我解决了这个问题 方法很多 选择了简单的 xcode --> Wind ...

  6. WCF 发布使用

    WCF发布,由于使用的是 net.tcp协议因此 需要在发布的WCF站点的管理网站-高级设置,连接协议中添加net.tcp的绑定 然后还需要在网站绑定编辑中添加net.tcp的绑定.否则访问的时候会出 ...

  7. 详解Android功耗分析工具Power Tutor的使用

    简介: PowerTutor 是由美国密歇根大学在谷歌的指导下开发的.它是用来展示google智能手机中主要组件或应用功耗的一种应用程序.例如,CPU,网络链接,LCD显示屏,GPS等.它允许开发者很 ...

  8. 使用PHP处理文本小技巧

    PHP的Cli模式使用:http://www.php.net/manual/zh/features.commandline.php PHP命令行部分参数:-B     在处理 stdin 之前先执行 ...

  9. 遇到tomcat端口被占用问题解决方案

    1) 启动Eclipse的Tomcat5.0时,报以下错误: 2)根据以上提示显示:Tomcat Server 的8080端口已经被占用.查看它被哪个占用,方法如下: 3)可以看到占用此端口的PID为 ...

  10. Python 基礎 - pyc 是什麼

    Python2.7 版中,只要執行 .py 的檔案後,即會馬上產生一個 .pyc 的檔案,而在 Python3 版中,執行 .py 的檔案後,即會產生一個叫 __pycache__ 的目錄,裡面也會有 ...