`
cozilla
  • 浏览: 94010 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

[Leetcode] String to Interger (atoi)

 
阅读更多
String to Integer (atoi)Dec 27 '116739 / 30948

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.

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.

» Solve this problem

 

#define  INT_MAX (2147483647)
#define  INT_MIN (-2147483648)
class Solution {
public:
    int atoi(const char *str) {
        char c;
        int len = strlen(str);
        int num = 0;
        bool neg = false;
        bool start = true;
        int old;
        for (int i =0 ; i < len; i++) {
            c = str[i];
            if (start && isspace(c)) continue;
            if (!start && isspace(c)) break;
            if (start) {
                if (c == '-') { 
                    neg = true;
                    start =false;
                    continue;
                }
                else if (c == '+') {
                    neg = false;
                    start = false;
                    continue;
                }
                start = false;
            }
            if (c < '0' || c > '9') break;
            old = num;
            num = 10 * num + (c - '0');
            if ((num - (c - '0'))/ 10 != old) { 
                if (neg) return INT_MIN;
                else return INT_MAX;
            }
        }
        if (num < 0) {
            if (neg) return INT_MIN;
            else return INT_MAX;
        }
        if (neg) {
            return -num;
        }
        else return num;
    }
};

 

0
0
分享到:
评论

相关推荐

    LeetCode 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 do not see below and ask yourself what are the possible input ...

    c语言-leetcode 0008-atoi.zip

    c c语言_leetcode 0008_atoi.zip

    LeetCode Reverse Interger 题目解决方案

    在LeetCode上的"Reverse Integer"题目中,目标是反转一个32位有符号整数的数字。本题考察了基本的数学操作、字符串处理以及边界条件的处理。下面将详细解释这个问题及其解决方案。 首先,我们需要了解32位有符号...

    java-leetcode题解之008-String-to-Integer(atoi)

    java入门 java_leetcode题解之008_String_to_Integer(atoi)

    LeetCode Roman to Integer解决方案

    LeetCode Roman to Integer解决方案

    C语言-leetcode题解之08-string-to-integer-atoi.c

    该题目要求编写一个名为atoi的函数,即“ASCII to Integer”的缩写。这个函数需要能够将一个字符串转换为对应的整数值。在这个过程中,需要考虑的不仅仅是常规的转换逻辑,还要处理许多边缘情况,比如前导空格、正负...

    js-leetcode题解之8-string-to-integer-(atoi).js

    LeetCode第8题“字符串转换整数(atoi)”要求编写一个函数,根据给定的字符串进行处理,转换为相应的整数。这里提供的是一个具体的实现方法,这个方法需要考虑多种边界情况,包括字符串开头的空格、正负号、非法...

    LeetCode 刷题汇总1

    * 字符串到整数(atoi)(String to Integer (atoi)):将字符串转换为整数。 2. 字符串操作: * 最长的回文子串(Longest Palindromic Substring):找到字符串中最长的回文子串。 * ZigZag转换(ZigZag ...

    电脑怎样下载leetcode-LeetCode_To_GitHub:Python工具从LeetCode搜索您的解决方案并自动将它们传输到您的G

    LeetCode_To_GitHub Python 工具,用于从 LeetCode () 中搜索您的解决方案并自动将它们传输到您的 GitHub 帐户。 支持的操作 到目前为止,该工具仅支持一种登录类型,即 Google 登录。 此外,您只能自动下载“算法”...

    LeetCode最全代码

    * [String](https://github.com/kamyu104/LeetCode#string) * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue]...

    Reverse words in a string-leetcode

    Reverse words in a string-leetcode

    leetcode答案-LeetCode-String:LeetCode-字符串

    在LeetCode平台上,字符串(String)专题是编程爱好者和求职者经常练习的重要部分。这个专题涵盖了字符串处理的各种算法问题,旨在提升编程者对字符串操作的熟练度和理解。在这个压缩包中,"LeetCode-String-master"很...

    c语言-leetcode题解perform-string-shifts.c

    在这个特定的示例中,我们关注的是一个名为"perform-string-shifts.c"的C语言文件,该文件包含了一个解决特定LeetCode问题的代码实现。为深入理解其内容,我们将从以下几个方面进行探讨: 1. LeetCode平台与题库:...

    LeetCode题解(java语言实现).pdf

    * String to Integer (atoi):该题目要求将字符串转换成整数,实现方法使用了状态机算法。 * Merge Sorted Array:该题目要求合并两个排序数组,实现方法使用了迭代算法。 * Valid Parentheses:该题目要求检查括号...

    LeetCode 101 - A LeetCode Grinding Guide (C++ Version).rar

    《LeetCode 101 - A LeetCode Grinding Guide (C++ Version)》是一本专为C++程序员设计的深入解析LeetCode算法问题的指南。这本书采用彩色版,以直观的方式讲解了各种数据结构和算法,旨在帮助读者磨练编程技能,...

    leetcode中文版-LeetCode:LeetcodeC++/Java

    Integer(atoi) 字符串转整数 string 13 Roman to Integer 罗马数字转整数 number,string 14 Longest Common Prefix 最长公共前缀 string 16 3Sum Closest 最接近的三数之和 two pointers,array 21 Merge Two Sorted ...

    javalruleetcode-LeetCode:LeetCode算法问题

    java lru leetcode Fighting for a job! 记录找工作期间搬运的题,全部使用Java实现,本人C++鶸 1 ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...LeetCode ...to Integer LeetCode 6 Zi

    leetcode小白刷题-String-to-Integer-atoi-:来自https://leetcode.com/problems/st

    leetcode小白刷题字符串到整数 (atoi) 示例 1: Input: " 42 " Output: 42 示例 2: Input: " -42 " Output: - 42 Explanation: The first non-whitespace character is ' - ' , which is the minus sign. Then take...

    leetcode分类-Leetcode:cpp中的Leetcode解决方案(已解决424个)

    leetcode 分类 Leetcode 总结 (updating) # Title Solution 1 Two ...用unordered_map,降至O(n) ...一个变量存储进位,当l1,l2,进位非均为空时,继续计算 ...注意要保证n1长度小于n2,因为...String to Integer (atoi) 9 Palind

    leetcode全套解答python版本

    《Python版LeetCode题解全集详解》 LeetCode是一个广受欢迎的在线编程挑战平台,致力于帮助程序员提升技能,特别是面试准备。这个压缩包“lc-all-solutions-master”包含了使用Python语言解决LeetCode所有问题的...

Global site tag (gtag.js) - Google Analytics