LeetCode 977. Squares of a Sorted Array (有序数组的平方)

题目标签:Array

  题目给了我们一组 从小到大的 integers,让我们平方数字 并且 也排序成 从小到达。

  因为有负数在里面,平方后,负数在array的位置会变动。

  可以设left 和 right pointers,从两边遍历,比较一下两个平方后的数字,把大的那个 放入新建的array的末尾。

  具体看code。

Java Solution:

Runtime beats 100.00%

完成日期:02/03/2019

关键点:two pointers

 1 class Solution
 2 {
 3     public int[] sortedSquares(int[] A)
 4     {
 5         int left = 0;
 6         int right = A.length - 1;
 7         int[] result = new int[A.length];
 8         int inputIndex = right;
 9
10         while(left <= right)
11         {
12             int leftInt = A[left] * A[left];
13             int rightInt = A[right] * A[right];
14
15             if(leftInt > rightInt)
16             {
17                 result[inputIndex] = leftInt;
18                 left++;
19                 inputIndex--;
20             }
21             else
22             {
23                 result[inputIndex] = rightInt;
24                 right--;
25                 inputIndex--;
26             }
27         }
28         return result;
29     }
30 }

参考资料:N/A

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

原文地址:https://www.cnblogs.com/jimmycheng/p/10468691.html

时间: 2024-11-08 10:04:01

LeetCode 977. Squares of a Sorted Array (有序数组的平方)的相关文章

LeetCode 977. Squares of a Sorted Array

977. Squares of a Sorted Array(有序数组的平方) 题目: 给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序. 示例 1: 输入:[-4,-1,0,3,10] 输出:[0,1,9,16,100] 示例 2: 输入:[-7,-3,2,3,11] 输出:[4,9,9,49,121] 提示: 1 <= A.length <= 10000 -10000 <= A[i] <= 10000 A 已按非递减顺序排序. 思路

LeetCode 977 Squares of a Sorted Array 解题报告

题目要求 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. 题目分析及思路 题目给出一个整数数组,已经按照非减顺序排列好了.要求返回一个数组,包含的元素是已知数组元素的平方,且按照非减顺序排列.可以使用列表的sort()函数进行排序. python代码? cl

【Leetcode_easy】977. Squares of a Sorted Array

problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSquares(vector<int>& A) { for(auto &a:A) a *=a; sort(A.begin(), A.end()); return A; } }; 参考1. Leetcode_easy_977. Squares of a Sorted Array; 完 原

leetCode 88. Merge Sorted Array 有序数组

88. Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The n

[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A = [

leetCode 26.Remove Duplicates from Sorted Array(删除数组重复点) 解题思路和方法

Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory.

540 Single Element in a Sorted Array 有序数组中的单一元素

给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数.示例 1:输入: [1,1,2,3,3,4,4,8,8]输出: 2 示例 2:输入: [3,3,7,7,10,11,11]输出: 10注意: 您的方案应该在 O(log n)时间复杂度和 O(1)空间复杂度中运行.详见:https://leetcode.com/problems/single-element-in-a-sorted-array/description/ C++: class Solution

【leetcode】977. Squares of a Sorted Array

题目如下: 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

[leetcode] 80 Remove Duplicates from Sorted Array II(数组下标操作)

因为这道题目的题意是要求我们在原数组上进行操作,所以操作变得稍微复杂了些,否则直接使用map最为简单. 基本思想是记录两个指针,一个是当前数组,另一个是目的数组,注意如果发现重复数超过2,那么目的数组的cur就要阻塞, 直到不同的出现后再赋值前进. class Solution { public: int removeDuplicates(vector<int>& nums) { if(nums.size()==0) return 0; int cur=1; //修改后数组的下标点 i