#define INT_MAX 2147483647
#define INT_MIN -2147483648
class Solution {
public:
int atoi(const char *str) {
//string如果能转换成int的话,各位都要是数字,一个例外是第一个字符可以是负号
int n=strlen(str);
int i=0;
int flag=0;//0表示正值,1表示负值
int val=0;
/*
if(str[0]=='-')
{
i=1;
flag=1;
}
else if(str[0]=='+')//1、好吧,我承认刚开始我只记得负号要单独判断,忘记了正号了,~~~~(>_<)~~~~
{
i=1;
}
//2、好吧,我服了,还有空格,我也都没有考虑,空格可能出现的位置:开头,中间,结尾
//3、我彻底服了,空格在开头和结尾还不能够一视同仁呀,因为" -123"~~~~(>_<)~~~~
*/
//这个是专门处理开头空格滴
/*
while(i<n)
{
if(str[i]==' ')
++i;
else if(str[i]=='-')
{
++i;
flag=1;
break;
}
else if(str[i]=='+')
{
++i;
break;
}
}
*///一直显示超时,调试才发现这里是个死循环,晕
while(i<n)
{
if(str[i]==' ')
++i;
else if(str[i]=='-')
{
++i;
flag=1;
break;
}
else if(str[i]=='+')
{
++i;
break;
}
else
break;
}
while(i<n)
{
//if(str[i]==' ') //其实字符串中间有空格的话,算是能转为int呀还是不能转为int呀,纠结,我的写法是算是能转了
//++i;//如果用for时,因为后面已经++i了,所以这里应该是空操作,不能再写++i了
//Input:" -0012a42" Output:0 Expected:-12 测试用例的意思是,把能返回的第一串都给返回了
//if(str[i]<'9'&&str[i]>'0') //写错了吧
if(str[i]<='9'&&str[i]>='0')
{
//val=val*10+str[i];
val=val*10+(str[i]-'0');
++i;//这里用的是while,所以这里要手动++i;
}
else
{
//return 0;//如果字符串不能转换为int的话,返回什么呀,这个很纠结呀,提交的错误用例说明返回值应该是0
break;
}
}
if(flag)
val=-val;
//Input: "2147483648" Expected: 2147483647
// If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
if(flag==0&&val<)//判断是否溢出
return INT_MAX;
else if(flag==1&&val>0)
return INT_MIN;
else
return val;
}
};

刚开始我没有看提示,直接做的,然后就各种错呀

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.  

Submission Result: Wrong Answer

Input: " 10522545459"
Output: 1932610867
Expected: 2147483647

说明我的溢出判断那个地方写的不对,对于这道题目来说,怎么判断溢出,什么时候判断溢出,我觉得如果在最后判断溢出是不好的,因为输入的是字符串,不能知道它的int值是多少就不能和之后出来的值比较,所以判断溢出应该是在转换的过程中判断的,至于具体怎么做,我想不到,看看别人是怎么写的吧:http://blog.csdn.net/ithomer/article/details/8800530

 class Solution {
public:
int atoi(const char *str) {
int i=;
int n=strlen(str);
int flag=;
int value=;
int digit=;
while(*str!='\0')
{
if(*str==' ')
{
str++;
}
else if(*str=='-')
{
flag=;
str++;
break;
}
else if(*str=='+')
{
str++;
break;
}
else
{
break;
}
}
while(*str!='\0')
{
if(*str<=''&&*str>='')
{
digit=*str-'';
//要满INT_MIN<=val*10+digit<=INT_MAX
if(flag==&&value>(INT_MAX-digit)/)
return INT_MAX;
//else if(value<(INT_MIN-digit)/10)//负值不能这么算
else if(flag==&&value>(-INT_MIN-digit)/)
return INT_MIN;
else
{
value=value*+digit;
str++;
}
}
else
{
break;
}
}
if(flag)
value=-value;
return value;
}
};

Submission Result: Wrong Answer

Input: " -00134"
Output: -2147483648
Expected: -134

不明真相,我在codeblocks中跑了代码,输入" -00134",输出的就是-134,不知道到底是怎么回事

String to Integer (atoi) ???的更多相关文章

  1. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  2. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  3. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  4. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  5. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

  6. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  7. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  8. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

  9. LeetCode: String to Integer (atoi) 解题报告

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  10. 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

随机推荐

  1. spring mvc:拦截器不拦截静态资源的三种处理方式

    方案一.拦截器中增加针对静态资源不进行过滤(涉及spring-mvc.xml) <mvc:resources location="/" mapping="/**/* ...

  2. SQLite在C#中的使用

    SQLite是一款轻型的数据库,在一些数据量不太大的程序中,它暂用的资源非常低.支持很多操作系统和许多语言,所以还是很方便的.在C#中,要用的话可以通过网站来下载或者在VS中通过NuGet来下载.这个 ...

  3. java 下载文件的两种方式和java文件的上传

    一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...

  4. webpack相关文章

    0.https://doc.webpack-china.org/concepts/ (webpack中文文档) 1.https://segmentfault.com/a/119000000617877 ...

  5. 25-THREE.JS 绘制线框样式几何图形的材质 线材质

    <!DOCTYPE html> <html> <head> <title></title> <script src="htt ...

  6. Flask 分页的简单用法 / flask_sqlalchemy /无刷新翻转页面(原创)

    flask_sqlalchemy对象提供分页方法 1. 后台views代码: from models import <table_name> #导入model的对象 @app.route( ...

  7. WinForm与Javascript交互

    在应用程序的集成过程中,有时候需要WinForm应用程序和Javascript程序进行交互.比如说:应用程序是一个综合调度系统,在整个综合调度系统中,要实现定位,显示地图.综合调度平台的大部分功能都是 ...

  8. iOS开发之谈谈App应用的架构搭建(推荐给大家看)

    1.iOS应用架构谈 开篇: 2.iOS应用架构谈 view层的组织和调用方案: 3.iOS应用架构谈 网络层设计方案: 4.iOS应用架构谈 本地持久化方案及动态部署: 5.iOS应用架构谈 组件化 ...

  9. OSPF 配置

    封装在IP层:协议号 89 hello时间是dead时间的1/4 224.0.0.5 .在点到点网络, 比如T1线路,是连接单独的一对路由器的网络, 点到点网络上的有效邻居总是可以形成邻接关系的,在这 ...

  10. 51nod 2006 飞行员配对

    第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...