/* improvement on dealing  with overflow accroding to this:
* https://discuss.leetcode.com/topic/57452/c-solution-beats-100
*
* be careful about the INT_MAX and INT_MIN
* this atoi is not thinking about special char like dot and so on.
*/ class Solution {
public:
int myAtoi(string str) {
int i,nega_sign=;
long long int re=; //long long re to avoid overflow
for(i=;i<str.size();i++) //skip to all the blank in the front of the string
{
if(str[i]!=' ')break;
}
if(str[i]=='+')i++; // deal with signature
else if(str[i]=='-'){ nega_sign=-;i++;}
while(i<str.size()&&isdigit(str[i]))
/*when the string is over or the current char is not a valid integral number,no more conversion will be performed.
*/
{
re=re*+str[i]-'';
if(nega_sign==-&& -*re<=INT_MIN)return INT_MIN; //overflow
else if (nega_sign==&&re>=INT_MAX)return INT_MAX;
i++;
}
return re*nega_sign;
}
};

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

  1. 【leetcode】String to Integer (atoi)

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

  2. No.008 String to Integer (atoi)

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

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

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

  4. 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 ...

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

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

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

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

  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 ...

随机推荐

  1. ADO.NET(完整修改和查询、实体类,数据访问类)

    一.完整修改和查询 在编写c#语句时需考虑到用户体验,例如在编写修改语句时,需要考虑到输入的内容在数据库中是否能够找到. 中间变量运用. 1.先查 2.执行操作 完整修改语句: bool has = ...

  2. java学习日志(1):命令行and小程序

    1.dos命令行,常见的命令 dir:列出当前目录下的文件以及文件夹md:创建目录rd:删除目录(必须空)cd:进入指定目录cd.. :退回到上一级目录cd/:退回到根目录del:删除文件exit:退 ...

  3. android 中怎么保存当前按钮的状态?就是退出后重新进入还是上一次离开的状态

    比如当前Activity中有一个按钮目前是开启,点击后按钮的text变成关闭!然后退出该Activtity,然后重新打开该Activity后当前按钮的状态还是关闭呢? 就是设置一个状态flag.fla ...

  4. Java设计模式—生产者消费者模式(阻塞队列实现)

    生产者消费者模式是并发.多线程编程中经典的设计模式,生产者和消费者通过分离的执行工作解耦,简化了开发模式,生产者和消费者可以以不同的速度生产和消费数据.这篇文章我们来看看什么是生产者消费者模式,这个问 ...

  5. Android之Service

    1.自定义Service类 package com.example.mars_2000_service; import android.app.Service; import android.cont ...

  6. Eclipse中对Python代码自动格式化!

    在Eclipse中使用PyDev发现无法进行代码格式化,使用通常的"Ctrl+Shift+F"快捷键,没有任何变化,难道不能对python代码格式化了吗? 通过下面设置,就可以每次 ...

  7. 《一课经济学》书摘笔记IV

    有出口,才有钱进口:没有进口,就没有机会出口,因为外国人没有美元可以用来买美国的产品.要想扩大出口,就必须有更多的进口,否则收不到货款.要想压缩进口,出口也会随之被压缩.所以,当我们决定减少进口的时候 ...

  8. delphi Pointer 转成string

    var  s: string;  p: pointer;  s := PChar(p);前提p指向的字符串要以#0结尾.

  9. [SQL]SQL中把一个字段的数据分多行显示

    其实你完全可以写个自定义函数就OK了 . =================================================================== create func ...

  10. Eclipse 常用设置

    1. eclipse中的汉字横着显示怎么解决 同一种字体有两种显示方式,比如Fixedsys Excelsior 3.01和@Fixedsys Excelsior 3.01,前一种汉字是竖着显示,后一 ...