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 ...
随机推荐
- selenium3 + python - action_chains源码分析
ActionChains简介 actionchains是selenium里面专门处理鼠标相关的操作如:鼠标移动,鼠标按钮操作,按键和上下文菜单(鼠标右键)交互.这对于做更复杂的动作非常有用,比如悬停和 ...
- hdu2027
http://acm.hdu.edu.cn/showproblem.php?pid=2027 #include<iostream> #include<stdio.h> #inc ...
- day02_12/12/2016_bean的实例化之构造器方式
- linux如何更改yum源
更改linux yum源方法:第一步:进入yum配置文件目录:cd /etc/yum.repos.d/第二步:备份配置文件(如果后续出现了问题就可以恢复):mv CentOS-Base.repo Ce ...
- IIS 503 错误
今天早上乘公交的时候,网站运维群里直接炸了,网站打不开,503错误.然后就各种@我,吓得我手机都要扔了,然后马不停蹄的赶往公司去查看错误. 我首先在IIS上浏览网页,想试图在服务器上显现出详细错误,这 ...
- java_基础知识_字符串练习题_计算两个字符串的最长公共字串长度
package tek; Java算法——求出两个字符串的最长公共字符串 /** * @Title: 问题:有两个字符串str1和str2,求出两个字符串中最长公共字符串. * @author 匹夫( ...
- Windows开发小问题集
ON_BN_KILLFOCUS无效,因为需要BS_NOTIFY
- Hive扩展功能(五)--HiveServer2服务高可用
软件环境: linux系统: CentOS6.7 Hadoop版本: 2.6.5 zookeeper版本: 3.4.8 主机配置: 一共m1, m2, m3这五部机, 每部主机的用户名都为centos ...
- Ajax——php基础知识(二)
header header('content-type:text/html; charset= utf-8');//设置编码格式为:utf-8 header('location:http://www. ...
- 【译】x86程序员手册02 - 基本的程序模式
Chapter 2 -- Basic Programming Model: 基本的程序模式 Introduces the models of memory organization. Defines ...