题目

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

答案

func sortedSquares(A []int) []int {
size := len(A)
res := make([]int, size)
for l, r, i := 0, size-1, size-1; l <= r; i-- {
if A[l]+A[r] < 0 {
res[i] = A[l] * A[l]
l++
} else {
res[i] = A[r] * A[r]
r--
}
}
return res
}

参考链接

977. Squares of a Sorted Array

最新文章

  1. Android中的AlertDialog使用示例一(警告对话框)
  2. MICAPS数据文件格式
  3. Linux 配置主机名
  4. Replication--复制延迟的诊断和解决
  5. 初始化httpclient的几种方式
  6. C puzzles详解【21-25题】
  7. hdu 1709 The Balance
  8. Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度
  9. Oracle存储过程的简单示例
  10. POJ 3602 Typographical Ligatures
  11. 编写React组件的最佳实践
  12. sgu438-The_Glorious_Karlutka_River
  13. 【Node.js】安装及使用
  14. Luogu P2261 [CQOI2007]余数求和
  15. 4.Spring中使用Log4j
  16. Netty源码分析第7章(编码器和写数据)----&gt;第3节: 写buffer队列
  17. memcached 学习笔记 3
  18. 第七周C语言代码
  19. float数据类型
  20. TensorFlow基础笔记(1) 数据读取与保存

热门文章

  1. POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离&lt;=K的点对数量
  2. IOI2019题解
  3. 前端微信小程序仿菜谱精灵
  4. 微信小程序 报错Failed to load image
  5. dedecms 模板文件不存在,无法解析文档的终极各种解决办法
  6. Echarts的简单入门
  7. mvn pom文件引用顺序关系
  8. [C++] namespace命名空间和using用法
  9. Linux_CentOS下搭建Nodejs 生产环境-以及nodejs进程管理器pm2的使用
  10. Laya改变文档结构后GameConfig自动生成错误问题