Datawhale|LeetCodeTencent Task02(lc7,8,9)

007 Reverse Integer(E)

Description

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

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

题意即将输入的整数按位进行反转,需要考虑给定环境下溢出情况的处理。

Analysis & Solution

思路1:可以考虑将整数转换为字符串,将字符串反转后转回整数。此方法需要花费额外的空间存储字符串。
思路2:可以直接在整数上进行操作,循环中每次取出当前 x x x的末位数: p o p = x % 10 pop = x \% 10 pop=x%10,然后令 x = x / 10 x=x/10 x=x/10,直至 x x x为0终止循环。
Caution
本题需要注意的就是对溢出情况的处理,每次求当前循环中结果 r e s = r e s ∗ 10 + p o p res=res*10+pop res=res10+pop 之前就要预先判断该结果是否溢出,否则程序运行可能出现异常。
判断溢出的方法如下:
上溢出:

res > INT_MAX / 10 || (res == INT_MAX / 10 && pop > 7)

下溢出:

res < INT_MIN / 10 || (res == INT_MIN / 10 && pop < -8)

判断条件中7和8的来由是int32的上界2147483647末位为7,下界-2147483648末尾为8。

Code

int reverse(int x)
{
    int pop = 0, res = 0;
    while(x != 0)
    {
        pop = x % 10;
        x = x / 10;
        if(res > INT_MAX / 10 || (res == INT_MAX / 10 && pop > 7)) return 0;
        if(res < INT_MIN / 10 || (res == INT_MIN / 10 && pop < -8)) return 0;
        res = res * 10 + pop;      
    }
    return res;
}

008 String to Integer(M)

Description

Implement atoi which converts a string to an integer.

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.

Note:

Only the space character ’ ’ is considered a whitespace character.
Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, 231 − 1 or −231 is returned.
Constraints:

0 <= s.length <= 200
s consists of English letters (lower-case and upper-case), digits, ’ ', ‘+’, ‘-’ and ‘.’

题意为将一个可能包含字母,数组,空格,’+/-/.’ 字符的字符串中的数字提取出来(其前不能出现字母,否则输出0),转换为整数输出。

Analysis & Solution

这道题的思路比较直接,不过牵涉到较多细节,需考虑以下几种字符的处理:
1.空格:去除字符串前面的所有空格。
2.+/-:遇到’-‘要记录,输出时乘以-1,遇到’+'则跳过。
3.字母:若在遇到数字前,遇到字母,则直接返回0。
4.数字:将遇到的整数按十进制相加,同样需要注意溢出问题,这里若溢出,则直接输出INT的上界或下界。溢出判定参考上一题007。
另外leetcode官方给出了基于状态机的解法,大家感兴趣的可以去学习。

Code

int atoi(string s)
{
    int i = 0, res = 0;
    int flag = 1;
    int len = s.length();
    while(s[i] == ' ' && i < len) i++;
    if(s[i] == '-')
    {
        flag = -1;
        i++;
    }
    else if(s[i] == '+') i++;
    while(isdigit(s[i]) && i < len)
    {
        int digit = int(s[i] - '0');
        if(res > INT_MAX / 10 || (res == INT_MAX / 10 && digit > 7))
            return flag > 0 ? INT_MAX : INT_MIN;
        res = res * 10 + digit; 
        i++;
    }
    return flag * res;

009 Palindrome Number

Description

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Follow up: Could you solve it without converting the integer to a string?

题意为判断输入的整数是否是回文数(正读和倒读相同)。

Analysis & Solution

思路1:可以联想到005最长回文子串,当然这里没有那么复杂。我们可以将整数转换为字符串,用双指针从两端向中间遍历,若元素一直相同,即为回文字符串。
思路2:题目的Follow up让我们是否能不转为字符串解决。我们可以简化问题,判断字符串的左半部分和右半部分的反转是否相同,反转我们可以直接用007中的方法实现。更进一步,具体实现时,可以一面从 x x x的末位更新反转字符串 r e v e r t revert revert,一面将输入 x = x / 10 x=x/10 x=x/10。当 x ≤ r e v e r t x\leq revert xrevert时,要么 x x x位数和 r e v e r t revert revert相等;要么 r e v e r t revert revert的位数比 x x x大1(这时去除 r e v e r t revert revert的末位),若二者相等,即可判断输入为回文数。
Caution
对于特殊情况,我们需要单独处理:
(1)输入为负数,直接判断不是回文数
(2)输入不是0,但末位为0,直接判断不是回文数。(输入无前置0)
另外,采用思路2时,我们 x x x r e v e r t revert revert都一直小于输入的长度,因此不会出现溢出的问题,简省了我们的判断。

Code

bool isPalindrome(int x)
{
    if(x < 0 || (x % 10 == 0 && x != 0)) return false; //x is negtive || last digit of x is 0 yet x is not 0
    int revert = 0;
    while(x > revert)
    {
        revert = revert * 10 + x % 10;
        x = x / 10;
    }
    return x == revert || x == revert / 10; //2 situations considering length of x is even or odd
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值