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 ...
随机推荐
- 43-2-CAN协议
1.帧的种类 通信是通过以下 5 种类型的帧进行的. • 数据帧 • 遥控帧 • 错误帧 • 过载帧 • 帧间隔 另外, 数据帧和遥控帧有标准格式和扩展格式两种格式.标准格式有 11 个位的标识符(I ...
- 【托业】【新东方托业全真模拟】TEST05~06-----P5~6
credit A with B 把A归功于B present A with B 给A赠送B proofread thoroughly 彻底地校对:exclusively 专门地:独占地:apparen ...
- Centos不能上外网解决
检查路由 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0 ...
- Spring框架的第四天(整合ssh框架)
## Spring框架的第四天 ## ---------- **课程回顾:Spring框架第三天** 1. AOP注解方式 * 编写切面类(包含通知和切入点) * 开启自动代理 2. JDBC模板技术 ...
- [openjudge-搜索]单词接龙
题目描述 描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的"龙"(每个单词都最多在"龙&q ...
- 关于ajax返回数据处理
查看jquery文档,我们知道jquery有很多种Ajax调用方法,下面结合springmvc返回的数据,假设返回的是data ='{"label":"1",& ...
- spring boot 整合freemarker(好用!!!!)
springboot整合freemarker 1.pom依赖 <!-- 引入freeMarker的依赖包. --> <dependency> <groupId>or ...
- spring jar包依赖
- ORA-12801/ORA-12853: insufficient memory for PX buffers: current 274880K, max needed 19722240K/ORA-04031解决方法
近日,现场一台服务器在运行时出现下列异常: ORA-12801: error signaled in parallel query server P139 ORA-12853: insufficien ...
- centos7救援模式--rescue模式
前序 经典问题:系统无法进入,如grub损坏或某个配置文件改错 操作 1 按方向键到Boot,选到Hard Driver,按减号,使其下移,最终让CD-ROM Drive到第一行,并按F10保存 2 ...