977. Squares of a Sorted Array Easy

@problem@discussion
#Array#Two Pointers#Sorting



1/**
2 * [977] Squares of a Sorted Array
3 *
4 * Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
5 *  
6 * Example 1:
7 * 
8 * Input: nums = [-4,-1,0,3,10]
9 * Output: [0,1,9,16,100]
10 * Explanation: After squaring, the array becomes [16,1,0,9,100].
11 * After sorting, it becomes [0,1,9,16,100].
12 * 
13 * Example 2:
14 * 
15 * Input: nums = [-7,-3,2,3,11]
16 * Output: [4,9,9,49,121]
17 * 
18 *  
19 * Constraints:
20 * 
21 * 	<span>1 <= nums.length <= </span>10^4
22 * 	-10^4 <= nums[i] <= 10^4
23 * 	nums is sorted in non-decreasing order.
24 * 
25 *  
26 * Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?
27 */
28pub struct Solution {}
29
30// problem: https://leetcode.com/problems/squares-of-a-sorted-array/
31// discuss: https://leetcode.com/problems/squares-of-a-sorted-array/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35impl Solution {
36    pub fn sorted_squares(nums: Vec<i32>) -> Vec<i32> {
37        vec![]
38    }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn test_977() {
49    }
50}
51


Back
© 2025 bowen.ge All Rights Reserved.