type
Post
status
Published
date
Jun 8, 2024
slug
summary
tags
算法
双指针法
数组
category
算法
icon
password
给你一个按 非递减顺序 排序的整数数组
nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。示例 1:
示例 2:
思路:
双指针法(快慢指针法): 通过一个快指针和慢指针在一个for循环下完成两个for循环的工作。
非递减:意味着序列中的元素可以保持不变或逐渐增大,但不能减少。
所以平方后,要么最大值在右侧(如:[-2,0,1,2,3]),要么在左侧(如:[-4,-2,-1,0,1,2,3])。
所以从两端开始比较逐渐向中间遍历。
- Author:guderain
- URL:https://wangguanxi.space/article/15f2b727-a3a3-8038-90f7-e8c8df559974
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!
Relate Posts

.webp?table=collection&id=92be88af-5f71-4631-9d3e-ee3bd53dcced&t=92be88af-5f71-4631-9d3e-ee3bd53dcced&width=1080&cache=v2)
