当前位置:网站首页>五月刷题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
边栏推荐
- 软件负载均衡和硬件负载均衡的选择
- IDS cache preheating, avalanche, penetration
- Global and Chinese market of AVR series microcontrollers 2022-2028: Research Report on technology, participants, trends, market size and share
- Workflow - activiti7 environment setup
- LeetCode41——First Missing Positive——hashing in place & swap
- Go redis initialization connection
- Meituan Er Mian: why does redis have sentinels?
- 英雄联盟轮播图手动轮播
- Redis之cluster集群
- YARN组织架构
猜你喜欢

Detailed explanation of cookies and sessions

Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges

Redis之五大基础数据结构深入、应用场景

Redis之发布订阅

Minio distributed file storage cluster for full stack development

Kratos ares microservice framework (II)

Redis之Geospatial

The five basic data structures of redis are in-depth and application scenarios

Mapreduce实例(七):单表join

Servlet learning diary 8 - servlet life cycle and thread safety
随机推荐
Basic concepts of libuv
Design and implementation of film and television creation forum based on b/s (attached: source code paper SQL file project deployment tutorial)
Libuv thread
[Yu Yue education] Wuhan University of science and technology securities investment reference
Minio distributed file storage cluster for full stack development
Detailed explanation of cookies and sessions
Global and Chinese markets of SERS substrates 2022-2028: Research Report on technology, participants, trends, market size and share
Servlet learning diary 8 - servlet life cycle and thread safety
AcWing 2456. 记事本
Global and Chinese market of appointment reminder software 2022-2028: Research Report on technology, participants, trends, market size and share
go-redis之初始化连接
Redis之主从复制
Nacos installation and service registration
一文读懂,DDD落地数据库设计实战
leetcode-14. Longest common prefix JS longitudinal scanning method
Redis之核心配置
DCDC power ripple test
Reids之删除策略
018.有效的回文
Basic usage of xargs command