当前位置:网站首页>leetcode--977. Squares of a Sorted Array
leetcode--977. Squares of a Sorted Array
2022-04-23 14:03:00 【Amelia who loves learning】
-
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]
- Tips :
1 <= nums.length <= 104-104 <= nums[i] <= 104nums is sorted in non-decreasing order.
- Ideas :
First, sort each value in the list
Output with built-in sorting algorithm
- Solution 1 – direct :
class Solution:
def sortedSquares(self, nums):
dou_nums = [x * x for x in nums]
return sorted(dou_nums)
- Ideas :
Set an empty list to store qualified values , Set the head and tail pointer to head and tail
When the head pointer is not equal to the tail pointer , Continue operation , If the square of the head is greater than the square of the tail , Insert the header value into the list and add one to the pointer ; If the square of the tail value is greater than or equal to the square of the head value , Insert the square of the tail value into the list and add one to the tail pointer ; If the head pointer is greater than or equal to the tail pointer , And then jump out of the loop
Insert the remaining header values squared into the list
Returns the reverse order of the list
- Solution 2 – Double pointer :
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]
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/squares-of-a-sorted-array ︎
版权声明
本文为[Amelia who loves learning]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231341317276.html
边栏推荐
- websocket
- Function executes only the once function for the first time
- Pytorch 经典卷积神经网络 LeNet
- Lin Lin, product manager of Lenovo: network failure of local network operator in Tianjin. The background server of Zui system can't work normally for the time being
- mysql新表,自增id长达20位,原因竟是......
- 微信小程序与低功耗蓝牙通信-往硬件端发送数据(三)
- Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
- Node接入支付宝开放平台的沙箱实现支付功能
- STM32学习记录0007——新建工程(基于寄存器版)
- 初识go语言
猜你喜欢

烟雾传感器(mq-2)使用详细教程(基于树莓派3b+实现)

基于CM管理的CDH6.3.2集群集成Atlas2.1.0

蓝绿发布、滚动发布、灰度发布,有什么区别?

Multithreading

freeCodeCamp----arithmetic_ Arranger exercise

How does redis solve the problems of cache avalanche, cache breakdown and cache penetration

Chapter I review of e-commerce spike products

微信小程序的订阅号开发(消息推送)

淘宝发布宝贝提示“您的消保保证金额度不足,已启动到期保障”
基于微信小程序的wifi模块使用
随机推荐
What is the difference between blue-green publishing, rolling publishing and gray publishing?
Introduction to spark basic operation
Problems encountered in the project (V) understanding of operating excel interface poi
Oracle告警日志alert.log和跟踪trace文件中文乱码显示
Express middleware ③ (custom Middleware)
基于ibeacons三点定位(微信小程序)
jacob打印word
Atcoder beginer contest 248c dice sum (generating function)
项目中遇到的问题(五)操作Excel接口Poi的理解
Intégration de Clusters CDH Phoenix basée sur la gestion cm
redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
Autumn recruitment in 2021, salary ranking No
Node接入支付宝开放平台的沙箱实现支付功能
Yarn online dynamic resource tuning
Elmo (bilstm-crf + Elmo) (conll-2003 named entity recognition NER)
How does redis solve the problems of cache avalanche, cache breakdown and cache penetration
基础知识学习记录
Special test 05 · double integral [Li Yanfang's whole class]
Restful WebService和gSoap WebService的本质区别
scikit-learn构建模型的万能模板