zoukankan      html  css  js  c++  java
  • 8. 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 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.

    public class Solution {
        public int myAtoi(String str) {
            isNeg = false;
            if(!isValidStr(str)) return 0;
            
            long result = 0;
            for(;pStr < str.length(); pStr++){
                if(str.charAt(pStr) > '9' || str.charAt(pStr) < '0') break;
                
                result = 10*result + str.charAt(pStr)-'0';
                if(!isNeg && result >= java.lang.Integer.MAX_VALUE) return java.lang.Integer.MAX_VALUE;
                else if(isNeg && result > java.lang.Integer.MAX_VALUE) return java.lang.Integer.MIN_VALUE;
            }
            if(isNeg) result = 0-result;
            return (int)result;
        }
        
        private boolean isValidStr(String s){
            for(pStr = 0; pStr < s.length(); pStr++){
                if(s.charAt(pStr) == ' ') continue;
                
                if(s.charAt(pStr) == '+') {
                    isNeg = false;
                    pStr++;
                }
                else if(s.charAt(pStr) == '-'){
                    isNeg = true;
                    pStr++;
                }
                else if(s.charAt(pStr) > '9' || s.charAt(pStr) < '0') return false;
                break;
            }
            if(pStr>= s.length() || s.charAt(pStr) > '9' || s.charAt(pStr) < '0') return false;
            else return true;
            
        }
        
        //Java除了类的数据成员,其他都是按值传递,没有指针,没有引用
        //所以这里需要用类成员在两个函数间传递数据
        private int pStr;
        private boolean isNeg;
    }
  • 相关阅读:
    4种方法帮你解决IntelliJ IDEA控制台中文乱码问题
    万字长文:解读区块链7类共识算法
    CoralCache:一个提高微服务可用性的中间件
    探究Python源码,终于弄懂了字符串驻留技术
    OAuth:每次授权暗中保护你的那个“MAN”
    厉害了!这群95后正在用三维成像技术让科幻变成现实
    华为云FusionInsight MRS在金融行业存算分离的实践
    【新春特辑】发压岁钱、看贺岁片、AI写春联……华为云社区给大家拜年了
    Java实现 蓝桥杯 算法训练 天数计算
    WebRTC框架中的硬件加速
  • 原文地址:https://www.cnblogs.com/qionglouyuyu/p/5509944.html
Copyright © 2011-2022 走看看