当前位置:网站首页>May brush question 03 - sorting
May brush question 03 - sorting
2022-07-06 09:38:00 【A Guang chasing dreams】
May brush questions 03—— Sort
Today's brush topic content : Sort
Preface
- Update the problem solution content of the problem brush every day
- Focus on personal understanding , Look at the difficulty and update the number of questions
- The title comes from Li Kou
- New column , Try to work out at least one question every day =v=
- Language java、python、c\c++
One 、 Today's topic
- 977. The square of an ordered array *****
- 268. Missing numbers *****
- 1877. The minimum value of the sum of the largest pairs in an array *****
- 950. Display cards in ascending order *****
Two 、 Their thinking
1. 977. The square of an ordered array *****
- You can directly find the square of each element
- After sorting, you can get the result
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
ret = [i * i for i in nums]
ret.sort()
return ret
2. 268. Missing numbers *****
- Known from
[0, n]
Number of numbers , So compare the values of elements and subscripts after sorting- If the element and the subscript value do not match, the subscript is the value to be found
- Otherwise, return the length of the array
class Solution:
def missingNumber(self, nums: List[int]) -> int:
nums.sort()
ret = len(nums)
for idx, val in enumerate(nums):
if idx != val:
ret = idx
break
return ret
3. 1877. The minimum value of the sum of the largest pairs in an array *****
- According to the meaning , The sum of pairs refers to the sum of the maximum and minimum values of the current array
- That is, get data from the array , Can not be repeated
- Maintain a maximum value
class Solution {
public int minPairSum(int[] nums) {
Arrays.sort(nums);
int i = 0;
int n = nums.length;
int ret = 0;
for(i = 0; i < n / 2; i++){
ret = Math.max(ret, nums[i] + nums[n - i - 1]);
}
return ret;
}
}
4. 950. Display cards in ascending order *****
- Simulation shows that , The size of each time is 0, 2, 4,…… Play cards regularly
- That is, simulate according to the subscript , After each play , Put the next subscript at the end of the team
- Repeat the above process
class Solution:
def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
n = len(deck)
# Maintain a subscript queue
mark = [i for i in range(n)]
deck.sort()
ret = [0 for i in range(n)]
for card in deck:
ret[ mark.pop(0) ] = card
if mark:
# If the subscript queue is not empty , Move the first element of the team to the end of the team
mark.append(mark.pop(0))
return ret
边栏推荐
- 基于B/S的医院管理住院系统的研究与实现(附:源码 论文 sql文件)
- AcWing 2456. Notepad
- Blue Bridge Cup_ Single chip microcomputer_ PWM output
- QML type: overlay
- Why data Tiering
- What is an R-value reference and what is the difference between it and an l-value?
- Redis之连接redis服务命令
- Redis' performance indicators and monitoring methods
- 一文读懂,DDD落地数据库设计实战
- Processes of libuv
猜你喜欢
[Yu Yue education] Wuhan University of science and technology securities investment reference
[three storage methods of graph] just use adjacency matrix to go out
Full stack development of quartz distributed timed task scheduling cluster
MapReduce instance (VII): single table join
Mapreduce实例(七):单表join
软件负载均衡和硬件负载均衡的选择
解决小文件处过多
Master slave replication of redis
Reids之删除策略
Reids之缓存预热、雪崩、穿透
随机推荐
QML control type: menu
英雄联盟轮播图自动轮播
Oom happened. Do you know the reason and how to solve it?
MapReduce instance (VI): inverted index
【深度学习】语义分割:论文阅读:(2021-12)Mask2Former
In order to get an offer, "I believe that hard work will make great achievements
解决小文件处过多
Redis' performance indicators and monitoring methods
有软件负载均衡,也有硬件负载均衡,选择哪个?
Global and Chinese markets for modular storage area network (SAN) solutions 2022-2028: Research Report on technology, participants, trends, market size and share
Blue Bridge Cup_ Single chip microcomputer_ PWM output
[Yu Yue education] Wuhan University of science and technology securities investment reference
基于B/S的影视创作论坛的设计与实现(附:源码 论文 sql文件 项目部署教程)
一大波开源小抄来袭
Master slave replication of redis
018. Valid palindromes
Redis之持久化实操(Linux版)
Minio distributed file storage cluster for full stack development
Parameterization of postman
QML type: locale, date