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 ...
随机推荐
- 基于docker的tomcat服务化
tomcat作为web容器被广泛应用,但作者所在的公司restful接口特别多,每个接口都需要一个tomcat来启动,为了配置隔离,一般都会把tomcat安装文件复制多遍,分别把war包部署在对应的w ...
- MySQL数据库笔记总结
MySQL数据库总结 一.数据库简介 1. 数据 所谓数据(Data)是指对客观事物进行描述并可以鉴别的符号,这些符号是可识别的.抽象的.它不仅仅指狭义上的数字,而是有多种表现形式:字母.文字.文本. ...
- 【CodeForces727E/CF727E】Games on a CD (字符串哈希)
题目: CodeForces727E 分析: 看到字符串比较,肯定想到哈希啊--现学的哈希,先丢两个重要的公式 (\(seed\)是大于字符集大小的质数,\(p\)是大质数) \[hash[i]=(h ...
- Django返回json给钱前台的方法
return HttpResponse(simplejson.dumps(resource.update_status, ensure_ascii=False))
- Application、Activity Stack 和 Task的区别
Application类 Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系 ...
- 涨知识III - 百度2016校园招聘——移动软件研发工程师
1.列关于线程调度的叙述中,错误的是(). 正确答案 :BE A调用线程的sleep()方法,可以使比当前线程优先级低的线程获得运行机会 B调用线程的yeild()方法,只会使与当前线程相同优先级的线 ...
- Elasticsearch之CURL命令的UPDATE
对于,Elasticsearch之CURL命令的UPDATE包括局部更新和全部更新.可以去看我写的另一篇博客. Elasticsearch之更新(全部更新和局部更新) 总结: ES全部更新,使用PUT ...
- 怎么用css hack处理各浏览器兼容IE6,IE7,IE8,IE9/ FF
第一:什么事浏览器兼容性 浏览器兼容性问题又被称为网页兼容性或网站兼容性问题,指网页在各种浏览器上的显示效果可能不一致而产生浏览器和网页间的兼容问题.在网站的设计和制作中,做好浏览器兼容,才能够让网站 ...
- ORA-02068,ORA-03135错误解决方法
今天查看了下ERP DB服务器 alter_<SID>.log日志,发现有个错误 Sat Sep 14 14:49:42 CST 2013 Error 2068 trapped in 2P ...
- Centos6.4 安装fail2ban防暴力破解
Centos6.4 安装fail2ban防暴力破解 一. 安装 curl -O https://codeload.github.com/fail2ban/fail2ban/tar.gz/0.9.0 m ...