原题链接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 - [04] 窗口(Windows)

    题记部分 一.Flink中的窗口是什么 (1)一般真实的流都是无界的,怎样处理无界的数据? (2)可以把无限的数据流进行切分,得到有限的数据集进行处理 -- 也就是得到有界流 (3)窗口(Window ...

  2. mac地址查询

    打开命令提示符窗口(cmd程序) 快捷键 win+r 打开运行窗口,输入 cmd 命令打开 命令提示符窗口 或者点击开始菜单,在搜索程序和文件输入框,输入 cmd(会找到进入dos命令的cmd程序) ...

  3. Golang 入门 : 浮点数

    浮点数介绍 Go语言提供了两种精度的浮点数:float32 和 float64.它们的算术规范由IEEE754浮点数国际标准定义,该浮点数规范被所有现代的CPU支持. 这些浮点数类型的范围可以从很微小 ...

  4. composer 指定php版本

    需要指定php和composer的位置,然后再去执行composer命令 ## 指定PHP版本 指定composer 指定载入包 /usr/local/php7/bin/php composer /u ...

  5. 在GNU Hurd中感受Mach微内核的进程通信(IPC)

    什么是GNU Hurd 具体的时间线已经在官方维基页面得到详细描述[0],笔者在此就简单叙述一下.在1983年Richard Stallman开启了GNU项目,目的是创建一个自由的操作系统[1].在接 ...

  6. 还原大师-遍历残缺字符串匹配md5杂凑值

    题目: 我们得到了一串神秘字符串:TASC?O3RJMV?WDJKX?ZM,问号部分是未知大写字母, 为了确定这个神秘字符串,我们通过了其他途径获得了这个字串的32位MD5码. 但是我们获得它的32位 ...

  7. 【python日期和时间处理】time模块基本使用

    1. time模块中三种时间格式 时间戳 time模块获取各种精度的时间戳 import time timestamp = time.time() timestamp_s = int(time.tim ...

  8. llamacpp转换hf、vllm运行gguf

    Linux通过huggingface安装大模型 huggingface官网 https://huggingface.co/ wget https://repo.anaconda.com/minicon ...

  9. 网络编程-关闭连接(1)-C/C++相关系统调用

    背景 在linux网络编程中,经常需要编写关闭socket的代码,比如心跳检测失败需要关闭重连:网络报异常需要关闭重连.但究竟关闭操作做了什么,却不太清楚.目前项目使用Netty框架来实现的网络编程, ...

  10. DP——从入门到放弃 [Did Not Finish]

    Part 00 dp 什么时候用? 首先,如果这题数据范围一眼 BFS/DFS/暴力/随机化 可以过,那么还写 dp 干什么 但是,但你决定这题要贪心时,我建议咱还是要看一下它对不对 整一个石子合并这 ...