LeetCode开心刷题第四天——7逆序8字符转数字

7 Reverse Integer

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Special Point:

PS:真正好的代码,果真是简洁又明晰的

1.对于溢出的考察

本身不溢出的数,经过逆序后可能溢出,所以要进行判断

2.对于逆序的考察

逆序的处理十分干净利落

res保存逆序后的数,初始值为0,每次都需要乘10再加新的值。每次比较是否比INT_MAX最大值除10的值大,这样如果大了,再乘10一定也会大,所以直接报溢出0

res=res*10+x%10

x/=10

可以以后作为小模块用。

其实如果数目过大,就需要用字符串表示或者变换,但是这个题目自己限定了溢出直接用0代替,这就是这道题的简单之处

class Solution {
public:
    int reverse(int x) {
        int res = 0;
        while (x != 0) {
            if (abs(res) > INT_MAX / 10) return 0;
            res = res * 10 + x % 10;
            x /= 10;
        }
        return res;
    }
};

在贴出答案的同时,OJ 还提了一个问题 To check for overflow/underflow, we could check if ret > 214748364 or ret < –214748364 before multiplying by 10. On the other hand, we do not need to check if ret == 214748364, why? (214748364 即为 INT_MAX / 10)

为什么不用 check 是否等于 214748364 呢,因为输入的x也是一个整型数,所以x的范围也应该在 -2147483648~2147483647 之间,那么x的第一位只能是1或者2,翻转之后 res 的最后一位只能是1或2,所以 res 只能是 2147483641 或 2147483642 都在 int 的范围内。但是它们对应的x为 1463847412 和 2463847412,后者超出了数值范围。所以当过程中 res 等于 214748364 时, 输入的x只能为 1463847412, 翻转后的结果为 2147483641,都在正确的范围内,所以不用 check。

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

题目翻译:

实现atoi,将字符串转换为整数。
提示:仔细考虑所有可能的输入情况。如果你想要挑战,请不要看下面,并问自己有哪些可能的输入情况。

注:这个问题有意模糊说明(即没有给定输入说明)。你负责预先收集所有的输入需求。

atoi的需求:
该函数首先丢弃尽可能多的空白(whitespace )字符直到遇到一个非空白字符。从这个字符开始,有一个可选的初始加号或减号,后面是尽可能多的数字,把它们当成数值解释。

该字符串在形成整数的字符后面可以包含额外字符,它们会被忽略,对这个函数的行为没有影响。

如果str中的第一个非空白字符序列不是一个有效的整数,或者如果这样的序列不存在(str是空的或只包含空白字符),不执行任何转换。

如果没有执行任何有效的转换,则返回0。如果正确的值超出了可表示的值的范围,返回INT_MAX(2147483647)或INT_MIN(-2147483648)。

分析:
        注意考虑特殊情况。

 

Special Point:

这是个medium的题,其实每个题都有hard的潜质,只是题目好心限制了一些条件,比如这个

只用管第一个是不是正负号,后面的数字照单全收,而不是则可以一竿子打死,这是十分便利的

比起上面特殊点就在于,这是个字符串,所以可能处理溢出和之前不一样。上面由于输入的是int所以判断不需要有等号的情况,但这个不同

class Solution {
public:
    int myAtoi(string str) {
        if(str.empty()) return 0;
        int base=0,i=0,sign=1,n=str.size();
        while(i<n&&str[i]==' ') i++;
        if(i<n&&(str[i]=='+'||str[i]=='-'))
            //注意这是在顺序处理字符串,所以每判断完一个要++
            sign=(str[i++]=='+')?1:-1;
        while(i<n&&str[i]>='0'&&str[i]<='9')
        {
            if(base>INT_MAX/10||(base==INT_MAX/10&&str[i]-'0'>7))
                return (sign==1)?INT_MAX:INT_MIN;

            base=10*base+(str[i++]-'0');
        }
        return base*sign;
    }
};

 

转载于:https://www.cnblogs.com/Marigolci/p/11007360.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值