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.   1 <= A.length <= 10000
    2.   -10000 <= A[i] <= 10000
    3.   A 已按非递减顺序排序。

思路:

  非递减顺序,再加上存在负数,可以通过在两端比较:先取head和tail表示两端的位置,比较两个数的平方,平方较大者,放到新数组的靠后位置。

  (真要偷懒可以直接用sort,但是这样就没啥意义了)

代码:

 1     public int[] sortedSquares(int[] A)
 2     {
 3         int len = A.length;
 4         int head = 0;
 5         int tail = len-1;
 6         int [] B = new int [len] ;
 7         while(head <= tail)
 8         {
 9             len--;
10             int i = A[head]*A[head];
11             int j = A[tail]*A[tail];
12             if(i>j)
13             {
14                 B[len] = i;
15                 head++;
16             }
17             else
18             {
19                 B[len] = j;
20                 tail--;
21             }
22         }
23         return B;
24     }

原文地址:https://www.cnblogs.com/blogxjc/p/10966177.html

时间: 2024-10-08 19:09:36

LeetCode 977. Squares of a Sorted Array的相关文章

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 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 {

【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】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】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

Java for LeetCode 081 Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 解题思路: 参考Java for LeetCode 033 Search in Rota

LeetCode Solutions : Search in Rotated Sorted Array II

Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. Java Solution ( refer to my blog LeetCode So

【Leetcode】Median of Two Sorted Array II

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 非常经典的一个问题,如果直接查找中位数,即偶数约定向下取整,奇数就是严格的中位数,这个题目直接可以比较每个数组的mid来计算得到. 但是这个问题的需求更改为如果为偶数,则中

[LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 026. Remove Duplicates from Sorted Array (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ 代码(github):https://github.com/ill