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 ...
随机推荐
- 解决Sublime Text 3 的 Package Control 启动失败问题
今天在使用Sublime Text的时候,需要了这样的情况 遇到这个问题的时候 我是这样解决的 一. 首先 找到 Package Control的下载地址1 下载地址2.将下载下 ...
- 数据库部署到linux服务器,供本地访问。
1. 将本地的sql文件上传至服务器 scp /Users/fangke/Documents/article.sql root@IP:/usr/local 2. 登陆服务器的mysql 3. 创建数 ...
- BZOJ 4530 LCT/线段树合并
//By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> using nam ...
- 修改docker-toolbox/boot2docker容器镜像
进入虚拟机 vi /var/lib/boot2docker/profile 编辑在EXTRA_ARGS,加入 --registry-mirror=https://pee6w651.mirror.ali ...
- 微信接口本地调试(IIS服务器)
1.下载ngrok,并注册获得token.官网下载地址:https://ngrok.com/ 如果你是在官网下载的,到后面映射域名的时候会要求购买他们的服务. 这里我们用一个国内免费的ngrok服务器 ...
- [Java]Java分层概念
service是业务层 action层即作为控制器 DAO (Data Access Object) 数据访问 1.JAVA中Action层, Service层 ,modle层 和 Dao层的功能 ...
- html5——全屏显示
基本概念 1.HTML5规范允许用户自定义网页上任一元素全屏显示 2.requestFullscreen() 开启全屏显示.cancleFullscreen() 关闭全屏显示 3.不同浏览器兼容性不一 ...
- oracle sql*loader的使用
用法: SQLLDR keyword=value [,keyword=value,...] 有效的关键字: userid -- ORACLE 用户名/口令 control -- 控制文件 ...
- 【译】x86程序员手册03 - 2.1内存组织和分段
2.1 Memory Organization and Segmentation 内存组织和分段 The physical memory of an 80386 system is organized ...
- 微信小程序php后台实现
这里简单介绍用php后台实现获取openid并保存到数据库: 微信的登陆流程是这样的 首先前端发送请求到服务器: wx.login({ success: function (res) { var co ...