LeetCode日常刷题(3)

7. Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

click to show spoilers.

Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.

算法:

对于这样的一个整数,如果我们每次将数字从后边加上并在数值上乘10,那么最后的结果就是一个反转,而如果某次反转之后发生了溢出,那么必然会出现异常。

代码:

    public int reverse(int x) {
        int result = 0;
        while (x != 0) {
            int tail = x % 10;
            int newResult = result * 10 + tail;
            if ((newResult - tail) / 10 != result) {
                return 0;
            }
            result = newResult;
            x = x / 10;
        }
        return result;
    }

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

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.

这个函数在第一个非空白字符出现可以丢弃尽量多的空白字符。然后从这个非空白字符开始,获取一个初始的加号或者减号,然后之后的数字字符会被转成一个数。

这个字符串在形成一个数之后可以包含其他字符,函数应该不受他们影响并忽略它们。

如果第一个非空白字符不是一个合法的数字字符,或者这个序列不存在即为空或者全为空白字符,不进行任何转换。

如果没有进行转换,函数应该返回0值。如果正确的数值越界了,应该返回2147483647或者-2147483648。

下边是一些实例:

InputExpected
b1231231231230
+-20
-0012a43-12
+0 1230
1231231231231232147483647

这样的测试用例给出来,相信各位大佬也知道自己为什么走远了?

走远了?

首先需要对整个字符串进行瘦身,前边没有意义的0和空白都是需要剪掉的,然后在开始转换。

代码:


    public static String loseWeight(String str){
        if (str == null || str.length() == 0){
            return null;
        }
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) != ' ' && str.charAt(i) != '0'){
                return str.substring(i);
            }
        }
        return null;
    }
    public static int myAtoi(String str) {
        String newStr = loseWeight(str);
        if (newStr == null){
            return 0;
        }
        final int UP_BOUND = 2147483647;
        final int DOWN_BOUND = -2147483648;
        Boolean hasSigned = null;
        int result = 0;
        for (int i = 0; i < newStr.length(); i++) {
            char ch = newStr.charAt(i);
            if (ch == '+' || ch == '-'){
                if (hasSigned == null){
                    hasSigned = ch == '+' ? Boolean.TRUE : Boolean.FALSE;
                }else{
                    return 0;
                }
            }else if (ch >= '0' && ch <= '9'){
                int tail = ch - '0';
                int newResult = result * 10 + tail;
                if ((newResult - tail) / 10 != result || newResult % 10 != tail){
                    return (hasSigned == null || hasSigned) ? UP_BOUND : DOWN_BOUND;
                }
                result = newResult;
            }else{
                return (hasSigned == null || hasSigned) ? result : -result;
            }
        }
        return (hasSigned == null || hasSigned) ? result : -result;
    }

9. Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

这个回文简单的爆炸,但是这个怎么尽可能的使用少的辅助空间,我这里只是缩小到了1个整型

代码:

    public boolean isPalindrome(int x) {
        if (x < 0 || x != 0 && x % 10 == 0){
            return false;
        }
        int reverse = 0;
        while (x > reverse){
            reverse = reverse * 10 + x % 10;
            x = x/ 10;
        }
        return (x == reverse || x == reverse / 10);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值