Skip to content

Commit 4dcb6d7

Browse files
committed
1
1 parent b32a197 commit 4dcb6d7

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

notes/src/day2/lc977.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,33 @@ impl Solution {
9191
}
9292
```
9393

94-
注意是abs比较,a和b都是闭区间
94+
注意是abs比较,a和b都是闭区间
95+
96+
97+
```rust
98+
# struct Solution {}
99+
impl Solution {
100+
pub fn sorted_squares(nums: Vec<i32>) -> Vec<i32> {
101+
let mut res: Vec<i32> = vec![0i32; nums.len()];
102+
let mut idx: usize = 0usize;
103+
let mut left: usize = 0usize;
104+
let mut right: usize = nums.len() - 1usize;
105+
while idx < res.len() {
106+
let left_square: i32 = nums[left].pow(2u32);
107+
let right_square: i32 = nums[right].pow(2u32);
108+
use std::cmp::Ordering;
109+
match left_square.cmp(&right_square) {
110+
Ordering::Less => {
111+
right -= 1usize;
112+
res[nums.len() - 1usize - idx] = right_square;
113+
}
114+
_ => {
115+
left += 1usize;
116+
res[nums.len() - 1usize - idx] = left_square;
117+
}
118+
}
119+
idx += 1;
120+
}
121+
res
122+
}
123+
}

0 commit comments

Comments
 (0)