请你来实现一个 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. Python3.7源码在windows(VS2015)下的编译和安装

    Python3.7源码在windows(VS2015)下的编译和安装 下载官方源码,使用vs2015(WIN10SDK),最python3.7.0的源码进行编译,编译出不同的版本(release,de ...

  2. React Native动画总结

    最近在使用react native进行App混合开发,相对于H5的开发,RN所提供的样式表较少,RN中不能使用类似于css3中的动画,因此,RN提供了Animated的API 1.写一个最简单的动画 ...

  3. sql 查询 以结果集为对象左连接

  4. springboot项目中配置swagger-ui

    Git官方地址:https://github.com/SpringForAll/spring-boot-starter-swagger Demo:https://github.com/dyc87112 ...

  5. PowerBI分析Exchange服務器IIS運行日誌

    PowerBI分析Exchange服務器IIS運行日誌 啟用狀態 PowerBI分析Exchange服務器IIS運行日誌 那麼在C:\inetpub\logs\LogFiles目錄下您才會看到如下日誌 ...

  6. let 和 const 在for 循环中的使用

    在ES6 的规范中,多了两个声明变量的关键字: let 和const.初次学习的时候,只记住了 let 声明的变量只在for 的循环体中有效,循环结束后 变量就消失了, 同时const 也可以在for ...

  7. 标准3层神经网络搭建Demo

    上面我们说了神经网络的基础知识,根据上章的基础尝试搭建一个标准的3层神经网络,参考https://www.cnblogs.com/bestExpert/p/9128645.html 1.框架代码 1. ...

  8. Linux C Socket简单实例与详细注释

    最近做的东西与socket十分紧密,所以很好奇它具体是如何实现的,以前也有了解过,但是又忘记了,于是把它记录下来,以便日后查看. 服务器端:server.c #include <sys/type ...

  9. Plctext 如何发送默认的模式

    当切屏指令都没有的情况下,就会发,即:当65这个地址位(0-7),都为零的情况下.

  10. ezdxf包下autocad的开发

    利用 ezdxf 库读写geotiff格式的地形影像方法(InsertRasterToCAD) 包一:dxfgrabber GitHub - mozman/dxfgrabber: Outdated D ...