[Leetcode] String to integer atoi 字符串转换成整数

时间:2023-01-06 08:13:36

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.

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

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.

题意:将字符串转换成整数,需要注意的几点如下:

1)从字符串开头开始遍历,若开始为空格,则跳过,直到第一个非空格的字符,没有非空格字符,则返回0,;

2)若第一个非空的字符为正负号,则最后返回的数考虑正负号;

3)若接下来的字符不是数字,则返回0. 完全不考虑小数点和自然数的情况;

4)继续遍历中,遇到数字则转换成整数保存下来,若再次遇到非数字型的字符,则返回当前保存的值;

5)遍历过程要考虑值的返回,若是超过整型类型的范围,则返回边界值;

参考了,zhouworld16 ,代码如下:

 class Solution {
public:
int atoi(const char *str)
{
if(str==NULL) return ;
long long res=;
int i=;
bool flag=true;
while(str[i]==' '||str[i]=='')
i++;
if(str[i]=='+')
i++;
if(str[i]=='-')
{
flag=false;
i++;
} int len=strlen(str);
for(;i<len;++i)
{
if(str[i]>=''&&str[i]<='')
{
res=res*+(str[i]-'');
if(res>INT_MAX)
return flag?INT_MAX:INT_MIN;
}
else
{
return flag?res:(-)*res;
}
}
return flag?res:(-)*res;
}
};

值得注意的是,res的类型应该是范围比int大的类型,因为当字符串为"2147483648"仅比INT_MAX大1时,由于res只能保存2147483647,此时会产生溢出。返回-2147483648。所以res的类型要扩展,不这样做的话,参考Grandyang的博客

[Leetcode] String to integer atoi 字符串转换成整数的更多相关文章

  1. 【LeetCode每天一题】String to Integer &lpar;atoi&rpar;&lpar;字符串转换成数字&rpar;

    Implement atoi which converts a string to an integer.The function first discards as many whitespace ...

  2. 8&period; String to Integer &lpar;atoi&rpar; 字符串转成整数

    [抄题]: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: T ...

  3. 【LeetCode】8&period; String to Integer &lpar;atoi&rpar; 字符串转换整数

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字符串转整数,atoi,题解,Leetcode, 力扣,P ...

  4. 【LeetCode】String to Integer &lpar;atoi&rpar;&lpar;字符串转换整数 &lpar;atoi&rpar;&rpar;

    这道题是LeetCode里的第8道题. 题目要求: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  5. &lbrack;LeetCode&rsqb;面试题67&period; 把字符串转换成整数

    题目 写一个函数 StrToInt,实现把字符串转换成整数这个功能.不能使用 atoi 或者其他类似的库函数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们 ...

  6. &lbrack;LeetCode&rsqb; String to Integer &lpar;atoi&rpar; 字符串转为整数

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

  7. &lbrack;LeetCode&rsqb; String to Integer &lpar;atoi&rpar; 字符串

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

  8. StringToInt&lpar;atoi&rpar; 字符串转换成整数

    public class StringToInt { public int atoi(String s) { long num = 0; int minus = 0; if(s==null) { re ...

  9. 算法练习-字符串转换成整数&lpar;实现atoi函数&rpar;

    练习问题来源 https://leetcode.com/problems/string-to-integer-atoi/ https://wizardforcel.gitbooks.io/the-ar ...

随机推荐

  1. &period;net4&period;0中使用ODP&period;net访问Oracle数据库(无需安装oracle客户端部署方法)

    1.在没有安装oracle客户端的设备上也能访问服务器上的oracle (通俗的讲就是:开发的应用程序 和 oracle数据库服务器分别在两台电脑上)2.不需要配置TnsNames.Ora文件 开发环 ...

  2. loadrunner做webservice接口之简单调用

    今天听大神讲了webservice做接口,我按照他大概讲的意思自己模拟实战了下,可能还有很多不对,一般使用webservice做接口,会使用到soapui,但是用了loadrunner以后发现lr很快 ...

  3. 【Android界面实现】信息更新小红点显示——自己定义控件BadgeView的使用介绍

    在如今大部分的信息公布类应用,都有这样的一个功能:当后台数据更新,比方有系统消息或者是用户间有互动的时候,通过在控件上显示一个小红点来提示用户有新的信息.一般来说,这样的业务需求,我们能够在布局文件里 ...

  4. leetcode — word-break-ii

    import java.util.*; /** * Source : https://oj.leetcode.com/problems/word-break-ii/ * * Given a strin ...

  5. 数据结构--hashtable(散列表)

    散列 散列又叫hash.是通过关键字把数据映射到指定位置的一种数据结构.理想的散列表,是一个包含关键字的固定大小的数组 哈希表存储的是键值对,其查找的时间复杂度与元素数量多少无关,哈希表在查找元素时是 ...

  6. es6 中的 symbol

    symbol 的引入是为了解决对象中的属性名冲突的问题 使用symbol() 函数生成的变量值不与任何的变量值相等,  所有用改变量的值做属性名是不会冲突的 symbol 可以转化为字符串, 可以转化 ...

  7. 在kali linux上安装VMware tool

    在安全圈的门口徘徊了一年,一直不知道该如何入门,现在决定先从kali 入手.有同样兴趣的伙伴欢迎一起. 但是刚在VMware上安好系统就遇到了一个大麻烦,看了很多书,还有教程但总是遇到这样那样的问题. ...

  8. 不一样的go语言-构建系统与构件系统

    前言   代码的最后一步是构建成计算机可识别的二进制数据,然后才得以在计算机上运行.如果你曾经写过有点规模(至少数十个以上独立的源文件,且需要依赖第三方包)C语言项目,必定对C语言项目的构建过程印象深 ...

  9. Druid 连接池 JDBCUtils 工具类的使用

    Druid工具介绍 它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个SQL Parser. 支持所有JDBC兼容的数据库,包括Oracle.MySQL. ...

  10. C&plus;&plus; 函数 引用

    一.引用的概念 引用就是某一变量(目标)的一个别名,对引用的操作与对变量直接操作完全一样.引用的声明方法: 类型标识符 &引用名 = 目标变量名: 为一个变量起一个别名.假如有一个变量a,想给 ...