LeetCode977.Squares of a Sorted Array
2024-10-19 22:48:22
题目
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
最新文章
- Android中的AlertDialog使用示例一(警告对话框)
- MICAPS数据文件格式
- Linux 配置主机名
- Replication--复制延迟的诊断和解决
- 初始化httpclient的几种方式
- C puzzles详解【21-25题】
- hdu 1709 The Balance
- Codeforces Gym 100342D Problem D. Dinner Problem Dp+高精度
- Oracle存储过程的简单示例
- POJ 3602 Typographical Ligatures
- 编写React组件的最佳实践
- sgu438-The_Glorious_Karlutka_River
- 【Node.js】安装及使用
- Luogu P2261 [CQOI2007]余数求和
- 4.Spring中使用Log4j
- Netty源码分析第7章(编码器和写数据)---->;第3节: 写buffer队列
- memcached 学习笔记 3
- 第七周C语言代码
- float数据类型
- TensorFlow基础笔记(1) 数据读取与保存
热门文章
- POJ 1741.Tree and 洛谷 P4178 Tree-树分治(点分治,容斥版) +二分 模板题-区间点对最短距离<;=K的点对数量
- IOI2019题解
- 前端微信小程序仿菜谱精灵
- 微信小程序 报错Failed to load image
- dedecms 模板文件不存在,无法解析文档的终极各种解决办法
- Echarts的简单入门
- mvn pom文件引用顺序关系
- [C++] namespace命名空间和using用法
- Linux_CentOS下搭建Nodejs 生产环境-以及nodejs进程管理器pm2的使用
- Laya改变文档结构后GameConfig自动生成错误问题