Leet code problem 7: reverse integer digit

class Solution {
public:
    int reverse(int x) {
        int ret = 0;
    int int_max_divide_10 = INT_MAX / 10;
    int int_max_mod_10 = INT_MAX % 10;
    int int_min_divide_10 = INT_MIN / 10;
    int int_min_mod_10 = INT_MIN % 10;
    while (x != 0){
        int remainder =  x % 10;
        if (x > 0 && (ret > int_max_divide_10 ||
                (ret == int_max_divide_10 && remainder > int_max_mod_10))
                ||
                x < 0 && (ret < int_min_divide_10 ||
                    (ret == int_min_divide_10 && remainder < int_min_mod_10)))
        {
            return 0;
        }
        ret *= 10;
        ret += remainder;
        x /= 10;
    }
        return ret;
    }
};
时间: 2024-08-24 22:32:01

Leet code problem 7: reverse integer digit的相关文章

【Leet Code】String to Integer (atoi) ——常考类型题

String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions 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 yours

Leet code problem 5 Longest Palindromic Substring

class Solution { public: string longestPalindrome(string s) { int x = -1; int l = -1; for (int i = 0; i < (int)s.size(); ++i){ int left = i; int right = i; while (left >= 0 && right < s.size() && s[left] == s[right]){ --left; ++ri

Leet Code OJ 344. Reverse String [Difficulty: Easy]

题目: Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". 翻译: 写一个函数,使用字符串作为输入,返回它反转后的结果. 例如,输入"hello",返回"olleh". 分析: 转为字符数组后,将第一个字符和最后一个字符对调,第二个字符

【Leet Code】Reverse Integer——“%”你真的懂吗?

Reverse Integer Total Accepted: 27372 Total Submissions: 68133My Submissions Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 题目咋看起来很简单,其实,真的很简单: class Solution { public: int reverse(int x) { int ret = 0; bo

#Leet Code# Evaluate Reverse Polish Notation

描述:计算逆波兰表达法的结果 Sample: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 使用stack实现: 1 def is_op

Leetcode 题目整理-2 Reverse Integer &amp;&amp; String to Integer

今天的两道题关于基本数据类型的探讨,估计也是要考虑各种情况,要细致学习 7. Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if

[LeetCode][JavaScript]Reverse Integer

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

LeetCode之“数学”:Reverse Integer &amp;&amp; Reverse Bits

1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if

65. Reverse Integer &amp;&amp; Palindrome Number

Reverse Integer Reverse digits of an integer. Example1: x =  123, return  321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alr