LeetCode_String to Integer (atoi)
Implement atoi to convert a string to an integer.
int atoi (const char * str);
Convert string to integer
Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int. The function first discards as many whitespace characters (as in isspace) 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 base- 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 and zero is returned.
class Solution {
public:
int atoi(const char *str) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = strlen(str) ;
int i =;
long long result = ;
int flag = , b= ;
int numjia = ;
int numjian = ;
while(i<len && str[i] == ' ') i++;
for(; i< len ; i++)
{
switch(str[i])
{
case '+': numjia++;break;
case '-': numjian++; flag =-;break;
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '':
case '': result+=str[i] - '';result*=;break;
default: b = ;break;
}
if(b==) break;
if(numjia> ||numjian>)
return ;
}
result = result/;
result *= flag;
if(result > INT_MAX) return INT_MAX;
if(result < INT_MIN) return INT_MIN;
return result;
}
};
重写后:
class Solution {
public:
int atoi(const char *str) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(str == NULL ) return ;
int len = strlen(str);
long long res = ;
int cur = , flag = ;
while(cur<len && str[cur] == ' ') ++cur;
if( str[cur] == '-' || str[cur] == '+'){
if(str[cur] == '-')
flag = -;
cur++;
}
while(cur < len){
int c = str[cur] - '';
if(c< || c > )
break;
res = c + res*;
++cur;
}
res *= flag ;
if(res > INT_MAX)
return INT_MAX;
if(res < INT_MIN)
return INT_MIN;
return res;
}
};
LeetCode_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 ...
随机推荐
- Linux企业级项目实践之网络爬虫(11)——处理http请求头
http请求头,HTTP客户程序(例如浏览器),向服务器发送请求的时候必须指明请求类型(一般是GET或者POST).如有必要,客户程序还可以选择发送其他的请求头.HTTP客户程序(例如浏览器),向服务 ...
- 什么时候该使用NoSQL存储数据库?
原文地址:http://www.jdon.com/39240 文章总结以下几点:1.频繁写,很少读统计数据,比如点击率,应该使用基于内存的in-memory的key/value存储数据库如Redis, ...
- 哲学家用餐问题的几个解法(c语言实现)
参考资料: 1.维基百科:哲学家用餐问题 2.Windows的多线程编程
- 传阿里整合资源,进军O2O市场
阿里巴巴对于本地生活市场,以及O2O领域始终虎视眈眈.从最早的融合口碑网,到最近阶段推出淘宝点点.收购高德地图等一系列app产品,其整合线上线下消费市场的野心已十分明显. 今年年初,阿里巴巴集团重新进 ...
- python 标准库基础学习之开发工具部分1学习
#2个标准库模块放一起学习,这样减少占用地方和空间#标准库之compileall字节编译源文件import compileall,re,sys#作用是查找到python文件,并把它们编译成字节码表示, ...
- Kali for Android
使用设备:小米平板1 材料:Complete Linux Installer(好像是这个名字,很多应用商店都能下载,所以我就不用送上链接了) 进入app之后,按照着提示做就是了.关于镜像,也是在app ...
- Javascript:一款简易的图片切换插件
最近迷上javascript,每天不写点什么都不舒服哈~ 尽管自己能力有限,还是尽自己所能写点东西出来. 实现效果: 效果预览:http://codepen.io/anon/pen/BNjxXj 该插 ...
- ASP.NET 实现PDF文件下载
本文介绍了一种在ASP.NET中下载文件的方法. 方法一:可能是最简单的.最短的方式: Response.ContentType = "application/pdf"; Resp ...
- asp.net页面之间的跳转
调用Request.CurrentExecutionFilePath方法返回到当前页面 站点中常常要跳转页面,调用Request.CurrentExecutionFilePath方法能够获取当前页面的 ...
- 实现简单的django上传文件
本文用django实现上传文件并保存到指定路径下,没有使用forms和models,步骤如下: 1.在模板中使用form表单,因为这个表单使用于上传文件的,所以method属性必须设置为post,而且 ...