LeetCode977. 有序数组的平方
问题:977. 有序数组的平方
给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序。
示例 1:
输入:[-4,-1,0,3,10]
输出:[0,1,9,16,100]
示例 2:
输入:[-7,-3,2,3,11]
输出:[4,9,9,49,121]
提示:
1 <= A.length <= 10000-10000 <= A[i] <= 10000A已按非递减顺序排序。
链接:https://leetcode-cn.com/contest/weekly-contest-120/problems/squares-of-a-sorted-array/
分析:
签到性质的题吧,计算平方,排序即可。
或者将源数据按照绝对值重新排序,计算平方。
AC Code:
class Solution {
public:
vector<int> sortedSquares(vector<int>& A) {
vector<int> ret;
for (int i = ; i < A.size(); i++)
{
ret.emplace_back(A[i] * A[i]);
}
sort(ret.begin(), ret.end());
return ret;
}
};
其他:
现在好像国服和国际服分别排名了,learn from the best,好在可以看到英文版的排名和答题,后面第一采用英文版的第一。
第一code:
typedef signed long long ll; #undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//------------------------------------------------------- class Solution {
public:
vector<int> sortedSquares(vector<int>& A) {
FORR(a,A) a=a*a;
sort(ALL(A));
return A;
}
};
LeetCode977. 有序数组的平方的更多相关文章
- leetcode-977. 有序数组的平方
leetcode-977. 有序数组的平方 (来自 120周赛) 题意 给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序. 示例 1: 输入:[-4,-1 ...
- [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 ...
- leetcode之有序数组的平方
题目描述: 给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序. 示例 1: 输入:[-4,-1,0,3,10] 输出:[0,1,9,16,100] 示例 ...
- 代码随想录第二天| 977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II
2022/09/22 第二天 第一题 这题我就直接平方后排序了,很无脑但很快乐啊(官方题解是双指针 第二题 滑动窗口的问题,本来我也是直接暴力求解发现在leetCode上超时,看了官方题解,也是第一次 ...
- 977. Squares of a Sorted Array有序数组的平方
网址:https://leetcode.com/problems/squares-of-a-sorted-array/ 双指针法 把左端的元素和右端的元素比较后挑出绝对值大的,将其平方放入ans中,并 ...
- LeetCode 977. Squares of a Sorted Array (有序数组的平方)
题目标签:Array 题目给了我们一组 从小到大的 integers,让我们平方数字 并且 也排序成 从小到达. 因为有负数在里面,平方后,负数在array的位置会变动. 可以设left 和 righ ...
- 有序数组每个数平方后,不同数字的个数?O(n)
此乃一道笔试题,当时的确也做出来啦.(但是在细节上还是出错啦,对多次重复出现的数字可能会重复计数,没有记录上次删除的元素) 如题,有序数组,可以知道平方之后在两边的数据较大,中间的数据较小. 因此可以 ...
- [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
随机推荐
- PHP高级分离术
<!--高级分离术--> <table border='1px'> <?php foreach($data as $key=>$value){ ...
- HDU 4081—— Qin Shi Huang's National Road System——————【次小生成树、prim】
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- Oracle错误——ORA-03113:通信通道的文件结尾
请参考:http://blog.csdn.net/zwk626542417/article/details/39667999 今天跟往常一样,登陆PL/SQL,确登陆失败,出现一个错误“ORA-010 ...
- web子项目的路径问题
http://baidu.com/userms/ userms是一个子应用程序,项目中使用的路径 /content/css/.. 从http://baidu.com/ 开始 ~/content/ ...
- 合并Gridview单元格
Introduction There are a lot of methods in the Internet solving the problem of how to merge GridView ...
- poj 1155 输入输出问题
http://acm.hust.edu.cn/vjudge/problem/16417 重做了一遍poj 1155 题目大意:给定一棵树,1为根结点表示电视台,有m个叶子节点表示客户,有n-m-1个中 ...
- css中的伪元素,我今天记住了!o~yeah
对于伪类和伪元素,我如果要区别它们,一般是使用css中的手册来区分,平常时候也没特意去记,需要用到时打开手册“哦,这个是伪元素,这个是伪类”,我个人觉的某些东西你把它存在网上,不一定要存在头脑中.带着 ...
- input type="file"获取文件名方法
文件上传比较丑,样式调整时会有一个获取文件名,或者包含文件路径的文件名的方法 html代码 <div class="file-box"> <form id=&qu ...
- position:fixed相对父级元素定位而不是浏览器
position:fixed默认是相对浏览器定位的 原理:fixed定位相对父级容器定位,不添加:top,bottom,left,right样式,通过margin定位 代码:http://jsbin. ...
- u-boot分析(十)----堆栈设置|代码拷贝|完成BL1阶段
u-boot分析(十) 上篇博文我们按照210的启动流程,分析到了初始化nand flash,由于接下来的关闭ABB比较简单所以跳过,所以我们今天按照u-boot的启动流程继续进行分析. 今天我们会用 ...