当前位置:网站首页>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 core configuration
运维,放过监控-也放过自己吧
Mapreduce实例(九):Reduce端join
Detailed explanation of cookies and sessions
Le modèle sentinelle de redis
基于B/S的影视创作论坛的设计与实现(附:源码 论文 sql文件 项目部署教程)
Activiti7工作流的使用
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
Redis' bitmap
Redis之持久化实操(Linux版)
随机推荐
Global and Chinese market of bank smart cards 2022-2028: Research Report on technology, participants, trends, market size and share
Sqlmap installation tutorial and problem explanation under Windows Environment -- "sqlmap installation | CSDN creation punch in"
Compilation of libwebsocket
Redis之主从复制
Global and Chinese market of appointment reminder software 2022-2028: Research Report on technology, participants, trends, market size and share
Mapreduce实例(九):Reduce端join
Segmentation sémantique de l'apprentissage profond - résumé du code source
Master slave replication of redis
Libuv thread
Meituan Er Mian: why does redis have sentinels?
Redis geospatial
Mathematical modeling 2004b question (transmission problem)
Design and implementation of online shopping system based on Web (attached: source code paper SQL file)
Appears when importing MySQL
Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)
Blue Bridge Cup_ Single chip microcomputer_ Measure the frequency of 555
CAP理论
What are the models of data modeling
Redis之Bitmap
Sentinel mode of redis