7. Reverse Integer

简介: 7. Reverse Integer

7. Reverse Integer


知识点

1.字符串倒置

#include <cstring>
#include <algorithm>
int main()
{
      string str1;
      cin>>str1;
      cout<<"原始字符串是:"<<str1<<endl;
      reverse(str1.begin(),str1.end());
      cout<<"转换后的字符串是:"<<str1<<endl;
      return 0;
}


2.string转int

string num;
int tmp = stoi(num);


错误示范

class Solution
{
public:
    int reverse(int x)
    {
        while (x % 10 == 0  &&x)
        {
            x /= 10;
        }
        if (x<-pow(2,31)||x>pow(2,31)-1)
        {
            return 0;
        }
        string num = to_string(x);
        std::reverse(num.begin(), num.end());
        char tmp = *(num.end() - 1);
        cout<<x;
        x = stoi(num);
        if (tmp=='-')
        {
            return -1*x;
        }
        return x;
    }
};


这种解法的失败之处在于先转字符串,再字符串转整型时会有溢出的问题,题目说了整数超过32位的有符号整数的范围 [−231, 231 − 1],就返回0。所以要考虑输入1534236469在范围内,可倒置后9646324351就溢出了。


1.白痴解法

class Solution
{
public:
    int reverse(int x)
    {
        cout << x << endl;
        while (x % 10 == 0 && x)
        {
            x /= 10;
        }
        if (x < -pow(2, 31) || x > pow(2, 31) - 1)
        {
            return 0;
        }
        string num = to_string(x);
        std::reverse(num.begin(), num.end());
        cout << "num:" << num << endl;
        char tmp = *(num.end() - 1);
        long int y = atol(num.c_str());
        cout << y;
        if (y < -pow(2, 31) || y > pow(2, 31) - 1)
        {
            return 0;
        }
        if (tmp == '-')
        {
            return -1 * y;
        }
        return y;
    }
};


将string转换成long类型,不齿的通过了。解法过于暴力。


进阶解法

int reverse(int x) 
{
    long result = 0;
    while(x != 0)
    {
        result = result*10 + x % 10;
        x /= 10;
    }
    return (result > INT_MAX || result < INT_MIN)? 0 : result;
}


这种解法通过数学方法倒置了数字,然后用long类型存储,判断用long类型存储的数字是否溢出。


标准解法

class Solution
{
public:
    int reverse(int x)
    {
        int rev = 0;
        while (x)
        {
            int pop = x % 10;
            if (rev > INT_MAX / 10 || rev == INT_MAX / 10 && pop > 7)
            {
                return 0;
            }
            if (rev < INT_MIN / 10 || rev == INT_MIN / 10 && pop < -8)
            {
                return 0;
            }
            x /= 10;
            rev = rev*10+pop;
        }
        return rev;
    }
};


32位有符号整数,最大2147483647,最小-2147483648

就是要注意判断条件,当没算最后一位时,该数大于INT_MAX/10或者最后一位大于INT_MAX范围,则判断为溢出。


感悟

要多思考数学方法,不要一碰到数字题目首先想到的是转字符串。

目录
相关文章
|
机器学习/深度学习 NoSQL
LeetCode 344. Reverse String
编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。
85 0
LeetCode 344. Reverse String
|
索引
LeetCode 345. Reverse Vowels of a String
编写一个函数,以字符串作为输入,反转该字符串中的元音字母。
79 0
LeetCode 345. Reverse Vowels of a String
LeetCode之Reverse Integer
LeetCode之Reverse Integer
96 0
LeetCode之Reverse String II
LeetCode之Reverse String II
108 0
LeetCode之Reverse String
LeetCode之Reverse String
95 0
|
Java
list set array map 排序问题
    While analyzing source code of a large number of open source Java projects, I found Java developers frequently sort in two ways.
1014 0
[LeetCode] Reverse Vowels of a String
Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = “hello”, return “holle”. Example 2: Given s = “leetcode”, return “leotcede”.
1248 0
LeetCode 345 Reverse Vowels of a String
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51315651 ...
743 0