[LeetCode 题解]: String to Interger (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.
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.
提示: 使用string的特性来判断是否越界。 strcmp
1.去掉字符串之前多余的空格
2. 判断正负号
3. 记录数字字符串。
4. 判断是否越界。
利用好:strcmp
class Solution {
public:
int atoi(const char *str) {
const char* Max = "";
const char* Min = "";
char b[];
int number=;
int below=,i=,j=;
while(str[i]==' ')i++;
if(i==strlen(str)) return ;
if(str[i]=='+'||str[i]=='-')
{
if(str[i]=='-') below=;
i++;
}
for(;i<strlen(str);i++)
{
if(str[i]>='' && str[i]<='')
b[j++]=str[i];
else
break;
}
b[j]='\0';
int lb = strlen(b);
if(lb>)
{
if(below) return INT_MIN;
else return INT_MAX;
}
else if(lb==)
{
if(below)
{
if(strcmp(b,Min)>=) return INT_MIN;
else
{
for(j=;j<lb;j++)
number= number* + b[j]-'';
}
}
else
{
if(strcmp(b,Max)>=) return INT_MAX;
else
{
for(j=;j<lb;j++)
number= number* + b[j]-'';
}
}
}
else
{
for(j=;j<lb;j++)
number= number* + b[j]-'';
}
return below>?-number:number;
}
};
分支结构有点多,需要仔细分析分析。
转载请注明出处: http://www.cnblogs.com/double-win/谢谢
[LeetCode 题解]: String to Interger (atoi)的更多相关文章
- LeetCode题解——String to Integer(atoi)
题目: 字符串转换为数字. 解法: 这道题的意思是要考虑到,如果有前置的空字符,则跳过:如果超出数字范围,则返回最大/最小整数:如果碰到第一个不能转换的字符,则返回. 代码: class Soluti ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
- [LeetCode 题解]: Roman to Interger
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a ro ...
- [LeetCode][Python]String to Integer (atoi)
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...
- 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 ...
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- [leetcode] 8. String to Integer (atoi) (Medium)
实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...
- Leetcode 8. String to Integer (atoi)(模拟题,水)
8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...
- LeetCode——8. String to Integer (atoi)
一.题目链接:https://leetcode.com/problems/string-to-integer-atoi/ 二.题目大意: 实现一个和C语言里atoi具有相同功能的函数,即能够把字符串转 ...
随机推荐
- django第一课大概了解
项目目录介绍: -------- manage.py : Django项目里面的工具,通过它可以调用django shell和数据库等. mysite/ 主要是项目配置的信息,通常一次生成后不需要其他 ...
- 【Oracle】安装Oracle 10gR2 For CentOS
Oracle10gR2安装安装环境项目 版本信息 备注操作系统 CentOS5.364bit Oracle数据库 Oracle10.2.0.4 64bit 硬件信 ...
- gridView删除提示框
实现方法: 双击GridView的OnRowDataBound事件: 在后台的GridView1_RowDataBound()方法添加代码,最后代码如下所示: protected void GridV ...
- c++builder 画图 填充
c++builder 画图 填充 void __fastcall TForm2::Button1Click(TObject *Sender) { Canvas->Brush->Color ...
- rtmp聊天相关归总
使用共享对象(SharedObject)来开发时时文字聊天其实是很简单的. SharedObject可以跟踪和广播消息,连接到SharedObject中的其中任何一个客户端改变了SharedObjec ...
- jq 遍历 each方法
1.选择器+遍历 $('div').each(function (i){ i就是索引值 this 表示获取遍历每一个dom对象 }); 2.选择器+遍历 $('div').each(function ...
- Mysql事务及行级锁
事务隔离级别 数据库事务隔离级别,只是针对一个事务能不能读取其它事务的中间结果. Read Uncommitted (读取未提交内容) 在该隔离级别,所有事务都可以看到其他未提交事务的执行结果.本隔离 ...
- Makefile 自动搜索 c 和 cpp 文件, 并生成 .a 静态库文件
最近 又弄linux 下的 .a 静态库编译, 于是想 做个 一劳永逸的Makefile, 经过一番折腾, 最后成功了 只需要 改两个 参数 就可以执行了(MYLIB 和 VPATH), 代码 如下: ...
- Qt5.11参考文档
Qt5.11参考文档: http://www.bim-times.com/qt/Qt-5.11.1/qtdoc/index.html (来源于Qt官网)
- [C++] NEW Advanced Usage
NEW Advanced Usage 将分配的内存限定在特定的一块区域 #include<iostream> #include<new> ); ); }; using name ...