当前位置:网站首页>[leetcode] 977 square of ordered array
[leetcode] 977 square of ordered array
2022-07-02 15:36:00 【Crisp ~】
Here's a button Non decreasing order Sorted array of integers nums, return The square of each number A new array of , According to the requirements Non decreasing order Sort .
Example 1:
Input : nums = [-4,-1,0,3,10]
Output : [0,1,9,16,100]
explain : After square , The array becomes [16,1,0,9,100]
After ordering , The array 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] <= 104
- nums Pressed Non decreasing order Sort
Advanced :
Please design with a time complexity of O(n) The algorithm solves this problem
# Direct sort
class Solution(object):
def sortedSquares(self, nums):
return sorted(num*num for num in nums)
# Time complexity :O(n \log n)O(nlogn)
# Spatial complexity :O(\log n)O(logn)
# Double pointer
# Because the array sequence is in ascending order , Square is a positive number , So start at both ends , Numbers with large absolute values enter the sequence squared first
class Solution(object):
def sortedSquares(self, nums):
a = 0
b = len(nums)-1
result = []
while a<=b:
if abs(nums[a])>=abs(nums[b]):
result.append(nums[a]*nums[a])
a+=1
else:
result.append(nums[b]*nums[b])
b-=1
return result[::-1]
边栏推荐
- Solution of Queen n problem
- 让您的HMI更具优势,FET-G2LD-C核心板是个好选择
- yolo格式数据集处理(xml转txt)
- 04.进入云原生后的企业级应用构建的一些思考
- 4. Jctree related knowledge learning
- 15_Redis_Redis.conf详解
- LeetCode_ String_ Simple_ 412.Fizz Buzz
- Bing.com網站
- LeetCode刷题——两整数之和#371#Medium
- Evaluation of embedded rz/g2l processor core board and development board of Feiling
猜你喜欢

Beijing rental data analysis

Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language

Tidb data migration tool overview

Solution of Queen n problem

Pytoch saves tensor to Mat file

List set & UML diagram

PTA 天梯赛习题集 L2-001 城市间紧急救援

LeetCode刷题——奇偶链表#328#Medium

16_ Redis_ Redis persistence

你不知道的Set集合
随机推荐
yolo格式数据集处理(xml转txt)
Custom exception
Data analysis thinking analysis methods and business knowledge - business indicators
Application of CDN in game field
Practical debugging skills
Semantic segmentation learning notes (1)
How to solve the problem of database content output
10_Redis_geospatial_命令
Bing. Site Internet
How does the computer set up speakers to play microphone sound
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
Pytorch 保存tensor到.mat文件
How to write sensor data into computer database
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
XML Configuration File
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
【LeetCode】344-反转字符串
Application and practice of Jenkins pipeline
QML pop-up frame, customizable
folium,确诊和密接轨迹上图