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

    class Solution: def insertIntoBST(self, root, val): """ Time: O(log(n)) [average case ...

  2. php 中文转拼音,可以只转首字母,可以设置utf8、gbk

    <?php class Pinyin { /** * 默认是gb编码,第二个参数随意设置即为utf8编 * @param type $isInitial 是否只返回首字母 * @return t ...

  3. hadoop配置2.6.1 centos7

    上传文件(分发)的三种方式: 1.本地: -file 的模式,上传一些小的文件. 例如: -file ./test INPUT_FILE_PATH_1="/The_Man_of_Proper ...

  4. php中显示数组与对象的实现代码

    1. 使用 print_r ( $array/$var ) print 是打印的意思,而r则取自Array的单词,那么该函数的功能就是打印数组内容,它既可以打印数组内容,也可以打印普通的变量. pri ...

  5. Cause: java.sql.SQLException: The user specified as a definer ('root'@'%') does not exist

    权限问题,授权 给 root 所有sql 权限 mysql> grant all privileges on *.* to root@"%" identified by &q ...

  6. Crash 文件调试

    Xcode目录下执行 find . -name symbolicatecrash 找到symbolicatecrash位置,将其拷贝到debug用的文件夹下 执行命令 export DEVELOPER ...

  7. 通过SSH克隆远程仓库(GitLab)到本地

    由于不是任何用户都能从远程仓库克隆到本地的,也是需要鉴别的,因此本地需要用git bash 创建一个公钥,而远程仓库也要把这个公钥保存下来,进而本地才可以从远程download.主要步骤如下: 1.首 ...

  8. 命令行下将磁盘从GPT转换为MBR

    1.在系统提示无法安装的那一步,按住“shift+f10”,呼出“cmd”命令符 2.输入:diskpart,回车 进入diskpart 3.输入:list disk,回车 显示磁盘信息 4.输入:s ...

  9. Python中fileinput模块使用方法

    fileinput模块提供处理一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行.python2.7文档关于fileinput介绍:fileinput   fileinp ...

  10. jquery实现分页+单删批删

    //定义一个分页的方法 public function fenye(){ //查询满足条件的总条数 $count = M("regis")->count(); //设置每页显 ...