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 ...
随机推荐
- Unity欢迎窗口的信息
1.Video Tutorials提供Unity相关的教程,包括用户手册.组件手册以及脚本手册等内容.2.Unity Basics提供Unity的基础知识,例如操作界面.工作流程.发布设置等内容.3. ...
- 开发支付宝支付用DELPHI实现 RSA签名
近来根据业务需求 在ERP中集成了微信支付,支付宝支付,开发支付宝支付时最大的障碍就是RSA签名,找了很多资料,最终用 下了个libeay32.pas 根据网上资料最终解决了问题 function ...
- IDA pro 的Python环境变量设置
推荐使用IDA PRO6.1+Python2.6 安装完毕Python2.6后,添加如下的环境变量: PYTHONHOME=C:\Python26PATH=%PATH%;C:\Python26LIB= ...
- c语言结构体2之变量赋值于字符串
#include <stdio.h> #include <stdlib.h> struct dangdang { ]; ]; ]; int num; int bugnum; ] ...
- 万恶DevExpress
公司需要,开始了DevExpress的学习之旅,说它万恶也只是在不了解它的情况下,熟悉之后能很方便的实现很多想要的功能 这里简单写一下要整理的内容,也就是大纲,以后再慢慢添加 一.控件和组件 date ...
- DOS命令大全--具体解释
在Linux和Windows下都能够用nslookup命令来查询域名的解析结果 DOS命令大全一)MD--建立子文件夹 1.功能:创建新的子文件夹 2.类型:内部命令 3.格式:MD[盘符:][路径名 ...
- swift2.0 UIImagePickerController 拍照 相册 录像
系统 ios9.1 语言swift2.0 在app 里最常用的功能就是多媒体选择,首先我们storyboard 创建一个button 用于触发选择事件 @IBAction func selectIma ...
- LoadRunner测试下载功能点脚本(方法二)
在上一篇<LoadRunner下载功能点脚本(方法一)>中,实现的脚本仅是录制下载功能点的脚本,现在性能需求的场景更改如下: 性能需求:对系统某页面中,点击下载并将下载文件保存到本地电脑的 ...
- Android应用盈利广告平台的嵌入方法详解
一.如何学习Android android开发(这里不提platform和底层驱动)你需要对Java有个良好的基础,一般我们用Eclipse作为开发工具.对于过多的具体知识详细介绍我这里不展开,我只 ...
- windows系统还原
windows系统还原 windows 系统还原有两种方法: 方法一.开始-控制面板-系统和安全-备份和还原 (或者开始—所有程序—附件—系统工具—系统还原) 详细请看下面的截图说明 方法二.开机的时 ...