`
frank-liu
  • 浏览: 1666012 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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 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.

 

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

 

问题分析

    相对比较简单的问题,将字符串转换成对应的整数。针对这个问题有一些情况需要考虑,很容易在实现的时候有遗漏。典型的来说有这么几种情况:

    1. 字符串开头有显示正负符号"+" "-" ,这种情况下对首字母要判断。

    2. 字符串开头结尾有空白字符,要将这些去掉。

    3. 字符串中间有非数字的符号。对于这种情况有值得商榷的地方。一种是返回一个目前能解析到的值,一种也可以是报错。

    4. 还有就是解析的数字太大,导致数字溢出了。比如大于或者整数所能表示的最大最小值,这个时候就返回对应的最大最小值就可以了。防止整数表示溢出,也可以采用类似的策略,用一个long类型来保存解析的结果。然后在一边解析的时候一边判断是否溢出。

    按照这个思路,可以得到如下的代码:

 

public class Solution {
    public int myAtoi(String str) {
        if(str == null || str.length() == 0) return 0;
        str = str.trim();
        char c = str.charAt(0);
        int flag = 1, start = 0;
        long result = 0;
        if(c == '-') {
            flag = -1;
            start++;
        } else if(c == '+') {
            start++;
        }
        for(int i = start; i < str.length(); i++) {
            char ch = str.charAt(i);
            if(!Character.isDigit(ch)) break;
            result = result * 10 + ch - '0';
            if(flag == 1 && result >= Integer.MAX_VALUE) return Integer.MAX_VALUE;
            if(flag == -1 && result * flag <= Integer.MIN_VALUE) return Integer.MIN_VALUE;
        }
        return (int)result * flag;
    }
}

 

总结

    这个问题在很多的面试情况下有问到。一个是它看起来并不复杂,几乎每个人都能想到一点思路。但是里面要考虑的细节比较多,很容易遗漏。所以对细节的全面考虑还是很重要的。 

分享到:
评论

相关推荐

    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 ...

    leetcode2sum-Problems:编程问题的回购

    leetcode 2sum # Programming-Problems This will have many problems from all over the web, As of ...LeetCode: ...Integer ...String To Integer Atoi [Medium] LC9: Palindrome Number [Easy] LC11:

    javalruleetcode-lxxcode:Leetcode或Lintcode的代码

    String to Integer (atoi) (java, javascript) leetcode 9: 回文数(java, javascript) leetcode 10:正则表达式匹配(java) leetcode 11:盛水的容器(java) leetcode 12:整数转罗马(java) leetcode 13:罗马到...

    约瑟夫环leetcode-LeetCode:力码

    leetcode LeetCode :pencil: 算法菜鸟的暴力做题记录 (143 / 1595) (不会算法,只会暴力求解) 经典算法记录 类型 算法 对应题目 寻找字符串中最长的回文串 Manacher算法 环形队列每次删去第m个元素 约瑟夫环问题

    leetcode卡-LeetCode:LeetCode题解

    Integer(atoi) :star: :star: :star: 注意细节,溢出 ---- strlen :star: :star: :star: const char,size_t类型 ---- strcpy :star: :star: :star: 字符串复制,返回值,赋值语句 0028 strStr :star: :star: :star:...

    leetcode中国-leetcode:leetcode刷题

    String to Integer (atoi) addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方图的最大面积,左右两次扫面+剪枝优化 Valid Parentheses 用栈判断括号匹配 Regular ...

    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 ...

    leetcode338-LeetCode:LeetCode刷题总结

    LeetCode刷题总结 1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7....

    leetcode双人赛-LeetCode:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...

    leetcode2sumc-leetcode:JavaScript版本leetcode中文版代码

    String to Integer (atoi) 中等 9 Palindrome Number 简单 11 Container With Most Water 中等 12 Integer to Roman 中等 13 Roman to Integer 简单 14 Longest Common Prefix 简单 15 3Sum 中等 16 3Sum Closest ...

    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答案-leetcode:leetcode问题解决方案

    leetcode 答案 Leetcode题解 leetcode题库的答案及解决思路,随着解题的深入,题解会不断改进时间复杂度和空间复杂度,因此每个题包含多个算法。 目录 Algorithms(算法) ...String to Integer (atoi) Medium com.bc

    leetcode答案-leetcode:leetcode

    leetcode 答案 leetcode leetcode exercises,algorithms part! TwoSum: 1.key point: unorderd_map(16ms), ...2.大概是前50%的样子,...String to Integer (atoi): 1.没有考虑到很多很多情况,比如溢出,字符串不是单纯的

    LeetCode判断字符串是否循环-leetcode:leetcode练习,算法部分!

    LeetCode判断字符串是否循环 leetcode leetcode exercises,algorithms part! TwoSum: 1.key point: unorderd_map(16ms), map(24ms) ...String to Integer (atoi): 1.没有考虑到很多很多情况,比如溢出,字符

    leetcode答案-LeetCode:重做一遍LeetCode

    Integer(atoi)](中)这个问题懒得想了 9.【回文数】(易) 20181030完成 10.【正则表达式匹配】(难)不理解 11.【盛水的容器】(中) 20181030成品 12.【整数转罗马】(中)太难回答 13.【Roman To Integer】(中) ...

    lrucacheleetcode-oh-my-leetcode:Leetcode题解

    题:**String to Integer (atoi) 实现 atoi 将字符串转换为整数。 **第 0004 题:**反转位 反转给定 32 位无符号整数的位。 例如,给定输入 43261596(以二进制表示为 00000010100101000001111010011100),返回 ...

    javalruleetcode-leetcode:leetcode问题的解决方案

    0008_String_to_Integer_atoi 0009_Palindrome_Number 0010_Regular_Expression_Matching 0011_Container_With_Most_Water 0012_Integer_to_Roman 0013_Roman_to_Integer 0014_Longest_Common_Prefix 0015_3总和 ...

    LeetCode判断字符串是否循环-LeetCode:LeetCode问题的Javascript解决方案

    LeetCode判断字符串是否循环 LeetCode Javascript Solutions to problems on LintCode # question difficulty 1 Two Sum Easy 2 Add Two Numbers Medium 3 Longest Substring Without Repeating Characters Medium 4...

    leetcode数组下标大于间距-leetcode:leetcode刷一遍

    leetcode数组下标大于间距 5. Longest Palindromic Substring 这里使用menecher方法,就是动态规划,首先在原先的字符串之间插入'#, 这样可以统一处理奇数串和偶数串, 使用两个变量纪录状态, far_pos表示最长回文...

    leetcode-String-to-Integer-atoi

    leetcode字符串转换为整数

Global site tag (gtag.js) - Google Analytics