-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_0556_next_greater_element_iii.rs
More file actions
49 lines (46 loc) · 1.55 KB
/
_0556_next_greater_element_iii.rs
File metadata and controls
49 lines (46 loc) · 1.55 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
struct Solution;
impl Solution {
pub fn next_greater_element(n: i32) -> i32 {
let mut num: i32 = n.clone();
let mut last: i32 = num % 10;
let mut h_array: Vec<i32> = vec![last];
num /= 10;
while num > 0 {
let mut current = num % 10;
if last <= current {
h_array.push(current);
} else {
let h_array_len: usize = h_array.len();
let mut num_replace: i32 = h_array[h_array_len - 1];
let mut index_replace: usize = h_array_len - 1;
for i in 0..h_array_len {
if h_array[i] > current && h_array[i] <= num_replace {
num_replace = h_array[i].clone();
index_replace = i;
break;
}
}
h_array[index_replace] = current;
current = num_replace;
num = num / 10 * 10 + current;
for v in h_array.iter() {
if num as i64 * 10 + *v as i64 > std::i32::MAX as i64 {
return -1;
} else {
num = num * 10 + v;
}
}
return num;
}
last = current;
num /= 10;
}
-1
}
}
#[test]
fn test() {
assert_eq!(Solution::next_greater_element(12), 21);
assert_eq!(Solution::next_greater_element(21), -1);
assert_eq!(Solution::next_greater_element(1999999999), -1);
}