Implement atoi to convert a string to an integer.

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.

int myAtoi(char* str) {
long int ret = ;
char* p = str;
bool isPos = true; //get the positive or negative
while(*p == ' '){ //neglect space at the beginning
p++;
continue;
}
if(*p == '+') p++;
else if(*p == '-'){
isPos = false;
p++;
} //get the digit
while(*p != '\0'){
if(*p < '' || *p > '') break; //invalid character occurs, stop converting
ret = ret* + (*p) - '';
if(ret >= ) break; //out of int range
p++;
} if(!isPos) ret = -ret;
if(ret > INT_MAX) ret = INT_MAX;
else if(ret < INT_MIN) ret = INT_MIN;
return ret;
}

8. String to Integer (整数的溢出)的更多相关文章

  1. 7. Reverse Integer (整数的溢出)

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 For the p ...

  2. LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

  3. 剑指offer 67. 字符串转换为整数(Leetcode 8. String to Integer (atoi))

    题目:剑指offer 67题 需要考虑的情况:空指针.nullptr.空字符串"".正负号.数值溢出.在写代码的时候对这些特殊的输入都定义好合理的输出.可以定义一个全局布尔型变量g ...

  4. 【leetcode】String to Integer (atoi)

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

  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. 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理

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

  7. 《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 书的地址:https://hk029.gitbooks.io/leetboo ...

  8. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

  9. Leetcode 题目整理-2 Reverse Integer && String to Integer

    今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 1 ...

  10. Q8:String to Integer (atoi)

    8. String to Integer (atoi) 官方的链接:8. String to Integer (atoi) Description : Implement atoi to conver ...

随机推荐

  1. leetcode1003

    class Solution: def isValid(self, S: str) -> bool: n = len(S) if n % 3 != 0: return False while n ...

  2. 【开发工具】 JEECG_3.7新版开发工具

    链接:http://pan.baidu.com/s/1gfthmAf    密码:2yfv

  3. 容器viewController添加或者删除子viewController

    假设有一个viewControllerA,我们想在viewControllerA中添加viewControllerB,需要执行以下方法: [viewControllerA addChildViewCo ...

  4. TCP 协议相关

    TCP特点: 提供可靠的,保证数据能够准确的到达目的地,如果不能,需要检测发现并重传 流量可控,管理发送数据的频率,不超过设备的承载能力 滑动窗口:https://blog.csdn.net/wdsc ...

  5. 设计模式入门——Head First

    设计模式是被前人发现.经过总结形成了一套某一类问题的一般性解决方案.使用模式最好的方式是:把模式装进脑子,然后在设计和已有的应用中,寻找何处可以使用它们.以往是代码复用,现在是经验复用. 从模拟鸭子游 ...

  6. uml用例关系

    关联关系 关联关系是指执行者与用例之间的关系,又称为通信关系,如果某个执行者可以对某个用例进行操作,它们之间就具有关联关系,如下图所示,“经理”有一个功能为“查看库存报表”,因此可以在执行者“经理”和 ...

  7. [PC]PHPCMS v9.5.6整合UEditer1.4.2

    ----------------------------------------------------------------------------------------------- 首先去U ...

  8. ckeditor使用说明

      2015-08-17 15:42热心网友最快回答 一.使用方法:1.在页面<head>中引入ckeditor核心文件ckeditor.js<script type="t ...

  9. 用Lucene4.5对中文文本建立索引

    这里需要完成一个能对txt文本建立索引,并能完成检索查询.完成这个功能,使用的是Lucene4.5,同时使用其自带的中文分析器. 准备工作是在一个文件夹里面建一些txt文件,这是我的文件结构: 首先要 ...

  10. VUE 关于理解$nextTick()的问题

    Vue.js 通常鼓励开发人员沿着“数据驱动”的方式思考,避免直接接触 DOM.this.$nextTick()官方介绍:将回调延迟到下次 DOM 更新循环之后执行.在修改数据之后立即使用它,然后等待 ...