网址:https://leetcode.com/problems/squares-of-a-sorted-array/

双指针法

把左端的元素和右端的元素比较后挑出绝对值大的,将其平方放入ans中,并且将指针往中间移动

不断循环上述过程,直至两指针重合。注意处理重合位置

最后将 ans 通过 reverse 反转一下就可以了

class Solution {
public:
vector<int> sortedSquares(vector<int>& A) {
int i = , j = A.size()-;
vector<int> ans;
while(i != j)
{
if(abs(A[i]) < abs(A[j]))
{
ans.push_back(A[j]*A[j]);
j--;
}
else
{
ans.push_back(A[i]*A[i]);
i++;
}
}
ans.push_back(A[i]*A[i]);
reverse(ans.begin(), ans.end());
return ans;
}
};

977. Squares of a Sorted Array有序数组的平方的更多相关文章

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

    题目标签:Array 题目给了我们一组 从小到大的 integers,让我们平方数字 并且 也排序成 从小到达. 因为有负数在里面,平方后,负数在array的位置会变动. 可以设left 和 righ ...

  2. 【Leetcode_easy】977. Squares of a Sorted Array

    problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...

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

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  4. [LeetCode] Single Element in a Sorted Array 有序数组中的单独元素

    Given a sorted array consisting of only integers where every element appears twice except for one el ...

  5. [leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

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

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

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

    给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数.示例 1:输入: [1,1,2,3,3,4,4,8,8]输出: 2 示例 2:输入: [3,3,7,7,10,1 ...

  8. 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 ...

  9. 977. Squares of a Sorted Array

    题目描述: Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...

随机推荐

  1. Linux服务器可以进百度,但是进阿里云或者别的一些网站提示‘错误代码:NS_ERROR_NET_INADEQUATE_SECURITY’的问题

    昨天遇到一个头疼的事情,在阿里云买了一台服务器: 然后环境各种都装了,因为本人是小白,所以一般都装MATE界面: 一开始环境没配好,访问百度可以进去,进万网但是进不去,先也没急着搞这个事情,第一天晚上 ...

  2. 一、iOS开发环境搭建

    前置条件 1. 必要:一台装有Mac OS X操作系统的电脑:经济允许的话可以买一部Mac book:否则的话,可以试试黑苹果或虚拟机. 2.必要:一个有可用的Apple ID:免费,在Apple的官 ...

  3. node(03 fs文件模块)

    我们通过读取一个文件内容并将内容写入到另外一个文件中. fs.createWriteStream 写入文件其实 这个可以看文档 参考这个更全一店 https://www.jianshu.com/p/d ...

  4. CFRunLoop 源码学习笔记(CF-1151.16)

    1.CFRunLoopModeRef 什么时候创建的? 在调用__CFRunLoopFindMode(rl, modeName, create) 1.1)首先通过modeName 在RunLoop 中 ...

  5. 查看 chrome 浏览器中的 Headers

    查看 chrome 浏览器中的 Headers, Response 信息:

  6. HSV to RGB

    HSV构成: Hue : the color type (red, blue, or yellow) Ranges from 0 to 360° Saturation : the intensity ...

  7. percona-toolkit工具的使用

    percona-toolkit是一组高级命令行工具的集合,可以查看当前服务的摘要信息,磁盘检测,分析慢查询日志,查找重复索引,实现表同步等等 percona-toolkit 源自 Maatkit 和 ...

  8. Mysql模糊查询like效率,以及更高效的写法

    在使用msyql进行模糊查询的时候,很自然的会用到like语句,通常情况下,在数据量小的时候,不容易看出查询的效率,但在数据量达到百万级,千万级的时候,查询的效率就很容易显现出来.这个时候查询的效率就 ...

  9. 像素与DPI之间的关系

    先说像素.像素是电子图像组成的基本单位,将图像放大数倍,会发现图像是由一个个“小色块”紧密排列组成的,每一个“小色块”就是一个像素点. 也就是说,每个图像都是由n多个像素点组成. 再说分辨率.所谓分辨 ...

  10. Eclipse进度条出现“Remote System Explorer Operation”解决方法

    Eclipse进度条出现“Remote System Explorer Operation”解决方法