桶排序 && leetcode 41
桶排序
对于0-1000 ,用1001个桶 简单版
或者用10个桶0-9,先按各位装桶,然后依(桶)次放回,然后再按十位放桶,放回,然后百位。
也就是基数排序
https://www.cnblogs.com/lqerio/p/11901828.html
Leetcode41
设数组长度为n
那么答案必在 1-n+1的范围内。那么一个萝卜一个坑
先做预处理。类似于桶排序,把正确的数放在正确的位置。
遍历数组,对于i+1,若i+1在1-n内,应该放在nums[i]的位置上
然后遍历,从0开始到n,若nums[i]!=i+1,就输出i+1. 因为这时候,通过上面的预处理,说明不存在i+1这个数。
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
for (int i = 0; i < nums.size(); i++) {
while (nums[i] != i + 1) {
if (nums[i] <= 0 || nums[i] > nums.size() || nums[i] == nums[nums[i] - 1])
break;
int idx = nums[i] - 1;
nums[i] = nums[idx];
nums[idx] = idx + 1;
}
}
for (int i = 0; i < nums.size(); i++) {
if (nums[i] != (i + 1)) {
return (i + 1);
}
}
return (nums.size() + 1);
}
};
然后官方题解为hash表
首先可知结果最大为n+1,n为数组长度
预处理:
1.先判断是否有1,无则输出1
2.否则遍历数组,对大于n或者小于1的数改为1
3.然后遍历数组,对于num[k]=i,将nums[i]设置为负数。 nums[i]<0说明数组中存在i (用自己做hash表)
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
int flag = 0; // 1 not exist
for (int i = 0; i < n; i++)
if (nums[i] == 1) {
flag=1;
break;
}
if (!flag)
return 1;
if (n == 1)
return 2;
// 用 1 替换负数,0,和大于 n 的数
// 在转换以后,nums 只会包含1-n
for (int i = 0; i < n; i++)
if ((nums[i] <= 0) || (nums[i] > n))
nums[i] = 1;
// 使用索引和数字符号作为检查器
// 例如,如果 nums[1] 是负数表示在数组中出现了数字 `1`
// 如果 nums[2] 是正数 表示数字 2 没有出现
for (int i = 0; i < n; i++) {
int a = abs(nums[i]);
// 如果发现了一个数字 a - 改变第 a 个元素的符号
// 注意重复元素只需操作一次
if (a == n)
nums[0] = - abs(nums[0]);
else
nums[a] = - abs(nums[a]);
}
// 现在第一个正数的下标就是第一个缺失的数
for (int i = 1; i < n; i++) {
if (nums[i] > 0)
return i;
}
if (nums[0] > 0)
return n;
return n + 1;
}
};
桶排序 && leetcode 41的更多相关文章
- 【leetcode 桶排序】Maximum Gap
1.题目 Given an unsorted array, find the maximum difference between the successive elements in its sor ...
- [LeetCode] 桶排序的特殊解,例 Sort Color
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- Bucket Sort - leetcode [桶排序]
桶排序(Bucket sort)或所谓的箱排序,是一个排序算法,工作的原理是将数组分到有限数量的桶里.每个桶再个别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序进行排序).桶排序是鸽巢排序 ...
- leetcode算法题3:分组,让每个组的最小者,相加之后和最大。想知道桶排序是怎么样的吗?
/* Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- 数据结构与算法之PHP排序算法(桶排序)
一.基本思想 桶排序是将待排序的数据分割成许多buckets,然后每个bucket各自排序,或用不同的排序算法,或者递归的使用bucket sort算法.也是典型的分而治之(divide-and-co ...
- 由Maximum Gap,对话桶排序,基数排序和统计排序
一些非比较排序 在LeetCode中有个题目叫Maximum Gap.是求一个非排序的正数数列中按顺序排列后的最大间隔.这个题用桶排序和基数排序都能够实现.以下说一下桶排序.基数排序和计数排序这三种非 ...
- CSP-J2020 洛谷P7072 直播获奖(Splay/桶排序)
题目描述 NOI2130 即将举行.为了增加观赏性,CCF 决定逐一评出每个选手的成绩,并直播即时的获奖分数线.本次竞赛的获奖率为 w%,即当前排名前 w% 的选手的最低成绩就是即时的分数线. 更具体 ...
- 计数排序和桶排序(Java实现)
目录 比较和非比较的区别 计数排序 计数排序适用数据范围 过程分析 桶排序 网络流传桶排序算法勘误 桶排序适用数据范围 过程分析 比较和非比较的区别 常见的快速排序.归并排序.堆排序.冒泡排序等属于比 ...
随机推荐
- Docker 拉取镜像速度太慢
Docker Hub 是我们分发和获取 Docker 镜像的中心,但由于服务器位于海外,经常会出现拉取/上传镜像时速度太慢或无法访问的情况.再加上运营方不断对 Docker Hub 的免费使用进行限制 ...
- Java多线程基础知识笔记(持续更新)
多线程基础知识笔记 一.线程 1.基本概念 程序(program):是为完成特定任务.用某种语言编写的一组指令的集合.即指一段静态的代码,静态对象. 进程(process):是程序的一次执行过程,或是 ...
- 指针锁定 Pointer Lock API 用法
指针锁定 Pointer Lock API 通过它可以访问原始的鼠标运动(基于指针的相对位移 movementX / movementY),把鼠标事件的目标锁定到一个特定的元素,同时隐藏视图中的指针光 ...
- 牛逼!MySQL 8.0 中的索引可以隐藏了…
MySQL 8.0 虽然发布很久了,但可能大家都停留在 5.7.x,甚至更老,其实 MySQL 8.0 新增了许多重磅新特性,比如栈长今天要介绍的 "隐藏索引" 或者 " ...
- 记一次ceph pg unfound处理过程
今天检查ceph集群,发现有pg丢失,于是就有了本文~~~ 1.查看集群状态 [root@k8snode001 ~]# ceph health detail HEALTH_ERR 1/973013 o ...
- windows激活密钥
密钥来源,微软官方 KMS 客户端安装密钥 | Microsoft Docs Windows Server 2008 R2 操作系统版本 KMS 客户端安装程序密钥 Windows Server 20 ...
- Spring AOP介绍与使用
Spring AOP介绍与使用 AOP:Aspect Oriented Programming 面向切面编程 OOP:Object Oriented Programming 面向对象编程 面向切面 ...
- Win+R 快速启动程序
将某个程序的可执行C:\Program Files\Oracle\VirtualBox\VirtualBox.exe 或其快捷键 放入 某个自建的快捷键集合文件夹,可以自定义命名 如 vb 将其添加到 ...
- Azure Terraform(八)利用Azure DevOps 实现Infra资源和.NET CORE Web 应用程序的持续集成、持续部署
一,引言 上一篇讲解到利用 Azure DevOps 将整个 Azure Web App,Azure Traffic Manager profile,Azure Storage Account,Azu ...
- (一)Spring-Boot-操作-Redis
Spring-Boot-操作-Redis 1.Spring Data Redis 1.1 引入依赖 1.2 配置 Redis 信息 1.3 使用 2.Spring Cache 2.1 引入依赖 2.2 ...