-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_integer.rs
More file actions
56 lines (47 loc) · 1.66 KB
/
reverse_integer.rs
File metadata and controls
56 lines (47 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Given a signed 32-bit integer x, return x with its digits reversed.
If reversing x causes the value to go outside the signed 32-bit
integer range [-231, 231 - 1], then return 0.
**Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
*/
/*
Naive approach for the reverse integer problem.
1. Convert the integer to a string.
2. Reverse the string.
3. Convert the reversed string back to an integer.
4. Handle the sign of the integer.
5. If the reversed integer is out of bounds, return 0.
*/
pub fn naive_reverse_integer(x: i32) -> i32 {
// Convert the i32 to a string
let mut x_str: String = x.to_string();
// Check if the number is negative
let is_negative: bool = x < 0;
// If the number is negative, remove the negative sign
if is_negative {
x_str.remove(0);
}
// Invert the string
let reversed_str: String = x_str.chars().rev().collect();
// If the number is negative, add the negative sign back
let reversed_str: String = if is_negative {
format!("-{}", reversed_str)
} else {
reversed_str
};
/*
Try to convert the reversed string back to i32,
if it panics, return 0
*/
match reversed_str.parse::<i32>() {
Ok(reversed_int) => reversed_int,
Err(_) => 0,
}
}
// TODO: Implement these test cases in the project itself
// // Test the naive reverse integer solution
// let test_cases: Vec<i32> = vec![123, -123, 120, 0, 1534236469];
// for &test_case in &test_cases {
// let result = naive_reverse_integer(test_case);
// println!("Input: {}, Reversed: {}", test_case, result);
// }