Skip to content

Commit 922fd46

Browse files
committed
2119. A Number After a Double Reversal: AC
1 parent 53768f5 commit 922fd46

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,3 +1599,4 @@ mod s2114_maximum_number_of_words_found_in_sentences;
15991599
mod s2115_find_all_possible_recipes_from_given_supplies;
16001600
mod s2116_check_if_a_parentheses_string_can_be_valid;
16011601
mod s2117_abbreviating_the_product_of_a_range;
1602+
mod s2119_a_number_after_a_double_reversal;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* [2119] A Number After a Double Reversal
3+
*
4+
* Reversing an integer means to reverse all its digits.
5+
*
6+
* For example, reversing 2021 gives 1202. Reversing 12300 gives 321 as the leading zeros are not retained.
7+
*
8+
* Given an integer num, reverse num to get reversed1, then reverse reversed1 to get reversed2. Return true if reversed2 equals num. Otherwise return false.
9+
*
10+
* Example 1:
11+
*
12+
* Input: num = 526
13+
* Output: true
14+
* Explanation: Reverse num to get 625, then reverse 625 to get 526, which equals num.
15+
*
16+
* Example 2:
17+
*
18+
* Input: num = 1800
19+
* Output: false
20+
* Explanation: Reverse num to get 81, then reverse 81 to get 18, which does not equal num.
21+
*
22+
* Example 3:
23+
*
24+
* Input: num = 0
25+
* Output: true
26+
* Explanation: Reverse num to get 0, then reverse 0 to get 0, which equals num.
27+
*
28+
*
29+
* Constraints:
30+
*
31+
* 0 <= num <= 10^6
32+
*
33+
*/
34+
pub struct Solution {}
35+
36+
// problem: https://leetcode.com/problems/a-number-after-a-double-reversal/
37+
// discuss: https://leetcode.com/problems/a-number-after-a-double-reversal/discuss/?currentPage=1&orderBy=most_votes&query=
38+
39+
// submission codes start here
40+
41+
impl Solution {
42+
pub fn is_same_after_reversals(num: i32) -> bool {
43+
num == 0 || num % 10 != 0
44+
}
45+
}
46+
47+
// submission codes end
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use super::*;
52+
53+
#[test]
54+
fn test_2119_example_1() {
55+
let num = 526;
56+
57+
let result = true;
58+
59+
assert_eq!(Solution::is_same_after_reversals(num), result);
60+
}
61+
62+
#[test]
63+
fn test_2119_example_2() {
64+
let num = 1800;
65+
66+
let result = false;
67+
68+
assert_eq!(Solution::is_same_after_reversals(num), result);
69+
}
70+
71+
#[test]
72+
fn test_2119_example_3() {
73+
let num = 0;
74+
75+
let result = true;
76+
77+
assert_eq!(Solution::is_same_after_reversals(num), result);
78+
}
79+
}

0 commit comments

Comments
 (0)