zoukankan      html  css  js  c++  java
  • LeetCode

    题目:

    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.

    思路:

    基本运算;注意判断乘和加的溢出。

    package manipulation;
    
    public class StringToInteger {
    
        public int myAtoi(String str) {
            int len;
            if (str == null || (len = str.length()) == 0) return 0;
            int pos = 0;
            int sign = 1;
            int res = 0;
            while (Character.isWhitespace(str.charAt(pos))) ++pos;
            if (str.charAt(pos) == '+') {
                ++pos;
            } else if (str.charAt(pos) == '-') {
                ++pos;
                sign = -1;
            }
            
            while (pos < len && Character.isDigit(str.charAt(pos))){
                int value = str.charAt(pos) - '0';
                int preRes = res;
    // We use Math.abs here because on the second time, res will become positive if sign == -1 res
    = Math.abs(res) * 10 * sign; if (res/(10 * sign) != Math.abs(preRes)) { return sign > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE; } preRes = res; res = res + sign * value; if (sign == 1 && res < preRes) return Integer.MAX_VALUE; if (sign == -1 && res > preRes) return Integer.MIN_VALUE; ++pos; } return res; } public static void main(String[] args) { // TODO Auto-generated method stub String str = "-123"; StringToInteger sti = new StringToInteger(); System.out.println(sti.myAtoi(str)); } }

    代码更简洁一些:

    package manipulation;
    
    public class StringToInteger {
    
        public int myAtoi(String str) {
            int len;
            if (str == null || (len = str.length()) == 0) return 0;
            int pos = 0;
            int sign = 1;
            int res = 0;
            while (Character.isWhitespace(str.charAt(pos))) ++pos;
            if (str.charAt(pos) == '+') {
                ++pos;
            } else if (str.charAt(pos) == '-') {
                ++pos;
                sign = -1;
            }
            
            while (pos < len && Character.isDigit(str.charAt(pos))) {
                int value = str.charAt(pos) - '0';
                if (sign == 1 && res > (Integer.MAX_VALUE - value) / 10) 
                        return Integer.MAX_VALUE;            
                
                if (sign == -1 && (-res < (Integer.MIN_VALUE + value) / 10)) 
                        return Integer.MIN_VALUE;            
            
                res = res * 10 + value;
                ++pos;
            }
            
            return sign > 0 ? res : -res;
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String str = "-2147483648";
            StringToInteger sti = new StringToInteger();
            System.out.println(sti.myAtoi(str));
            // -(INT_MIN_VALUE) is still INT_MIN_VALUE 
            int x = -2147483648;
            System.out.println(-x);
        }
    
    }
  • 相关阅读:
    GDI+ 实现透明水印和文字
    delphi调用LUA函数来处理一些逻辑
    Delphi 不使用自带模板创建服务
    Delphi在Listview中加入Edit控件
    中文转码器的工作原理_delphi教程
    使用钩子函数[6]
    简单全局HOOK拦截大部分键盘消息
    4个字节就相当于移动一位,原来指针是这样用的
    C#调用Delphi接口(ITest = interface)
    DELPHI 对象的本质 VMT
  • 原文地址:https://www.cnblogs.com/null00/p/5039120.html
Copyright © 2011-2022 走看看