当前位置:网站首页>leetcode--977. Squares of a Sorted Array
leetcode--977. Squares of a Sorted Array
2022-04-23 13:41:00 【爱学习的Amelia】
-
Problem:Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. 1
-
Example :
# Example 1
Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]
Explanation: After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].
# Example 2
Input: nums = [-7,-3,2,3,11]
Output: [4,9,9,49,121]
- 提示:
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums is sorted in non-decreasing order.
- 思路:
首先将列表中的每个值排序
采用内置排序算法输出
- 解法一–直接:
class Solution:
def sortedSquares(self, nums):
dou_nums = [x * x for x in nums]
return sorted(dou_nums)
- 思路:
设置一个空列表存储符合条件的值,设置头尾指针指向头尾
当头指针不等于尾指针时,继续运行,若头值平方大于尾的平方,则将头值插入列表并指针加一;若尾值平方大于等于头值平方,则将尾值平方插入列表并尾指针加一;若头指针大于等于尾指针,则跳出循环
将剩下的头值平方插入列表
返回列表的倒序
- 解法二–双指针:
class Solution:
def sortedSquares(self, nums):
dou_nums = []
a, b = 0, len(nums)-1
while a != b:
dou_a = nums[a] ** 2
dou_b = nums[b] ** 2
if dou_a > dou_b:
dou_nums.append(dou_a)
a += 1
elif dou_b >= dou_a:
dou_nums.append(dou_b)
b -= 1
if a >= b:
break
dou_nums.append(nums[a]**2)
return dou_nums[::-1]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/squares-of-a-sorted-array ︎
版权声明
本文为[爱学习的Amelia]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_46361294/article/details/124357319
边栏推荐
- SAP ui5 application development tutorial 72 - animation effect setting of SAP ui5 page routing
- SAP ui5 application development tutorial 72 - trial version of animation effect setting of SAP ui5 page routing
- Aicoco AI frontier promotion (4.23)
- SQL learning window function
- QT calling external program
- Es introduction learning notes
- PG library to view the distribution keys of a table in a certain mode
- 联想拯救者Y9000X 2020
- Oracle defines self incrementing primary keys through triggers and sequences, and sets a scheduled task to insert a piece of data into the target table every second
- Generate 32-bit UUID in Oracle
猜你喜欢
【重心坐标插值、透视矫正插值】原理以及用法见解
Express②(路由)
Exemple de méthode de réalisation de l'action d'usinage à point fixe basée sur l'interruption de déclenchement du compteur à grande vitesse ob40 pendant le voyage de tia Expo
Double pointer instrument panel reading (I)
Dynamic subset division problem
Basic SQL query and learning
Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]
Apache seatunnel 2.1.0 deployment and stepping on the pit
Modify the Jupiter notebook style
Tersus notes employee information 516 MySQL query (time period uniqueness judgment of 2 fields)
随机推荐
Oracle kills the executing SQL
Detailed explanation of constraints of Oracle table
Oracle modify default temporary tablespace
Plato farm, a top-level metauniverse game, has made frequent positive moves recently
ARGB transparency conversion
聯想拯救者Y9000X 2020
Personal learning related
Handling of high usage of Oracle undo
2022年江西最新建筑八大员(质量员)模拟考试题库及答案解析
Leetcode brush question 897 incremental sequential search tree
Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
Technologie zéro copie
Oracle view related
On the bug of JS regular test method
Basic SQL query and learning
【视频】线性回归中的贝叶斯推断与R语言预测工人工资数据|数据分享
Move blog to CSDN
SAP ui5 application development tutorial 72 - trial version of animation effect setting of SAP ui5 page routing
Double pointer instrument panel reading (I)
NPM err code 500 solution