carloscn / structstudy

Leetcode daily trainning by using C/C++/RUST programming.
4 stars 1 forks source link

leetcode2119: A Number After a Double Reversal #342

Open carloscn opened 1 year ago

carloscn commented 1 year ago

Description

Reversing an integer means to reverse all its digits.

For example, reversing 2021 gives 1202. Reversing 12300 gives 321 as the leading zeros are not retained. Given an integer num, reverse num to get reversed1, then reverse reversed1 to get reversed2. Return true if reversed2 equals num. Otherwise return false.

Example 1:

Input: num = 526 Output: true Explanation: Reverse num to get 625, then reverse 625 to get 526, which equals num.

Example 2:

Input: num = 1800 Output: false Explanation: Reverse num to get 81, then reverse 81 to get 18, which does not equal num.

Example 3:

Input: num = 0 Output: true Explanation: Reverse num to get 0, then reverse 0 to get 0, which equals num.

Constraints:

0 <= num <= 106

carloscn commented 1 year ago

Analysis

bool is_same_after_reversals(int32_t num)
{
    return (num % 10 != 0) || (num < 9);
}
carloscn commented 1 year ago

Code

https://review.gerrithub.io/c/carloscn/structstudy/+/1168850 https://github.com/carloscn/structstudy/commit/4c7d077ad64c04a36e4d35339af86a97af28e7eb