977. Squares of a Sorted Array有序数组的平方
网址: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有序数组的平方的更多相关文章
- LeetCode 977. Squares of a Sorted Array (有序数组的平方)
题目标签:Array 题目给了我们一组 从小到大的 integers,让我们平方数字 并且 也排序成 从小到达. 因为有负数在里面,平方后,负数在array的位置会变动. 可以设left 和 righ ...
- 【Leetcode_easy】977. Squares of a Sorted Array
problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [LeetCode] Single Element in a Sorted Array 有序数组中的单独元素
Given a sorted array consisting of only integers where every element appears twice except for one el ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 977. Squares of a Sorted Array
题目描述: Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...
随机推荐
- fat32转ntfs命令
1.打开电脑左下角的 “开始” 菜单2.鼠标左键单机 “运行”3.弹出横框输入 cmd 后,确定4.弹出黑框输入 convert E:/FS:NTFS 然后回车5.提示输入盘符名,也就是你输入D盘的名 ...
- linux 进程间通信——内存共享映射mmap和munmap
IPC三种通信机制是指:信号量.共享内存.消息队列, 信号量:通过操作系统中的PV操作来实现: 共享内存:申请一块内存,进程A往共享内存中写,其他的进程就可以通过读出共享内存中的内容来获取进程A所 ...
- 大牛推荐的30本经典编程书籍,从Python到前端全系列。
注:为了方便阅读与收藏,我们也制作了30本书籍完整清单的Markdown.PDF版以及思维导图版,大家可以在实验楼公众号后台回复关键字"书籍推荐"获取. Python 系列(10本 ...
- linux安装lamp/lamp/lanmp
wdcp安装lamp/lanp/lanmp 和宝塔(centOS)1. yum install -y wget //yum安装wegt2. wget http://dl.wdlinux.cn/fil ...
- Spring框架的第四天(整合ssh框架)
## Spring框架的第四天 ## ---------- **课程回顾:Spring框架第三天** 1. AOP注解方式 * 编写切面类(包含通知和切入点) * 开启自动代理 2. JDBC模板技术 ...
- Python之socket_tcp
1.1socket编程之tcp编程 """ socket类型 sock_stream 面向连接的流套接字,默认值 tcp协议 sock_dgram 无连接的数据报文套接字 ...
- [完美]原生JS获取浏览器版本判断--支持Edge,IE,Chrome,Firefox,Opera,Safari,以及各种使用Chrome和IE混合内核的浏览器
截至自2017-08-11,支持现世已出的几乎所有PC端浏览器版本判断. 受支持的PC端浏览器列表: Edge IE Chrome Firefox Opera Safari QQ浏览器 360系列浏览 ...
- Python数据分析Pandas库数据结构(一)
pandas数据结构 1.生成一维矩阵模拟数据 import pandas as pdimport numpy as nps = pd.Series([1,2,3,4,np.nan,9,9])s2 = ...
- MyBatis注解方式批量插入操作
@Insert({ "<script>", "insert into table_name(column1, column2, column3) values ...
- 【数据使用】3k水稻数据库现成SNP的使用
---恢复内容开始--- 我们经常说幻想着使用已有数据发表高分文章,的确,这样的童话故事每天都在发生,但如何走出第一步我们很多小伙伴不清楚,那么我们就从水稻SNP数据库的使用来讲起. http://s ...