【LeetCode】977. Squares of a Sorted Array 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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 1:
Input: [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Example 2:
Input: [-7,-3,2,3,11]
Output: [4,9,9,49,121]
Note:
- 1 <= A.length <= 10000
- -10000 <= A[i] <= 10000
- A is sorted in non-decreasing order.
题目大意
给了一个单调不减的数组,返回排序了的每个元素求平方的结果。
解题方法
排序
没错这个是Easy题目,不用想太多,直接对每个元素进行求平方并排序即可。
需要注意的是,求平方之后是否会超出int的最大值呢?还好题目给了A[i]的范围只有10000,那么平方是1个亿,不会超过int最大。所以是安全的。
数组总长度是10000,所以O(NlongN)的时间复杂度是完全OK的。
c++代码如下:
class Solution {
public:
vector<int> sortedSquares(vector<int>& A) {
vector<int> res;
for (int a : A) {
res.push_back(a * a);
}
sort(res.begin(), res.end());
return res;
}
};
日期
2019 年 1 月 20 日 —— 这次周赛有点简单
【LeetCode】977. Squares of a Sorted Array 解题报告(C++)的更多相关文章
- 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
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 (有序数组的平方)
题目标签:Array 题目给了我们一组 从小到大的 integers,让我们平方数字 并且 也排序成 从小到达. 因为有负数在里面,平方后,负数在array的位置会变动. 可以设left 和 righ ...
- 【Leetcode_easy】977. Squares of a Sorted Array
problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- LeetCode: Search in Rotated Sorted Array 解题报告
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...
随机推荐
- ggplot 画堆叠柱状图
1. 关注下方公众号可获得更多精彩
- halt
halt命令用来关闭正在运行的Linux操作系统.halt命令会先检测系统的runlevel,若runlevel为0或6,则关闭系统,否则即调用shutdown来关闭系统. 语法 halt(选项) 选 ...
- Python基础之字典内置方法
目录 1. 字典 1.1 字典的作用 1.2 创建和使用字典 1.2.1 dict类 1.2.2 基本的字典操作 1.2.3 字典方法 1. 字典 映射:可以通过名称来访问其各个值的数据结构. 字典是 ...
- 在Telegraf上报的监控数据中添加固定的标签列
Telegraf作为InfluxData提供的TICK工具栈(由Telegraf, InfluxDB, Chronograf, Kapacitor四个工具的首字母组成)中收集监控数据的一环,功能非常强 ...
- Erda 系列 Meetup「成都站」携手SOFAStack 和你聊聊云原生基础设施建设那点事儿
技术控快上车啦秋天的第一场活动一起来收获技术干货吧! 主题: 云原生基础设施建设的现在及未来时间: 2021 年 9 月 11 日 (周六) 13:30-17:00活动地点: 四川省成都市蚂蚁 C 空 ...
- Spark基础:(五)Spark编程进阶
共享变量 (1)累加器:是用来对信息进行聚合的,同时也是Spark中提供的一种分布式的变量机制,其原理类似于mapreduce,即分布式的改变,然后聚合这些改变.累加器的一个常见用途是在调试时对作业执 ...
- Node.js 概述
JavaScript 标准参考教程(alpha) 草稿二:Node.js Node.js 概述 GitHub TOP Node.js 概述 来自<JavaScript 标准参考教程(alpha) ...
- npm下载错误解决办法
解决办法:删除npmrc文件. 使用镜像 镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在):1.通过config命令12npm config set re ...
- js中!!的妙用
0.-0.null."".false.undefined 或者 NaN转化为false,其他为true
- 在 Qualys SSL Labs SSL 测试中获得 A+ 评级的秘技 2021 版
本系列文章将阐述主流应用交付控制器和主流 Web 服务器如何运行 HTTP/2 和 TLSv1.3 协议,以及如何在 SSL Test 中获得 A+ 评级. 请访问原文链接:https://sysin ...