当前位置:网站首页>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
边栏推荐
- Redis cluster
- 五月集训总结——来自阿光
- 软件负载均衡和硬件负载均衡的选择
- Kratos战神微服务框架(三)
- Global and Chinese markets of SERS substrates 2022-2028: Research Report on technology, participants, trends, market size and share
- Mathematical modeling 2004b question (transmission problem)
- Leetcode problem solving 2.1.1
- Basic concepts of libuv
- What is an R-value reference and what is the difference between it and an l-value?
- Go redis initialization connection
猜你喜欢
随机推荐
Minio distributed file storage cluster for full stack development
Global and Chinese markets for hardware based encryption 2022-2028: Research Report on technology, participants, trends, market size and share
Oom happened. Do you know the reason and how to solve it?
Redis之Lua脚本
MapReduce instance (x): chainmapreduce
【深度学习】语义分割:论文阅读:(2021-12)Mask2Former
【深度学习】语义分割:论文阅读:(CVPR 2022) MPViT(CNN+Transformer):用于密集预测的多路径视觉Transformer
基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
[Yu Yue education] reference materials of power electronics technology of Jiangxi University of science and technology
[deep learning] semantic segmentation: paper reading: (CVPR 2022) mpvit (cnn+transformer): multipath visual transformer for dense prediction
What is an R-value reference and what is the difference between it and an l-value?
YARN组织架构
Go redis initialization connection
Why data Tiering
解决小文件处过多
Le modèle sentinelle de redis
Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)
Redis geospatial
Heap (priority queue) topic
Webrtc blog reference: