当前位置:网站首页>五月刷题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
边栏推荐
- 美团二面:为什么 Redis 会有哨兵?
- Advanced Computer Network Review(3)——BBR
- Redis之连接redis服务命令
- Connexion d'initialisation pour go redis
- Global and Chinese market of capacitive displacement sensors 2022-2028: Research Report on technology, participants, trends, market size and share
- Kratos ares microservice framework (I)
- 英雄联盟轮播图手动轮播
- 【深度学习】语义分割:论文阅读:(CVPR 2022) MPViT(CNN+Transformer):用于密集预测的多路径视觉Transformer
- [daily question] Porter (DFS / DP)
- What is an R-value reference and what is the difference between it and an l-value?
猜你喜欢
数据建模有哪些模型
Reids之缓存预热、雪崩、穿透
Selenium+pytest automated test framework practice
Pytest's collection use case rules and running specified use cases
Redis core configuration
Selenium+pytest automated test framework practice (Part 2)
MapReduce工作机制
Design and implementation of online shopping system based on Web (attached: source code paper SQL file)
[oc]- < getting started with UI> -- common controls - prompt dialog box and wait for the prompt (circle)
Redis geospatial
随机推荐
[three storage methods of graph] just use adjacency matrix to go out
Global and Chinese market of metallized flexible packaging 2022-2028: Research Report on technology, participants, trends, market size and share
Servlet learning diary 8 - servlet life cycle and thread safety
MySQL数据库优化的几种方式(笔面试必问)
Withdrawal of wechat applet (enterprise payment to change)
Leetcode problem solving 2.1.1
五层网络体系结构
Redis之哨兵模式
基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)
Blue Bridge Cup_ Single chip microcomputer_ Measure the frequency of 555
Advance Computer Network Review(1)——FatTree
Full stack development of quartz distributed timed task scheduling cluster
Le modèle sentinelle de redis
Mapreduce实例(七):单表join
Mapreduce实例(八):Map端join
Lua script of redis
Global and Chinese market for annunciator panels 2022-2028: Research Report on technology, participants, trends, market size and share
Mapreduce实例(六):倒排索引
Libuv thread
Pytest parameterization some tips you don't know / pytest you don't know