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.

写一个a to i 字符串转为整形的函数。

注意考虑所有可能的输入情况。

题目不设置具体的输入规范,你需要思考收集所有可能的输入。

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.

函数要求:

函数首先应该丢弃字符串首字符前的空白格;

判断数字的正负符号,并将后面跟着的数字符号转化为数值;

在数字字符之后,可能还包含其他不相关字符,这些字符应该省去;

如果在空白格后,第一个字符不是有效的数字字符,或者根本没有有效的数字字符存在,那么不需要进行转化,返回空;

如果没有有效的字符可以进行转化,那么返回0;

如果正确的数值超过了能表示的范围,那么返回INT_MAX或者INT_MIN。

解题:

对于一个输入,基本需要考虑4种情况:

1、删去头部的空白格(空格);

2、判断数字的正负;

3、注意整形溢出;

4、处理无效的输入。

解题步骤:

1、不管输入是char *还是 stl string,需要三个int变量,正负号sign,结果值base,操作序号i;

2、判断输入是否有效;

3、删去头部的空格;

4、判断正负符号,用一些技巧使正时sign为1,负时sign为0;

5、判断字符是否是数字,如果是:

  (1)在计算此数字之前,查看当前base是否已经越界,即是否能安全的计算当前数字;

    即 base < INT_MAX / 10; 或者 base == INT_MAX / 10 && 当前数字 <= INT_MAX % 10;

  (2)计算当前数字;

6、返回代符号的base;

代码借鉴自Leetcode上题后讨论

 class Solution {
public:
int atoi(const char *str) {
int sign = , base = , i = ; while (str[i] == ' ')
i++; if (str[i] == '-' || str[i] == '+') {
sign = - * (str[i++] == '-');
} while (str[i] >= '' && str[i] <= '') {
if (base > INT_MAX / || (base == INT_MAX / && str[i] - '' > )) {
if (sign == )
return INT_MAX;
else
return INT_MIN;
}
base = * base + (str[i++] - '');
} return base * sign;
}
};

【Leetcode】【Easy】String to Integer (atoi)的更多相关文章

  1. 【leetcode】String to Integer (atoi)

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

  2. LeetCode【8】. String to Integer (atoi) --java实现

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

  3. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  4. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  5. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  6. 【Leet Code】String to Integer (atoi) ——常考类型题

    String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement ato ...

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

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

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

  9. LeetCode: String to Integer (atoi) 解题报告

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

  10. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

随机推荐

  1. C# Task超时规则

    需要知道以下的知识 正规的骚操作:https://stackoverflow.com/questions/4238345/asynchronously-wait-for-taskt-to-comple ...

  2. sublim text3中的一些设置

    {    "dictionary": "Packages/Language - English/en_US.dic",    "font_face&q ...

  3. hdu1286 找新朋友 欧拉函数模板

    首先这一题用的是欧拉函数!!函数!!不是什么欧拉公式!! 欧拉函数求的就是题目要求的数. 关于欧拉函数的模板网上百度一下到处都是,原理也容易找,这里要介绍一下另一个强势模板. 在这一题的讨论里看到的. ...

  4. css3 渐变色

    Firefox可以使用角度来设定渐变的方向,而webkit只能使用x和y轴的坐标. 渐变可以创建类似于彩虹的效果,低版本的浏览器使用图片来实现,CSS3将会轻松实现网页渐变效果 粘贴代码 <di ...

  5. centos yum安装高版本php,apache,mysql

    1.检查当前安装的PHP包 yum list installed | grep php 或者   yum list installed php* 如果要删除,可执行 yum remove php.x8 ...

  6. vue组件传参

    一.父子组件的定义 负值组件的定义有两种,我称为常规父子组件和特殊父子组件. 1.1.常规父子组件 将其他组件以import引入用自定义标签接收,在当前组件中component里注册该标签,页面上可以 ...

  7. VUE项目引入微信jssdk

    npm i -S weixin-js-sdkimport wx from 'weixin-js-sdk'

  8. CSAPP阅读笔记-32位64位的区别--来自第三章引言的笔记--P110

    仅从寻址上看,32位和64位机器能寻址的内存空间大小不同. 需要知道的是,计算机系统对存储器作了抽象,程序“认为”内存是一个很大的字节数组,然而实际上它是由多个硬件存储器和操作系统组合起来实现的. 程 ...

  9. 017-Servlet抽取时的BaseServlet模板代码

    package www.test.web.servlet; import java.io.IOException; import java.lang.reflect.Method; import ja ...

  10. JS实现多少小时前,多少天前...

    最近需要实现题目的功能,因为我的时间戳是PHP生成的,所以转换JS时间戳需要乘1000,废话不多说,看下面的代码把! 大家可以判断一下传进来的值是否为数值型,还有判断是否比当前的时间戳大!可以根据结果 ...