LeetCode 977. Squares of a Sorted Array

Problem Statement


impl Solution {
pub fn sorted_squares(a: Vec<i32>) -> Vec<i32> {
let mut ans = vec![];
let (mut i, mut j) = (0, a.len() - 1);
while i <= j {
let (s, e) = (a[i].pow(2), a[j].pow(2));
if s > e {
ans.push(s); i += 1;
} else {
ans.push(e);
if j == 0 {
break;
}
j -= 1;
}
}
ans.reverse();
return ans;
}
}