当前位置:网站首页>五月刷题03——排序
五月刷题03——排序
2022-07-06 09:02:00 【追逐梦想的阿光】
五月刷题03——排序
今日刷题内容: 排序
前言
- 更新每天刷题的题解内容
- 注重个人理解,看难度更新题目数量
- 题目来源于力扣
- 新开专栏,争取每日都能做出至少一题=v=
- 语言java、python、c\c++
一、今日题目
二、解题思路
1. 977. 有序数组的平方*****
- 可以直接求每个元素的平方
- 排序后即可得到结果
class Solution:
def sortedSquares(self, nums: List[int]) -> List[int]:
ret = [i * i for i in nums]
ret.sort()
return ret
2. 268. 丢失的数字*****
- 已知是从
[0, n]的数,所以排序后比较元素和下标的值即可- 如果元素和下标值不匹配说明该下标就是要找的值
- 否则返回数组的长度
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. 数组中最大数对和的最小值*****
- 根据题意,数对和是指当前数组的最大值和最小值之和
- 即从数组中取数,不可重复取
- 维护一个最大值即可
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. 按递增顺序显示卡牌*****
- 模拟可知,每次放的大小按0, 2, 4,……的规律放牌
- 即根据下标模拟,每次放完牌后,将下一个下标放到队尾
- 重复以上过程即可
class Solution:
def deckRevealedIncreasing(self, deck: List[int]) -> List[int]:
n = len(deck)
# 维护一个下标队列
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:
# 如果下标队列不为空,将队首元素移到队尾
mark.append(mark.pop(0))
return ret
边栏推荐
- Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
- Basic concepts of libuv
- 解决小文件处过多
- Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)
- 数据建模有哪些模型
- Selenium+pytest automated test framework practice (Part 2)
- Hard core! One configuration center for 8 classes!
- Mathematical modeling 2004b question (transmission problem)
- 七层网络体系结构
- Redis core configuration
猜你喜欢

Blue Bridge Cup_ Single chip microcomputer_ Measure the frequency of 555

QML type: locale, date

MapReduce工作机制

Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)

Workflow - activiti7 environment setup

面渣逆袭:Redis连环五十二问,图文详解,这下面试稳了

Full stack development of quartz distributed timed task scheduling cluster

Use of activiti7 workflow

Activiti7工作流的使用
![[deep learning] semantic segmentation: paper reading: (CVPR 2022) mpvit (cnn+transformer): multipath visual transformer for dense prediction](/img/f1/6f22f00843072fa4ad83dc0ef2fad8.png)
[deep learning] semantic segmentation: paper reading: (CVPR 2022) mpvit (cnn+transformer): multipath visual transformer for dense prediction
随机推荐
MySQL数据库优化的几种方式(笔面试必问)
Blue Bridge Cup_ Single chip microcomputer_ PWM output
068.查找插入位置--二分查找
Appears when importing MySQL
Kratos ares microservice framework (I)
Servlet learning diary 7 -- servlet forwarding and redirection
Design and implementation of film and television creation forum based on b/s (attached: source code paper SQL file project deployment tutorial)
数据建模有哪些模型
Advanced Computer Network Review(5)——COPE
Persistence practice of redis (Linux version)
Global and Chinese market of airport kiosks 2022-2028: Research Report on technology, participants, trends, market size and share
Redis之Bitmap
Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
What is MySQL? What is the learning path of MySQL
Mysql database recovery (using mysqlbinlog command)
工作流—activiti7环境搭建
Full stack development of quartz distributed timed task scheduling cluster
Connexion d'initialisation pour go redis
One article read, DDD landing database design practice
解决小文件处过多