当前位置:网站首页>LeetCode 面试题17.14 最小k个数(中等)
LeetCode 面试题17.14 最小k个数(中等)
2022-08-10 05:32:00 【生信研究猿】
python
1冒泡法
找出最小的k个数输出(超出时间限制)
class Solution:
def smallestK(self, arr: List[int], k: int) -> List[int]:
count = 0
for i in range(0,len(arr)):
min = i
for j in range(i,len(arr)):
if(arr[j]<arr[min]):
min = j
temp = arr[i]
arr[i] = arr[min]
arr[min] = temp
count += 1
if(count==k):
break
if(k==0):
return []
# print(arr)
return arr[0:k]
2.直接用python自带的函数
class Solution:
def smallestK(self, arr: List[int], k: int) -> List[int]:
arr.sort()
return arr[0:k]
边栏推荐
猜你喜欢
随机推荐
作业实验四
idm下载器如何使用 idm下载器使用技巧
Database Notes Create Database, Table Backup
Four characteristics of ACID
智能合约和去中心化应用DAPP
The submenu of the el-cascader cascade selector is double-clicked to display the selected content
行盒子的盒模型
数据库 笔记 创建数据库、表 备份
第五次实验
事务、存储引擎
共识计算和激励机制
win12 modify dns script
知识蒸馏论文学习
ACID四种特性
我不喜欢我的代码
力扣——统计只差一个字符的子串数目
Canal reports Could not find first log file name in binary log index file
Linux数据库Oracle客户端安装,用于shell脚本用sqlplus连接数据库
Chain Reading|The latest and most complete digital collection sales calendar-07.29
Batch add watermark to pictures batch scale pictures to specified size