zoukankan      html  css  js  c++  java
  • [LeetCode]面试题67. 把字符串转换成整数

    题目

    写一个函数 StrToInt,实现把字符串转换成整数这个功能。不能使用 atoi 或者其他类似的库函数。

    首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。

    当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。

    该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。

    注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。

    在任何情况下,若函数不能进行有效的转换时,请返回 0。

    说明:

    假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231,  231 − 1]。如果数值超过这个范围,请返回  INT_MAX (231 − 1) 或 INT_MIN (−231) 。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/ba-zi-fu-chuan-zhuan-huan-cheng-zheng-shu-lcof
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    题解

    注意各种特殊情况的判断。并且尽量简洁。

    代码

    class Solution {
        public int myAtoi(String str) {
            if(str==null||str.length()==0){
                return 0;
            }
    
            int sign=1;
            int i=0;
            int len=str.length();
            while(i<len&&str.charAt(i)==' '){
                i++;
            }
            if(i<len&&str.charAt(i)=='-'){
                sign=-1;
                i++;
            }else if(i<len&&str.charAt(i)=='+'){
                i++;
            }
    
            long ans=0;
            for(;i<len;++i){
                if(str.charAt(i)<'0'||str.charAt(i)>'9'){
                    return sign*(int)ans;
                }
                ans=ans*10+(str.charAt(i)-'0');
                if(ans>Integer.MAX_VALUE){
                    return sign==1?Integer.MAX_VALUE:Integer.MIN_VALUE;
                }
            }
    
            return sign*(int)ans;
        }
    }
    
  • 相关阅读:
    topcoder srm 445 div1
    topcoder srm 440 div1
    topcoder srm 435 div1
    topcoder srm 430 div1
    topcoder srm 400 div1
    topcoder srm 380 div1
    topcoder srm 370 div1
    topcoder srm 425 div1
    WKWebView强大的新特性
    Runtime那些事
  • 原文地址:https://www.cnblogs.com/coding-gaga/p/12890475.html
Copyright © 2011-2022 走看看