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 ...
随机推荐
- 在JavaScript中"+"什么时候是链接符号,什么时候是加法运算?
二元加法运算符“+”在两个操作数都是数字或都是字符串时,计算结果是显而易见的.加号“+”的转换规则优先考虑字符串连接,如果其中一个操作数是字符串或者转换为字符串的对象,另外一个操作数会转换为字符串,加 ...
- Unity项目 - 捡苹果 Apple Picker
项目展示 Github项目地址:Apple Picker 涉及知识 正投视图 3D场景内树与苹果的图层 记录最高分到本地 准备工作 模型制作: 基本模型创建 树叶:sphere 拉伸为椭圆形,绿色材质 ...
- golang——常用内建函数
(1)func len(v Type) int 返回长度,取决于具体类型:字符串返回字节数:channel返回缓存元素的个数: (2)func cap(v Type) int 返回容量,取决于具体类型 ...
- 数据库得到too many connections”错误信息
查进程 show processlist删除进程 kill ID查完整sql show full processlist; 连数据库 MySQL -S /tmp/mysql.sock 或 ...
- ZOJ1969-Hard to Believe, but True!
import re while True: x = raw_input() if(x == '0+0=0'): print 'True' break a,b,c = re.split('[+=]', ...
- android:autoLink
android:autoLink 显示URL链接 TextView识别链接的方式有两种,一种是自动识别链接和HTML解析链接 1)自动识别链接 <!-- android:autoLink=&qu ...
- 328 Odd Even Linked List 奇偶链表
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- C#内容格式刷html 转成txt
/// <summary> /// 内容格式刷 /// </summary> /// <param name="strHtml">要格式的文本& ...
- 回收maven私仓过期垃圾
login->scheduled tasks->add
- Java 中 父类变量访问子类方法 需要使用 类型转换 (instenceof)关键字 /类型判断/
通过数组元素访问方法的时候只能访问在 Animal中定义的方法,对 于 Tiger类和 Fish中定义的方法时却不能调用,例如语句 animal[2].swim();就是不正确的.当 需要访问这些 ...