当前位置:网站首页>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
边栏推荐
- 五月刷题02——字符串
- Blue Bridge Cup_ Single chip microcomputer_ Measure the frequency of 555
- Redis之核心配置
- [deep learning] semantic segmentation: paper reading: (2021-12) mask2former
- Redis之连接redis服务命令
- 【深度学习】语义分割-源代码汇总
- Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
- 为拿 Offer,“闭关修炼,相信努力必成大器
- 基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
- Mapreduce实例(八):Map端join
猜你喜欢
随机推荐
五月刷题03——排序
为拿 Offer,“闭关修炼,相信努力必成大器
Sentinel mode of redis
英雄联盟轮播图手动轮播
Global and Chinese market of cup masks 2022-2028: Research Report on technology, participants, trends, market size and share
Why data Tiering
068.查找插入位置--二分查找
IDS' deletion policy
Global and Chinese markets for hardware based encryption 2022-2028: Research Report on technology, participants, trends, market size and share
[Yu Yue education] Wuhan University of science and technology securities investment reference
Kratos战神微服务框架(一)
Connexion d'initialisation pour go redis
Mapreduce实例(五):二次排序
Blue Bridge Cup_ Single chip microcomputer_ PWM output
Redis分布式锁实现Redisson 15问
Hard core! One configuration center for 8 classes!
[Yu Yue education] reference materials of complex variable function and integral transformation of Shenyang University of Technology
有软件负载均衡,也有硬件负载均衡,选择哪个?
AcWing 2456. Notepad
Cap theory