String to Integer (atoi)

Total Accepted: 15482 Total
Submissions: 106043My Submissions

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.

字符串的操作。敲代码常常性遇到。string这个类真的很实用哟,题目要求自行实现atoi的功能:

class Solution
{
public:
int atoi(const char *str)
{
while(' ' == *str)
{
str++;
}
bool isNegative = false;
if('-' == *str)
{
isNegative = true;
str++;
}
else if('+' == *str)
{
str++;
}
long long ret = 0;
while(*str)
{
if( isdigit(*str) )
{
ret = ret * 10 + (*str - '0');
if(isNegative && (-ret <= INT_MIN))
{
return INT_MIN;
}
if(!isNegative && (ret >= INT_MAX))
{
return INT_MAX;
}
}
else
{
break;
}
str++;
}
return (isNegative ? -ret : ret);
}
};

【Leet Code】String to Integer (atoi) ——常考类型题的更多相关文章

  1. LeetCode【8】. String to Integer (atoi) --java实现

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

  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. Kotlin实现LeetCode算法题之String to Integer (atoi)

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

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

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

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

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

随机推荐

  1. POJ 2406 Power Strings 简单KMP模板 strcmp

    http://poj.org/problem?id=2406 只是模板,但是有趣的是一个strcmp的字符串比较函数,学习到了... https://baike.baidu.com/item/strc ...

  2. [BZOJ 4719] 天天爱跑步

    Link: BZOJ 4719 传送门 Solution: 感觉求LCA又有了新姿势啊:$Tarjan$离线$O(n+m)$ 每次递归返回时将子树和父节点合并,如果询问节点已访问过则LCA就是已合并的 ...

  3. 【递推】【概率】Gym - 100814A - Arcade Game

    题意:给你一个不超过九位的不含重复数码的十进制数,每次会随机将它的数码打乱,变成一个新的数,如果它小于等于上一次的数,那么你输了:如果它大于上一次的数,那么可以继续.直到它变成能够表达的最大数为止就赢 ...

  4. bzoj 3238: [Ahoi2013]差异 -- 后缀数组

    3238: [Ahoi2013]差异 Time Limit: 20 Sec  Memory Limit: 512 MB Description Input 一行,一个字符串S Output 一行,一个 ...

  5. 线段树 求区间连乘——hdu 3074 Multiply game

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  6. iOS 画圆

    _demoView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; [self.view addSubview:_de ...

  7. Eclipse打开.class文件

    1.准备材料 jad.exe    下载地址  https://varaneckas.com/jad/,根据系统去选择(就当是废话)  下载下来的是jad158g.win.zip压缩文件,解压后有个j ...

  8. Spring Bean作用域实例

    在Spring中,bean作用域用于确定哪种类型的 bean 实例应该从Spring容器中返回给调用者.bean支持的5种范围域: 单例 - 每个Spring IoC 容器返回一个bean实例 原型- ...

  9. Step by Step 設定 TFS 2012 Create Team Project 權限 - 避免 TF218017、TF250044

    基本上權限的設定和 以往的 TFS 沒有什麼太大的差別 只是這次的權限設定畫面有略作些調整,我還是一併整理一下 當我們用 TFSSetup 的帳號安裝完 TFS 2012 後 想要在自已的電腦上用自已 ...

  10. 【堆栈平衡的说明太有才了】转贴自Jim&#39;s blog

    先说明.原发者iso9001 http://www.ghoffice.com/bbs/read.php?tid-35165.html他提供的地址(当他是个指针好了:P)http://ajiannet. ...