当前位置:网站首页>五月刷题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
边栏推荐
- [three storage methods of graph] just use adjacency matrix to go out
- 为拿 Offer,“闭关修炼,相信努力必成大器
- 【shell脚本】使用菜单命令构建在集群内创建文件夹的脚本
- Redis之发布订阅
- [Chongqing Guangdong education] reference materials for nine lectures on the essence of Marxist Philosophy in Wuhan University
- Redis connection redis service command
- 基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
- Kratos ares microservice framework (I)
- Design and implementation of film and television creation forum based on b/s (attached: source code paper SQL file project deployment tutorial)
- 运维,放过监控-也放过自己吧
猜你喜欢

Redis' bitmap

Kratos ares microservice framework (II)

工作流—activiti7环境搭建

Activiti7工作流的使用

What is MySQL? What is the learning path of MySQL

基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
![[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

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

QML control type: Popup

Redis之哨兵模式
随机推荐
IDS' deletion policy
Global and Chinese market of linear regulators 2022-2028: Research Report on technology, participants, trends, market size and share
018.有效的回文
【深度学习】语义分割-源代码汇总
AcWing 2456. 记事本
One article read, DDD landing database design practice
Advance Computer Network Review(1)——FatTree
数据建模有哪些模型
美团二面:为什么 Redis 会有哨兵?
Webrtc blog reference:
An article takes you to understand the working principle of selenium in detail
Sqlmap installation tutorial and problem explanation under Windows Environment -- "sqlmap installation | CSDN creation punch in"
xargs命令的基本用法
Global and Chinese market for annunciator panels 2022-2028: Research Report on technology, participants, trends, market size and share
Parameterization of postman
【深度学习】语义分割:论文阅读:(CVPR 2022) MPViT(CNN+Transformer):用于密集预测的多路径视觉Transformer
Global and Chinese markets for modular storage area network (SAN) solutions 2022-2028: Research Report on technology, participants, trends, market size and share
Reids之删除策略
Segmentation sémantique de l'apprentissage profond - résumé du code source
英雄联盟轮播图手动轮播