当前位置:网站首页>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] <= 104nums 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
边栏推荐
- Oracle lock table query and unlocking method
- RAC environment alert log error drop transient type: systp2jw0acnaurdgu1sbqmbryw = = troubleshooting
- Error 403 in most cases, you or one of your dependencies are requesting
- pycharm Install packages failed
- Search ideas and cases of large amount of Oracle redo log
- ARGB transparency conversion
- Leetcode | 38 appearance array
- sys. dbms_ scheduler. create_ Job creates scheduled tasks (more powerful and rich functions)
- Personal learning related
- OSS cloud storage management practice (polite experience)
猜你喜欢
![Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]](/img/ec/b1e99e0f6e7d1ef1ce70eb92ba52c6.png)
Three characteristics of volatile keyword [data visibility, prohibition of instruction rearrangement and no guarantee of operation atomicity]

SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置试读版

自动化的艺术

SQL learning | complex query

Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
![[machine learning] Note 4. KNN + cross validation](/img/a1/5afccedf509eda92a0fe5bf9b6cbe9.png)
[machine learning] Note 4. KNN + cross validation

零拷贝技术

Express②(路由)

Solution of discarding evaluate function in surprise Library

OSS cloud storage management practice (polite experience)
随机推荐
Storage scheme of video viewing records of users in station B
The query did not generate a result set exception resolution when the dolphin scheduler schedules the SQL task to create a table
Utilisation de GDB
Zero copy technology
Tersus notes employee information 516 MySQL query (time period uniqueness judgment of 2 fields)
Opening: identification of double pointer instrument panel
AttributeError: ‘dict‘ object has no attribute ‘iteritems‘
At the same time, the problems of height collapse and outer margin overlap are solved
[machine learning] Note 4. KNN + cross validation
[code analysis (3)] communication efficient learning of deep networks from decentralized data
19c RAC steps for modifying VIP and scanip - same network segment
这个SQL语名是什么意思
RAC environment alert log error drop transient type: systp2jw0acnaurdgu1sbqmbryw = = troubleshooting
Cross carbon market and Web3 to achieve renewable transformation
SAP UI5 应用开发教程之七十二 - SAP UI5 页面路由的动画效果设置试读版
Antd design form verification
ACFs file system creation, expansion, reduction and other configuration steps
零拷貝技術
Special window function rank, deny_ rank, row_ number
JS compares different elements in two arrays