977. Squares of a Sorted Array
题目描述:
Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order. Example : Input: [-,-,,,]
Output: [,,,,]
Example : Input: [-,-,,,]
Output: [,,,,] Note: <= A.length <=
- <= A[i] <=
A is sorted in non-decreasing order.
思路:将数组中每个元素平方之后,最大的值肯定在数组的两侧,因此从数组两侧向中间查找即可。
参考代码:
#include <vector>
#include <iostream> using namespace std; class Solution {
public:
vector<int> sortedSquares(vector<int>& A) {
vector<int> answer(A.size(), );
int l = , r = A.size() - ;
int left, right; while (l <= r)
{
left = abs(A[l]);
right = abs(A[r]);
if (left < right)
{
answer[r - l] = right * right;
r -= ;
}
else
{
answer[r - l] = left * left;
l += ;
}
} return answer;
}
}; int main()
{
int a[] = {-, -, , , };
Solution solu;
vector<int> vec_arr(a, a+);
vector<int> ret = solu.sortedSquares(vec_arr);
for (int i = ; i < ret.size(); ++i)
{
cout << ret[i] << " ";
} return ;
}
运行结果:
977. Squares of a Sorted Array的更多相关文章
- 【Leetcode_easy】977. Squares of a Sorted Array
problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...
- #Leetcode# 977. Squares of a Sorted Array
https://leetcode.com/problems/squares-of-a-sorted-array/ Given an array of integers A sorted in non- ...
- 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 ...
- 【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 ...
- 【LeetCode】977. Squares of a Sorted Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- LeetCode 977. Squares of a Sorted Array (有序数组的平方)
题目标签:Array 题目给了我们一组 从小到大的 integers,让我们平方数字 并且 也排序成 从小到达. 因为有负数在里面,平方后,负数在array的位置会变动. 可以设left 和 righ ...
- 977. Squares of a Sorted Array有序数组的平方
网址:https://leetcode.com/problems/squares-of-a-sorted-array/ 双指针法 把左端的元素和右端的元素比较后挑出绝对值大的,将其平方放入ans中,并 ...
- LeetCode977.Squares of a Sorted Array
题目 977. Squares of a Sorted Array Given an array of integers A sorted in non-decreasing order, retur ...
- [Swift]LeetCode977. 有序数组的平方 | Squares of a Sorted Array
Given an array of integers A sorted in non-decreasing order, return an array of the squares of each ...
随机推荐
- php 静态属性和静态变量
- 【知识总结】卡特兰数 (Catalan Number) 公式的推导
卡特兰数的英文维基讲得非常全面,强烈建议阅读! Catalan number - Wikipedia (本文中图片也来源于这个页面) 由于本人太菜,这里只选取其中两个公式进行总结. (似乎就是这两个比 ...
- html基础代码演示2
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 关于debug.keystore文件用法以及错误处理
在开发过程中需要频繁的为测试的同事签名apk,非常很麻烦,把默认debug.keystore文件替换成发布用(生产环境)的签名文件,不用频繁地签名apk文件了. 如果直接使用生产keysto ...
- jQuery——尺寸位置
获取宽:$(".box").width() 设置宽:$(".box").width(200) 获取高:$(".box").height() ...
- C#的一些知識點
不能將屬性以ref或out的方式傳遞 看上去屬性和字段差不多,可是屬性本質上是個方法,并不是真正指向一個内存位置,所以不能像字段那樣能以ref或out方式傳遞. Lookup運行一個鍵對應多個值,但無 ...
- Sandbox 沙盒
In computer security, a sandbox is a security mechanism for separating running programs, usually in ...
- Map 键值对 set get delete
- CAD把一个DWG文件中的多个图框一次性全部插入到打开的DWG文件中
主要用到函数说明: _DMxDrawX::InsertBlock 向控件数据库中插入一个图块,不用它插入匿名块,详细说明如下: 参数 说明 BSTR pszDwgFileName 图块定义的dwg 文 ...
- 运维是做什么的?史上最全互联网Linux工作规划!十分钟找到linux运维工程师职业方向!
首先祝贺你选择学习Linux,你可能即将踏上Linux的工作之旅,出发之前,让我带你来看一看关于Linux和Linux运维的一切. Linux因其高效率.易于裁剪.应用广等优势,成为了当今中高端服务器 ...