-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0912-sort-an-array.rs
49 lines (47 loc) · 1.28 KB
/
0912-sort-an-array.rs
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
/*
* @lc app=leetcode id=912 lang=rust
*
* [912] Sort an Array
*/
struct Solution;
// @lc code=start
impl Solution {
pub fn sort_array(nums: Vec<i32>) -> Vec<i32> {
if nums.len() > 1 {
let (l, r) = nums.split_at(nums.len() / 2);
let sorted_r = Self::sort_array(r.to_vec());
let sorted_l = Self::sort_array(l.to_vec());
let mut res: Vec<i32> = nums.into();
let (mut i, mut j) = (0, 0);
let mut k = 0;
while i < sorted_l.len() && j < sorted_r.len() {
if sorted_l[i] <= sorted_r[j] {
res[k] = sorted_l[i].clone();
i += 1;
} else {
res[k] = sorted_r[j].clone();
j += 1;
}
k += 1;
}
while i < sorted_l.len() {
res[k] = sorted_l[i].clone();
i += 1;
k += 1
}
while j < sorted_r.len() {
res[k] = sorted_r[j].clone();
j += 1;
k += 1;
}
res
} else {
nums
}
}
}
// @lc code=end
fn main() {
let v = vec![-2,3,-5];
println!("{:?}", Solution::sort_array(v));
}