LeetCode算法题之8:String to Integer (atoi)

LeetCode算法题8. String to Integer (atoi)(medium) 声明: 因本人为AI路上的新手,文章用于辅助个人的整理记忆,理解难免有偏差之处,都是个人拙见,如给其他同僚造成困扰,还请见谅,非常非常非常欢迎私信共同讨论,共同进步 题目描述: Implement atoi which converts a string to an integer. The function first discards as many whitespace characters... 阅读详情

LeetCode之8:String to Integer (atoi)

问题描述:

在这里插入图片描述
将字符串转换为合法的数字。题目地址

问题的陷阱与难点:

  1. 思路很好想,但是鲁棒性,鲁棒性,鲁棒性!!!

缺陷代码(99.50%)

public int myAtoi(String str) {
    if (str == null || str.length() ==0) {
      return 0;
    }
    int index = 0;
    int signalIndex = -1;
    boolean firstNonEmpty = true;
    while (index < str.length() && (str.charAt(index) < '0' || str.charAt(index) > '9')) {
      char current = str.charAt(index);
      //解决"words and 987",首个非空字符为非数字
      if (current != ' ' && current != '+' && current != '-' && firstNonEmpty) {
        firstNonEmpty = false;
        return 0;
      }
      if (current == '+' || current == '-') {
        signalIndex = index;
      }
      index ++;
    }
    if (index >= str.length()) {
      return 0;
    }
    long negative = 1L;
    //解决"+ 99",符号位与数字有间隔
    if (signalIndex >=0 && index - signalIndex > 1) {
      return 0;
    }
    if (index > 0) {
      char signal = str.charAt(index - 1);
      if (signal == '-') {
        negative = -1L;
      }
      //解决"++999",连续多个符号位
      if (index > 1 && (signal == '+' || signal == '-')) {
        char temp =  str.charAt(index - 2);
        if (temp == '+' || temp == '-') {
          return 0;
        }
      }
    }
    long result = 0L;
    while (index < str.length() && str.charAt(index) >= '0' && str.charAt(index) <= '9') {
      result = result * 10L + (long)(str.charAt(index) - '0');
      //解决越界问题,使用long存储数字也可能是不足够的。
      if (result >= Integer.MAX_VALUE && negative == 1) {
        return Integer.MAX_VALUE;
      } else if (result <= Integer.MIN_VALUE && negative == -1) {
        return Integer.MIN_VALUE;
      }
      index ++;
    }
    result = result * negative;
    if (result >= Integer.MAX_VALUE) {
      return Integer.MAX_VALUE;
    } else if (result <= Integer.MIN_VALUE) {
      return Integer.MIN_VALUE;
    } else {
      return (int) result;
    }
  }

思路

这道题主要考察的是鲁棒性。 思路是定位到符号位以及第一个数字位。下面只强调一下鲁棒性可能被遗忘的点。

  • 解决"+ 99",符号位与数字有间隔
  • 解决"++999",连续多个符号位
  • 解决越界问题,使用long存储中间的计算结果也可能是不足够的。
python写算法题leetcode: 8. String to Integer (atoi) https://leetcode.com/problems/string-to-integer-atoi/#/description class Solution(object): def myAtoi(self, str): """ :type str: str :rtype: int """ 阅读详情

相关推荐

【力扣Leetcode题解系列之0008String to Integer (atoi):字符串转换整数 (atoi) 解题攻略】

本文介绍了力扣第8题“字符串转换整数(atoi)”的解题方法。题目要求实现一个类似C/C++中atoi的函数,将字符串转换为32位有符号整数。解题需处理以下关键点:去除前导空格、判断正负号、提取连续数字字符并进行边界检查。文章提供了Python正则表达式法和常规遍历法两种解法,并给出了Python、Java、C、C++四种语言的代码实现。正则表达式法简洁高效,而常规遍历法则更直观地展现了处理流程。两种方法都需要注意32位整数的溢出问题,当结果超出[-2^31, 2^31-1]范围时返回对应的边界值。

youngerwang的博客 889

Leetcode 8. String to Integer (atoi)

@[TOC](Leetcode 8. String to Integer (atoi)) 题目说明 Implement atoi which converts a string to an integer. 题目可以简短概括为将一个输入的字符串转化为数值型输出,其中特殊情况有: 1.首字符可以是空格/数字/正负符号(有效输入为多空格直至出现后面两种情况之一) 2.首字符一旦不是上述三种情况之一直...

babi_qq的博客 270

LeetCode8. String to Integer (atoi)(C++)

地址:https://leetcode.com/problems/string-to-integer-atoi/ 题目: Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the firs...

Ethan95的博客 593

8. String to Integer (atoi) [easy] (Python)

题目链接https://leetcode.com/problems/string-to-integer-atoi/题目原文 Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please

Coder_Orz的博客 6904

[LeetCode]String to Integer (atoi)

题目要求: mplement 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 inpu

zhouworld16的专栏 5895

LeetCode8. String to Integer (atoi)(Python)

Problem 字符串转为整型 https://leetcode.com/problems/string-to-integer-atoi/ 实现atoi将字符串转换为整数。 该函数首先丢弃所需数量的空白字符,直到找到第一个非空白字符。然后,从该字符开始,采用可选的初始加号或减号,后跟尽可能多的数字,并将它们解释为数值。 字符串可以包含在形成整数之后的其他字符,这些字符将被忽略并且对此函数的行为没有...

E.W的博客 1689

LeetCode--String to Integer (atoi)

String to Integer (atoi) 1 题目 Implementatoiwhichconverts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character i...

Vilin的博客 301

LeetCode题解 String-to-Integer(atoi)

原文在此, 可以来我Blog翻翻哦。 hhh, 开始一天一道LeetCode吧, 恩, 忘记了之前算到第几天了, 那么从头开始吧, 今天是第一天. 今天的题目是(8. String to Integer (atoi))[https://leetcode.com/problems...] 题目描述: Implement atoi which...

weixin_33922672的博客 75

String to Integer (atoi) -- LeetCode

原题链接:http://oj.leetcode.com/problems/string-to-integer-atoi/  这道题还是对于Integer的处理,在Reverse Integer这道题中我有提到,这种题的考察重点并不在于问题本身,而是要注意corner case的处理,整数一般有两点,一个是正负符号问题,另一个是整数越界问题。思路比较简单,就是先去掉多余的空格字符,然后读符号(

Code Ganker 1万+

LeetCode - String to Integer (atoi)

LeetCode - String to Integer (atoi)

杂的文 540

LeetCode 8. String to Integer (atoi) 字符串转整形

LeetCode 8. String to Integer (atoi) 字符串转整形 8. String to Integer (atoi) Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary unt...

goasleep的博客 210

leetcode——String to Integer (atoi)

leetcode——String to Integer (atoi) 【题目描述】 Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace ch...

ma_ke_xin的博客 175

[LeetCode]String to Integer (atoi)atof,itoa

好像也不算太难,核心的代码只有几行,但是往往需要我们重点考虑的其实是在各种特殊输入的考虑上。 #include <iostream> #include <cstdio> using namespace std;float atof(const char * arr) { const char * index = arr; float result = 0; int fcoun

1195

LeetCode 8String to Integer (atoi)(C++ Java Python)

题目:http://oj.leetcode.com/problems/string-to-integer-atoi/ Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please

龙之梦 4858

LeetCode(8) String To Integer(atoi)

题目如下: String to Integer (atoi) Total Accepted: 29609 Total Submissions: 214853 My Submissions Question Solution  Implement atoi to convert a string to an integer. Hint: Carefully consider all pos

BUILD 2582

Leetcode1-50: 08, 8. String to Integer (atoi)

@[TOC](Leetcode1-50: 08, 8. String to Integer (atoi)) 问题描述 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 t

Y123iwantit的博客 205

String to Integer atoi -- LeetCode

String to Integer atoi -- LeetCode

weixin_43418664的博客 291
上一篇: LeetCode算法题之7:Reverse Integer
下一篇: LeetCode算法题之9:Palindrome Number
耐利
博客等级 码龄12年 188粉丝 38原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值