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 ...
随机推荐
- UART与USART的区别
UART与USART都是单片机上的串口通信,他们之间的区别如下: 首先从名字上看: UART:universal asynchronous receiver and transmitter通用异步收/ ...
- KEIl混合编程步骤详解
一.在keil中C函数调用汇编函数: 主要思路:先用C来编写所要实现及调用的汇编函数,然后由此C函数生成相应的汇编代码,这样我们就可以不用去管混合编程调用时复杂的函数接口,我们只要修改相应汇编函数中的 ...
- bzoj 1196
http://www.lydsy.com/JudgeOnline/problem.php?id=1196 二分+并查集 一共有2*M条路径,我们首先将这2*M条路径按费用排序. 然后二分最大费用的公路 ...
- VS2013中C++创建DLL导出class类
1.创建"Win32 Console Application"项目,命名为"ClassDllLib",并在"Application type" ...
- hdu1028:整数拆分
求整数的拆分数.. 一种解法是母函数 #include <iostream> #include <stdio.h> #include<string.h> #incl ...
- Ext中窗体第二次点击报错或者其内控件不显示的问题,弄了2天才解决,记录下
registerPanel.js: registerPanel = new Ext.form.FormPanel({ id:'registerPanel', layout:'form', autoHe ...
- 7 个改变世界的 Java 项目
英文链接:http://radar.oreilly.com/2011/07/7-java-projects.html 文章转载自:开源中国社区 http://www.oschina.net/news/ ...
- 分页标签:pager-taglib使用指南
一简介, Pager-taglib,支持多种风格的分页显示.实际上她是一个Jsp标签库,为在JSP上显示分页信息而设计的一套标签,通过这些标签的不同的组合,会形成多种不一样的分页页面,风格各异,她自带 ...
- QListWidget方式显示缩略图
最近在工作中经常遇到了一个问题就是把把文件夹中的图片全部以缩略图的形式显示出来,刚开始的时候一头雾水,不知道怎么办,经过在网上查资料,发现QListWidget控件可以实现图片的缩略图显示,但是不知道 ...
- StroyBoard中UICollectionView中添加Header和footer
到Storyboard中,选择collection view controller中的"Collection View".在Attributes inspector中,选择&quo ...