网址: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. [js]js栈内存的全局/私有作用域,代码预解释

    js代码如何执行的 浏览器提供执行环境: 全局作用域(提供js执行环境, 栈内存) --> 执行js需要预解释 - 带var : 提前声明 - 带function关键字的: 提前声明+定义 js ...

  2. 133A

    #include <stdio.h> #include<string.h> #include <stdbool.h> #define MAXSIZE 105 int ...

  3. InternalError (see above for traceback): Blas GEMM launch failed

    训练BiLSTM模型的时候报错: InternalError (see above for traceback): Blas GEMM launch failed : a.shape=(32, 200 ...

  4. IdentityServer4授权和认证集成Identity和profile

    identiyt的使用可以看之前的文章:https://www.cnblogs.com/nsky/p/10323415.html 之前的ids4授权服务器都是用的in-men方式把数据添加到内存, 现 ...

  5. kali蓝牙渗透

    1.hcitool 通过前面讲的升级操作后,在BackTrack4 Linux或者Ubuntu系统下将会安装好蓝牙的全套操作工具,其中就包括hcitool.该工具支持大量的蓝牙设备操作,从扫描到查看设 ...

  6. 关于PChar(@string)的疑惑

    看到一篇博客关于讲Delphi中MOVE的例子,心生疑惑.记录下自己的实验,虽然我也不知道这是啥... program Project1; {$APPTYPE CONSOLE} uses SysUti ...

  7. 0004-20180422-自动化第五章-python基础学习笔记

    内容回顾:1.数据类型 2.for和while循环 continue break #如下循环将怎么打印结果? for i in range(1,10): print(i) for i in range ...

  8. Linux 查看磁盘读写速度IO使用情况

    # 查看io进程 命令:iotop 注:DISK TEAD:n=磁盘读/每秒              DISK WRITE:n=磁盘写/每秒. 注:标黄的可查看磁盘的读写速率,下面可以看到使用的io ...

  9. dataguard从库数据库丢失恢复例子(模拟丢失数据文件)

    准备工作,使用如下脚本进行数据库的全备份[oracle@localhost ~]$ more rman_backup.sh #!/bin/sh RMAN_OUTPUT_LOG=/home/oracle ...

  10. shell的交互式和非交互式登录

    工作中经常碰见环境变量加载问题,归根结底就是配置文件的加载问题. 一般会有四种模式:交互式登陆.非交互式登陆.交互式非登陆.非交互非登陆. 交互式和非交互式对环境变量的加载: +----------- ...