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 ...
随机推荐
- volatile简介
volatile简介 java语言提供了一种稍弱的内存同步机制,即volatile变量.用来确保将变量的更新操作通知到其它线程,保证了新值能立即同步到主内存,以及每次使用前立即从内存刷新.当变量声明为 ...
- In close() at SocketHttpClientConnection in Android
In close() at SocketHttpClientConnection Error In Android. when i tried to acess network data on Mai ...
- PHP 配置文件中open_basedir选项作用
如下是php.ini中的原文说明以及默认配置: ; open_basedir, if set, limits all file operations to the defined directory ...
- android基础(三)ContentProvider
ContentProvider主要用于在不同的应用程序之间实现数据共享,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访问数据的安全性,目前内容提供其实android实现跨 ...
- SQL分类
SQL(Structure Query Language)结构化查询语言,是使用关系型数据库的应用语言. SQL主要可以划分为以下三个类别: DDL(Data Define Language)语句:数 ...
- 2015百度之星1002 查找有序序列(RMQ+主席树模板水过)
题意:求在数列中能找到几个个长度为k 的区间,里面的 k 个数字排完序后是连续的. 思路:枚举范围,判断区间内是否有重复的数字(主席树),没有的话求区间最大-区间最小(RMQ),判断是否等于K,是的话 ...
- 【转载】分享一些Qt学习资源,欢迎下载
资源来源:http://bbs.csdn.net/topics/390358737 经过我一翻整理,把一些我收集到的Qt学习资源分享给大家,主要适合新手,老鸟可以直接忽略我.要说明一下,很多资源都是在 ...
- 转 UML类图几种关系的总结
UML类图几种关系的总结 在UML类图中,常见的有以下几种关系: 泛化(Generalization), 实现(Realization),关联(Association),聚合(Aggregati ...
- Java笔记4-do while循环,break,修饰符,方法的调用
do while循环语法:do{ //循环体}while(条件表达式); 注:它是先执行循环体,后再判断的循环结构. 如:int i = 0;do{ System.out.println(" ...
- GridView 控件中如何绑定 CheckBoxList
需求:设计这样一个页面,在页面上可以选择和展示各省份对应的文明城市? 思路:一次性查询出所需的记录(查询数据库的操作不宜写到 C# 代码的循环语句中),并保存到全局变量中,之后根据条件过滤出需要的.可 ...