广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++实现LeetCode(8.字符串转为整数)
  • 380
分享到

C++实现LeetCode(8.字符串转为整数)

2024-04-02 19:04:59 380人浏览 八月长安
摘要

[LeetCode] 8. String to Integer (atoi) 字符串转为整数 Implement atoi which converts

[LeetCode] 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 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.

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.

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.

字符串转为整数是很常用的一个函数,由于输入的是字符串,所以需要考虑的情况有很多种。博主之前有一篇文章是关于验证一个字符串是否为数字的,参见 Valid Number。在那篇文章中,详细的讨论了各种情况,包括符号,自然数,小数点的出现位置,判断他们是否是数字。个人以为这道题也应该有这么多种情况。但是这题只需要考虑数字和符号的情况:

1. 若字符串开头是空格,则跳过所有空格,到第一个非空格字符,如果没有,则返回0.

2. 若第一个非空格字符是符号 +/-,则标记 sign 的真假,这道题还有个局限性,那就是在 c++ 里面,+-1 和-+1 都是认可的,都是 -1,而在此题里,则会返回0.

3. 若下一个字符不是数字,则返回0,完全不考虑小数点和自然数的情况,不过这样也好,起码省事了不少。

4. 如果下一个字符是数字,则转为整形存下来,若接下来再有非数字出现,则返回目前的结果。

5. 还需要考虑边界问题,如果超过了整型数的范围,则用边界值替代当前值。

C++ 解法:


class Solution {
public:
    int myAtoi(string str) {
        if (str.empty()) return 0;
        int sign = 1, base = 0, i = 0, n = str.size();
        while (i < n && str[i] == ' ') ++i;
        if (i < n && (str[i] == '+' || str[i] == '-')) {
            sign = (str[i++] == '+') ? 1 : -1;
        }
        while (i < n && str[i] >= '0' && str[i] <= '9') {
            if (base > INT_MAX / 10 || (base == INT_MAX / 10 && str[i] - '0' > 7)) {
                return (sign == 1) ? INT_MAX : INT_MIN;
            }
            base = 10 * base + (str[i++] - '0');
        }
        return base * sign;
    }
};

Java 解法:


public class Solution {
    public int myAtoi(String str) {
        if (str.isEmpty()) return 0;
        int sign = 1, base = 0, i = 0, n = str.length();
        while (i < n && str.charAt(i) == ' ') ++i;
        if (i < n && (str.charAt(i) == '+' || str.charAt(i) == '-')) {
            sign = (str.charAt(i++) == '+') ? 1 : -1;
        }
        while (i < n && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
            if (base > Integer.MAX_VALUE / 10 || (base == Integer.MAX_VALUE / 10 && str.charAt(i) - '0' > 7)) {
                return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            }
            base = 10 * base + (str.charAt(i++) - '0');
        }
        return base * sign;
    }
}

到此这篇关于C++实现LeetCode(8.字符串转为整数)的文章就介绍到这了,更多相关C++实现字符串转为整数内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C++实现LeetCode(8.字符串转为整数)

本文链接: https://www.lsjlt.com/news/130259.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++实现LeetCode(8.字符串转为整数)
    [LeetCode] 8. String to Integer (atoi) 字符串转为整数 Implement atoi which converts...
    99+
    2022-11-12
  • C++怎么实现字符串转为整数
    今天小编给大家分享一下C++怎么实现字符串转为整数的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。String to Inte...
    99+
    2023-06-19
  • C++实现LeetCode(6.字型转换字符串)
    [LeetCode] 6. ZigZag Conversion 之字型转换字符串 The string "PAYPALISHIRING" is written i...
    99+
    2022-11-12
  • C++实现LeetCode字型转换字符串的方法
    这篇文章主要介绍“C++实现LeetCode字型转换字符串的方法”,在日常操作中,相信很多人在C++实现LeetCode字型转换字符串的方法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++实现LeetCo...
    99+
    2023-06-20
  • C++实现字符串和整数的相互转换
    目录字符串转换整数方法1方法2(推荐)整数转换字符串字符串转换整数 方法1 #include <iostream> #include <typeinfo> ...
    99+
    2023-01-03
    C++字符串转整数 C++整数转字符串 C++ 整数 字符串
  • C语言实现将字符串转换成整数
    目录准备工作1.NULL指针2.空字符串3.空格4.正负号5.非法字符6.越界测试总结这是一个很有意思的问题。请不要把这个问题想的太简单了,考虑问题时应该尽可能的全面一些。请先思考并...
    99+
    2023-05-14
    C语言字符串转整数 C语言 字符串 整数
  • python实现字符串转换整数
    实现一个函数,使其能将字符串转换成整数。 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如...
    99+
    2023-01-30
    整数 字符串 python
  • C++实现将长整型数转换为字符串的示例代码
    C++实现将长整型数转换为字符串 #include <iostream> using namespace std; char *convertLongTo...
    99+
    2022-11-12
  • C++实现LeetCode(151.翻转字符串中的单词)
    [LeetCode] 151.Reverse Words in a String 翻转字符串中的单词 Given an input string, reverse the strin...
    99+
    2022-11-12
  • php字符串如何转换为整数
    这篇文章主要介绍php字符串如何转换为整数,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!php字符串转换为整数的方法:1、通过“(int)$foo”进行强制类型转换;2、通过“intval($foo)”函数进行转换;...
    99+
    2023-06-14
  • C++实现LeetCode(205.同构字符串)
    [LeetCode] 205. Isomorphic Strings 同构字符串 Given two strings s and t, determi...
    99+
    2022-11-12
  • C++实现LeetCode(43.字符串相乘)
    [LeetCode] 43. Multiply Strings 字符串相乘 Given two non-negative integers num1 and...
    99+
    2022-11-12
  • C++实现LeetCode(87.搅乱字符串)
    [LeetCode] 87. Scramble String 搅乱字符串 Given a string s1, we may represent it as a binar...
    99+
    2022-11-12
  • 怎么在C++中将长整型数转换为字符串
    这篇文章将为大家详细讲解有关怎么在C++中将长整型数转换为字符串,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。C++实现将长整型数转换为字符串#include <iostre...
    99+
    2023-06-14
  • C语言如何实现将字符串转换成整数
    本文小编为大家详细介绍“C语言如何实现将字符串转换成整数”,内容详细,步骤清晰,细节处理妥当,希望这篇“C语言如何实现将字符串转换成整数”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。这是一个很有意思的问题。请不要...
    99+
    2023-07-05
  • C#整数转二进制字符串方式
    目录C# 整数转二进制字符串C# Int to Binary StringC# 输入任意整数转成二进制总结C# 整数转二进制字符串 C# Int to Binary String 要...
    99+
    2023-02-26
    C#整数 C#二进制字符串 整数转二进制字符串
  • C#整数如何转二进制字符串
    这篇“C#整数如何转二进制字符串”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“C#整数如何转二进制字符串”文章吧。C# 整数...
    99+
    2023-07-05
  • vue如何将字符串转化为整数
    这篇文章主要介绍“vue如何将字符串转化为整数”,在日常操作中,相信很多人在vue如何将字符串转化为整数问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”vue如何将字符串转化为整数”的疑惑有所帮助!接下来,请跟...
    99+
    2023-07-06
  • C++实现LeetCode(12.整数转化成罗马数字)
    [LeetCode] 12. Integer to Roman 整数转化成罗马数字 Roman numerals are represented by seven different...
    99+
    2022-11-12
  • C++实现LeetCode(13.罗马数字转化成整数)
    [LeetCode] 13. Roman to Integer 罗马数字转化成整数 Roman numerals are represented by seven different...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作