请你来实现一个 atoi 函数,使其能将字符串转换成整数。

首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。

当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。

该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。

注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。

在任何情况下,若函数不能进行有效的转换时,请返回 0。

说明:

假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231,  231 − 1]。如果数值超过这个范围,qing返回  INT_MAX (231 − 1) 或 INT_MIN (−231) 。

示例 1:

输入: "42"
输出: 42

示例 2:

输入: "   -42"
输出: -42
解释: 第一个非空白字符为 '-', 它是一个负号。
  我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。

示例 3:

输入: "4193 with words"
输出: 4193
解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。

示例 4:

输入: "words and 987"
输出: 0
解释: 第一个非空字符是 'w', 但它不是数字或正、负号。
因此无法执行有效的转换。

示例 5:

输入: "-91283472332"
输出: -2147483648
解释: 数字 "-91283472332" 超过 32 位有符号整数范围。
  因此返回 INT_MIN (−231) 。
 int myAtoi(char* str) {
if(!str){
return ;
}
bool inte = true;
char *tmp;
tmp = str;
signed int num=;
signed int compare_num = ;
signed int tmp_add=;
while(*tmp != '\0'){
if(*tmp == ' ')
{ tmp++;
continue;
}
if( (*tmp >= '' && *tmp <= '') || *tmp == '+' || *tmp == '-')
{
if(*tmp == '-'){
inte = false;
tmp++;
break;
}
if(*tmp == '+'){
inte = true;
tmp++; }
break;
}
else{
return ;
}
} while(*tmp != '\0'){
if(*tmp < '' || *tmp >''){
if(inte){
return num;
}
else
return - * num;
}
tmp_add = num;
for(int i = ; i < ;i++){
if (num+tmp_add <num )
goto overnum;
num = num + tmp_add;
}
if(num +(*tmp -'') < num)
goto overnum;
num = num +(*tmp - '');
//num = num * 10 + (*tmp - '0'); compare_num = num;
tmp++;
}
if(inte){
return num;
}
else{
return - * num;
}
overnum:
if(inte) //正数
return ;
else
return -; }

LeetCode.atoi的更多相关文章

  1. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  2. LeetCode OJ-- String to Integer (atoi) **

    https://oj.leetcode.com/problems/string-to-integer-atoi/ 细节题,把一个字符串转换成整数 class Solution { public: in ...

  3. [LeetCode][Python]String to Integer (atoi)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...

  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) leetcode

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

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

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

  7. leetcode刷题笔记08 字符串转整数 (atoi)

    题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...

  8. 【一天一道LeetCode】#8. String to Integer (atoi)

    一天一道LeetCode系列 (一)题目 Implement atoi to convert a string to an integer. Hint: Carefully consider all ...

  9. LeetCode(8):字符串转整数(atoi)

    Medium! 题目描述: 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合 ...

随机推荐

  1. Git操作记录

    记录一些用过的操作 增加远程推送分支 git remote add orgin http://xxxxx.git 直接新建本地分支,将远程分支提取出来. git checkout -t origin/ ...

  2. ecshop 商品属性显示方法

    功能:在商品列表上,点击放大镜,显示商品所有属性以及其价格,效果如下: 方法/步骤: 1.编辑\admin\templates\goods_list.htm 模板,在 <!-- 商品搜索 --& ...

  3. SVN和Git 介绍,区别,优缺点以及适用范围

    SVN是Subversion的简称,是一个开放源代码的版本控制系统,支持大多数常见的操作系统.作为一个开源的版本控制系统,Subversion管理着随时间改变的数据.这些数据放置在一个中央资料档案库( ...

  4. 通过后缀名和MIME-TYPE检查实现文件类型校验

    前言 文件上传是一个在开发中很常见的需求场景,通常出于安全考虑,我们会对上传的文件进行类型校验,其中常见的有后缀名校验,mime-type校验 话不多说,直接上代码 1.首先定义允许上传的文件类型白名 ...

  5. Windows 10 & React Native & Android

    Windows 10 & React Native & Android https://facebook.github.io/react-native/docs/getting-sta ...

  6. python 学习三

    list循环删除下标会出错 L = [1,1,1,2,3,4,5]#list是根据下标来取值 #下标0,1,2,3,4,5,6 循环后下标错位 输出的结果是[1,2,4],把1也取到了 #l2 = [ ...

  7. Django_rest framework 框架介绍

    restful介绍  restful协议 一切皆是资源,操作只是请求方式 URL 设计 原先的URL设计方式 在url 中体现出操作行为 /books/ books /books/add/ addbo ...

  8. js 调用打印机方法

    <button onclick="localdy({php echo $item['order']['id'];})" class="btn btn-xs orde ...

  9. chrome主页篡改解决方法

    网上有一个超级细致的小白教学连接,但是发现很难找到,分享一下:https://arlenluo.github.io./2017/03/12/DefeatYourBrowser 还有一种情况是要打开 & ...

  10. 运行adb命令报错adb server version (31) doesn't match this client (39); killing...

    执行adb devices 报错 原因分析: 这个是socket 的端口被占用了,我这里是因为360手机助手占用了这个端口,所以其他的就不能够用了. 解决办法: 卸载了360的手机助手就可以了 首先 ...