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. 1 <= A.length <= 10000
  2. -10000 <= A[i] <= 10000
  3. A is sorted in non-decreasing order.

代码:

class Solution {
public:
vector<int> sortedSquares(vector<int>& A) {
vector<int> ans;
int n = A.size();
//sort(A.begin(), A.end());
for(int i = 0; i < n; i ++) {
ans.push_back(A[i] * A[i]);
}
sort(ans.begin(), ans.end());
return ans;
}
};

  200!今日任务完成

 

#Leetcode# 977. Squares of a Sorted Array的更多相关文章

  1. 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 ...

  2. LeetCode 977. Squares of a Sorted Array (有序数组的平方)

    题目标签:Array 题目给了我们一组 从小到大的 integers,让我们平方数字 并且 也排序成 从小到达. 因为有负数在里面,平方后,负数在array的位置会变动. 可以设left 和 righ ...

  3. 【Leetcode_easy】977. Squares of a Sorted Array

    problem 977. Squares of a Sorted Array solution: class Solution { public: vector<int> sortedSq ...

  4. 【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 ...

  5. 【LeetCode】977. Squares of a Sorted Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  6. 977. Squares of a Sorted Array

    题目描述: Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...

  7. 977. Squares of a Sorted Array有序数组的平方

    网址:https://leetcode.com/problems/squares-of-a-sorted-array/ 双指针法 把左端的元素和右端的元素比较后挑出绝对值大的,将其平方放入ans中,并 ...

  8. LeetCode977.Squares of a Sorted Array

    题目 977. Squares of a Sorted Array Given an array of integers A sorted in non-decreasing order, retur ...

  9. [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...

随机推荐

  1. HDU 2865 Birthday Toy

    题目链接 题意:n个小珠子组成的正n边形,中间有一个大珠子.有木棍相连的两个珠子不能有相同的颜色,旋转后相同视为相同的方案,求着色方案数. \(\\\) 先选定一种颜色放在中间,剩下的\(k-1\)种 ...

  2. 模板题Pollard_Rho大数分解 A - Prime Test POJ - 1811

    题意:是素数就输出Prime,不是就输出最小因子. #include <cstdio> #include<time.h> #include <algorithm> ...

  3. cmd 监控网络状况

    提示:如果提示curl不是内部命令,请自行百度 windows 安装curl @echo off color 1f title 正在监控 echo 正在监控http://ioscheck.duapp. ...

  4. mysql 创建 mb4 字符集数据库

    create database sina default character set utf8mb4 collate utf8mb4_unicode_ci; show variables like ' ...

  5. M100 对频

  6. day10,11-Python 基本数据类型介绍之数字与字符串(看看就好)

    数字:int #字符串转换整型 a = "123" print(type(a),a) b = int(a) print(type(b),b) b = b + 1000 print( ...

  7. poi导出excel,表头数据动态拼装

    /* * 第一步:拼装表头和数据 */ // 放多个sheet的集合 List<Map<String,Object>> datas = new ArrayList<Map ...

  8. ESP8266开发综合篇(LUA开发-视频教程总揽)

    为了解决基础教程简单入门但不实用,项目方案非常实用但比较难的问题,开始推出8266开发综合篇 综合篇涉及到AT,LUA,SDK,LUA(sdk)开发,LUA和SDK开发会同步进行,后期再整理AT指令的 ...

  9. sprintf()函数用法

    sprintf()用法见操作手册:http://www.php.net/sprintf 简单写下format的用法: 1. + - 符号,数字 2. 填充字符 默认是空格,可以是0.如果其他字符填充, ...

  10. javascript闭包的使用--按钮切换

    闭包实现按钮状态切换 看下面的代码: var toggleBtn = document.getElementById('toggle'); var toggleFun = (function() { ...