LeetCode题解之Squares of a Sorted Array 1、题目描述 2、问题分析 使用过两个计数器。 3、代码 1 class Solution { 2 public: 3 vector<int> sortedSquares(vector<int>& A) { 4 int left = 0, right = A.size() - 1; 5 vector<int> res; 6 while (left <= right) { 7 if (abs(A[...
给你一个包括正数和负数的有序数组,让你返回每个数字的乘积组成的有序数组。 虽然是个easy的题目,我觉得还是用到技巧了的,值得记录一下。 二.思路 用了三个下标变量: 一个用来保存新的乘积; 另外两个用来从数组的开头和末尾 分别遍历。 代码: class Solution { public int[] sortedSquares(int[] A) { int...
Given an integer arraynumssorted innon-decreasingorder, returnan array ofthe squares of each numbersorted in non-decreasing order. Example 1: Input:nums = [-4,-1,0,3,10]Output:[0,1,9,16,100]Explanation:After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [...
Runtime:248 ms, faster than49.87% of Python3 online submissions for Squares of a Sorted Array. Memory Usage:15.7 MB, less than51.20% of Python3 online submissions for Squares of a Sorted Array. 为什么不能这样? class Solution: def sortedSquares(self, A: List[int]) -> List[int]: B = ...
Can you solve this real interview question? Squares of a Sorted Array - Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. Example 1: Input: nums = [-4,-1,0,3,10
Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order. Example 1:Input: [-4,-1,0,3,10] Output: [0,1,9,16,100] Example 2:Input: [-7,-3,2,3,11] Output: [4,9,9,49,121] Note: 1...
Squares of a Sorted Array 题:https://leetcode.com/problems/squares-of-a-sorted-array/ 题目大意 对于非递减数组A,对所有元素的平方进行排序。 思路 元素的平方 较大的元素 只可能是 A中 很小的(负数)或 A中较大的元素。 于是使用双 指针,指向A 的 首元素 与 末尾元素。 比较两者的绝对值。 讲较...
Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. Example 1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. ...
其他思路 假设有两个指针,分别从头和从尾向中间移动,然后比较左右两个值的绝对值大小,绝对值大的将平方和添加到最左边,一直到两个指针相遇。 class Solution:def sortedSquares(self, A):answer = [0] * len(A)l, r = 0, len(A) - 1while l <= r:left, right = abs(A[l]), abs(A[r])if ...
Given an array of integersAsorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order. Example 1: Input:[-4,-1,0,3,10] Output:[0,1,9,16,100] 1. 2. Example 2: Input:[-7,-3,2,3,11] ...