原题链接https://leetcode.com/problems/string-to-integer-atoi/

说实话,看到这道题之前,看这通过率有点慌,到底是因为啥 让一道medium的题目这么“厉害”咧,接着往下瞧~

  • problem description:

    Implement atoi which converts a string to an integer.

    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.

    实现将字符串转换为整数的atoi。

    函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符。然后,从这个字符开始,取一个可选的初始加号或减号,后面跟着尽可能多的数字,并将它们解释为一个数值。

    字符串可以在组成整数的字符之后包含其他字符,这些字符将被忽略,并且对该函数的行为没有影响。

    如果str中的非空格字符的第一个序列不是有效的整数,或者由于str为空或只包含空格字符而不存在这样的序列,则不执行转换。

    如果不能执行有效的转换,则返回零值。

    Note:

  • Only the space character ' ' is considered as whitespace character.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.

    注意:

    只有空格字符' '被认为是空格字符。

    假设我们正在处理一个环境,只能在32位带符号整数存储整数范围:(231−231−1)。如果数值的范围可表示的值,INT_MAX(231−1)或INT_MIN返回(231−)。

  • Example 1:

    Input: "42"
    Output: 42

    Example 2:

    Input: "   -42"
    Output: -42
    Explanation: The first non-whitespace character is '-', which is the minus sign.
      Then take as many numerical digits as possible, which gets 42.

    Example 3:

    Input: "4193 with words"
    Output: 4193
    Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.

    Example 4:

    Input: "words and 987"
    Output: 0
    Explanation: The first non-whitespace character is 'w', which is not a numerical
      digit or a +/- sign. Therefore no valid conversion could be performed.

    Example 5:

    Input: "-91283472332"
    Output: -2147483648
    Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
      Thefore INT_MIN (−231) is returned.
  • thinking:读罢题目,应该会明确,之所以准确率不高应该与情况较多有关系。
    1.删掉空格
    2.判断是否有正负号,但是只能有一个,否则判为无效转换返回0
    3.一旦遇到非数字,停止转换
    4.出现数据溢出时,返回边界值
  • 源码附录:
    class Solution {
    public int myAtoi(String str) {
    str = str.trim();//删空格 if(str == null || str.length() < 1){
    return 0;
    } int flag = 1;
    int index = 0;
    long result = 0; if(str.charAt(index) == '-'){
    flag = -1;
    index ++;
    }
    else if(str.charAt(index) == '+'){
    index ++;
    } for(;index<str.length();index ++){
    int t = str.charAt(index) - '0';
    if(t>9 || t<0){
    break;
    } if(flag == 1){
    result = result*10 + t;
    if(result > Integer.MAX_VALUE){
    return Integer.MAX_VALUE;
    }
    }
    else{
    result = result*10 - t;
    if(result < Integer.MIN_VALUE){
    return Integer.MIN_VALUE;
    }
    }
    } int temp = (int)result;
    return temp;
    }
    }

    #四级over 寒假之前被实训和复习安排的明明白白,LeetCode……寒假见

String to Integer (atoi)——LeetCode进阶路⑧的更多相关文章

  1. String to Integer (atoi) leetcode

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

  2. String to Integer (atoi) ---- LeetCode 008

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

  3. String to Integer (atoi) leetcode java

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

  4. 8. String to Integer (atoi) ---Leetcode

    Implement atoi to convert a string to an integer. 题目分析: 题目本身很简单就是将一个字符串转化成一个整数,但是由于字符串的千差万别,导致在实现的时候 ...

  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. Kotlin实现LeetCode算法题之String to Integer (atoi)

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

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

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

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

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

  9. LeetCode 8. 字符串转换整数 (atoi)(String to Integer (atoi))

    8. 字符串转换整数 (atoi) 8. String to Integer (atoi) 题目描述 LeetCode LeetCode8. String to Integer (atoi)中等 Ja ...

  10. 【leetcode】String to Integer (atoi)

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

随机推荐

  1. Flink - [06] 状态管理

    题记部分 一.Flink中的状态 由一个任务维护,并且用来计算某个结果的所有数据,都属于这个任务的状态. 可以认为状态就是一个本地变量,可以被任务的业务逻辑访问. Flink会进行状态管理,包括状态一 ...

  2. 单线程的Redis速度为什么快?

    博客:https://www.emanjusaka.com 博客园:https://www.cnblogs.com/emanjusaka 公众号:emanjusaka的编程栈 by emanjusak ...

  3. WordPress域名更换小记

    WordPress域名更换记录 1.准备工作 ​ 在开始之前,要有一个全面的备份,包括网站的文件和数据库.这确保了如果出现问题,你可以恢复到更改之前的状态.不然中间卡壳直接连后台都打不开了,只能重装. ...

  4. 200条Git命令复习总结使用

    新建 创建一个新的 git 版本库.这个版本库的配置.存储等信息会被保存到.git 文件夹中 # 初始化当前项目 $ git init # 新建一个目录,将其初始化为Git代码库 $ git init ...

  5. Camel多智能体框架初探

    Camel介绍 CAMEL 是一个开源社区,致力于探索代理的扩展规律.我们相信,在大规模研究这些代理可以提供对其行为.能力和潜在风险的宝贵见解.为了促进该领域的研究,我们实现了并支持各种类型的代理.任 ...

  6. 接口中的成员特点、类和接口之间的各种关系--java进阶day02

    1.接口的成员特点 1.接口没有构造方法 接口没有构造方法,但是实现类中有构造方法,super()又该访问谁呢? 类实现接口只是认干爹,类本身还是会有亲爹Object,super()会访问Object ...

  7. win11的go安装

    背靠国外各大金主的go语言,在各种推动下,可谓是新的弄潮儿,但国内虽然各种推销,但从安装到开发再到维护,资料都少之又少,可能被垄断了解释权吧. 因此下面的也只是一个记录而已,是一次仅限于本人本机本阶段 ...

  8. unity 多层叠加的BillBoard特效转序列帧特效降低overdraw

  9. Java+Selenium+Junit实现web自动化demo

    1.新建maven工程 打开IDEA新建maven项目并引入相关依赖,步骤如下: 需要引入的依赖 <dependencies> <dependency> <groupId ...

  10. MySql随笔记基础

    XAMPP使用 shell 命令 每个数据库对应 一个子文件夹 mysql 进入mySQL的命令 -uroot userroot 登录用户 -uroot -p password 登录密码 -p123 ...