当前位置:网站首页>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
边栏推荐
- 五月刷题03——排序
- Publish and subscribe to redis
- Global and Chinese market of airport kiosks 2022-2028: Research Report on technology, participants, trends, market size and share
- MapReduce instance (VI): inverted index
- One article read, DDD landing database design practice
- Redis之核心配置
- Mathematical modeling 2004b question (transmission problem)
- [Chongqing Guangdong education] reference materials for nine lectures on the essence of Marxist Philosophy in Wuhan University
- 一文读懂,DDD落地数据库设计实战
- Mapreduce实例(十):ChainMapReduce
猜你喜欢

Redis之发布订阅

运维,放过监控-也放过自己吧

Reids之缓存预热、雪崩、穿透

Servlet learning diary 8 - servlet life cycle and thread safety

Take you back to spark ecosystem!

【深度學習】語義分割-源代碼匯總

五月集训总结——来自阿光

Improved deep embedded clustering with local structure preservation (Idec)

基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)
![[shell script] - archive file script](/img/50/1bef6576902890dfd5771500414876.png)
[shell script] - archive file script
随机推荐
数据建模有哪些模型
面试突击62:group by 有哪些注意事项?
解决小文件处过多
【深度学习】语义分割-源代码汇总
Libuv thread
Leetcode:608 树节点
Mathematical modeling 2004b question (transmission problem)
[shell script] use menu commands to build scripts for creating folders in the cluster
Redis之cluster集群
QML type: locale, date
Mysql database recovery (using mysqlbinlog command)
Use of activiti7 workflow
Kratos战神微服务框架(一)
Nacos installation and service registration
Redis之五大基础数据结构深入、应用场景
Redis分布式锁实现Redisson 15问
【深度学习】语义分割:论文阅读:(CVPR 2022) MPViT(CNN+Transformer):用于密集预测的多路径视觉Transformer
Activiti7工作流的使用
发生OOM了,你知道是什么原因吗,又该怎么解决呢?
Mapreduce实例(四):自然排序