网址: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. Apache 2.4.27外网访问403(Forbidden)错误

    httpd.conf <Directory /> AllowOverride none #Require all denied 注释这句 Allow from all Require al ...

  2. Echarts-图表根据值的不同展示成不同的颜色

    series : [        {            name:'直接访问',            type:'bar',            barWidth: '60%',      ...

  3. 3.1.2 Spring之IoC

    二.Spring之IoC 1. IoC与DI (1) IoC 控制反转( IoC, Inversion of Control) , 是一个概念, 是一种思想. 控制反转就是对对象控制权的转移, 从程序 ...

  4. SQL SERVER 备份脚本

    DECLARE @FileName VARCHAR(200), @CurrentTime VARCHAR(50), @DBName VARCHAR(100), @SQL VARCHAR(1000)SE ...

  5. PHP----------php-fpm进程数的一些相关配置

    1.其中pm.max_children如何配置:pm.max_children 数量的多少根据机器内存确定,基本上一个进程需要30M的内存,假设起100个进程,那么就是3000M,3G内存. 2.pm ...

  6. js对象属性名驼峰式转下划线

    一.题目示例: 思路: 1.匹配属性名字符串中的大写字母和数字 2.通过匹配后的lastIndex属性获取匹配到的大写字母和数字的位置 3.判断大写字母的位置是否为首位置以及lastIndex是否为0 ...

  7. ReportViewe调用Reporting Services报表时报错Session超时

    序列化问题加上[Serializable]即可 /// <summary> /// 报表身份授权重写 /// </summary> [Serializable] public ...

  8. Android-Gradle(一)

    理解基本的Gradle 如果你想创建一个Android project基于gradle,那么你必须写一个构建脚本,这个文件通常称之为build.grade,你可能已经觉察到了,当我们查看这一脚本,gr ...

  9. [转载]要提高SQL查询效率where语句条件的先后次序应如何写

    出处:https://www.cnblogs.com/exe19/p/5786806.html 我们要做到不但会写SQL,还要做到写出性能优良的SQL语句. (1)选择最有效率的表名顺序(只在基于规则 ...

  10. 记账本微信小程序开发六

    记账本微信小程序开发六 我的界面 主界面