pub fn reverse_integer(x: i32) -> i32 {
reverse_s1(x)
}
fn reverse_s1(x: i32) -> i32 {
let mut temp_stack: Vec<u8> = vec![];
for ch in x.to_string().as_bytes() {
if *ch != 45 {
temp_stack.push(*ch);
}
}
loop {
match temp_stack.last() {
Some(last_ch) => {
if *last_ch == 48 && temp_stack.len() > 1 {
temp_stack.pop();
} else {
break;
}
}
None => break,
}
}
temp_stack.reverse();
let mut overflow_at_u8: [u8; 10] = [50, 49, 52, 55, 52, 56, 51, 54, 52, 55];
let mut target: Vec<u8> = vec![];
if x < 0 {
target.push('-' as u8);
overflow_at_u8 = [50, 49, 52, 55, 52, 56, 51, 54, 52, 56];
}
if temp_stack.len() >= overflow_at_u8.len() {
for idx in 0..overflow_at_u8.len() {
if temp_stack[idx] < overflow_at_u8[idx] {
break;
}
if temp_stack[idx] > overflow_at_u8[idx] {
return 0;
}
}
}
target = [target, temp_stack].concat();
String::from_utf8(target).unwrap().parse::<i32>().unwrap()
}