剑指offer——01数组中重复的数字
题目描述
从0位置遍历,开辟一个lenght大小的数组,统计每个数字出现的次数,当某个数字出现的次数大于1时,则是第一个重复数字
class Solution01 {
public:
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
bool duplicate(int numbers[], int length, int* duplication) {
vector<int>v(length, );
for (int i = ; i < length; ++i)
{
v[numbers[i]]++;
if (v[numbers[i]] > )
{
*duplication = numbers[i];
return true;
}
}
return false;
}
};
class Solution02 {
public:
// Parameters:
// numbers: an array of integers
// length: the length of array numbers
// duplication: (Output) the duplicated number in the array number
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
bool duplicate(int numbers[], int length, int* duplication) {
for (int i = ; i < length; ++i)
{
while (numbers[i] != i)
{
if (numbers[i] == numbers[numbers[i]])
{
*duplication = numbers[i];
return true;
}
swap(numbers[i], numbers[numbers[i]]);
}
}
return false;
}
};
剑指offer——01数组中重复的数字的更多相关文章
- 剑指 Offer 03. 数组中重复的数字
剑指 Offer 03. 数组中重复的数字 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知 ...
- 菜鸟刷题路:剑指 Offer 03. 数组中重复的数字
剑指 Offer 03. 数组中重复的数字 哈希表/set class Solution { public int findRepeatNumber(int[] nums) { HashSet< ...
- 5.1 剑指 Offer 03. 数组中重复的数字
类型题:剑指 Offer 03. 数组中重复的数字 找出数组中重复的数字.在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了, ...
- 【剑指Offer】数组中重复的数字 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 Set 快慢指针 日期 题目地址:https://leetcod ...
- Go语言实现:【剑指offer】数组中重复的数字
该题目来源于牛客网<剑指offer>专题. 在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组 ...
- (python)剑指Offer:数组中重复的数字
问题描述 在长度为n的数组中,所有的元素都是0到n-1的范围内. 数组中的某些数字是重复的,但不知道有几个重复的数字,也不知道重复了几次,请找出任意重复的数字. 例如,输入长度为7的数组{2,3,1, ...
- 剑指Offer 50. 数组中重复的数字 (数组)
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- 剑指offer:数组中重复的数字
题目描述: 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度 ...
- [剑指Offer] 50.数组中重复的数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
随机推荐
- Luogu P1738 洛谷的文件夹
P1738 Luogu 发一个链表题解! 仅有24ms,排名第一哦~ 圆圈代表点,每个店有两个指针,一个指向自己兄弟(同级文件夹),另一个指向自己孩子(子文件夹),还有一个保存当前名字. 有点像二叉树 ...
- HDU6333 求组合数前m项的和
目录 分块 莫队 @ HDU6333:传送门 题意:求组合数前m项的和. 在线分块or离线莫队 分块 重要的一个定理: \[C_{n}^{m} = 0\;\;m > n\] \[C_{n}^{m ...
- Serializable读写类操作
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...
- git分布式版本控制系统权威指南学习笔记(三):简单了解git对象、head以及sha1sum
文章目录 git对象(简单了解) 对象是存在哪里的? head和master分支 上面的hash值怎么来的? git对象(简单了解) 每次提交都有tree.parent.author.committe ...
- C++——编译器运行过程
C++ 编译过程简介 C/C++程序编译流程: 预处理->编译->汇编->链接 具体的就是: 源代码(source coprede)→预处理器(processor)→编译器(co ...
- 机器学习技法笔记:02 Dual Support Vector Machine、KKT
原文地址:https://www.jianshu.com/p/58259cdde0e1 Roadmap Motivation of Dual SVM Lagrange Dual SVM Solving ...
- 去除字符串中的HTML标签
背景:Kindeditor内容保存在数据库中的类型是text,包含文字和HTML标签. 需求:显示内容的前50个字(纯文字内容) 方法:将字段查出去除标签,截取前50 import java.util ...
- vim + ctag + Taglist (转)
一.ctag 当然,第一步就是要下载它!一条命令搞定: $sudo apt-get install ctags 如果不幸提示找不到软件包ctags,首先你也许应该update一下你的软件源,还不行的话 ...
- MFS分布式文件系统【2】MFS MASTER 部署
MFS版本 mfs-1.6.27 MFS-MASTER 192.168.1.190 MFS-CHUNKSERVER1 192.168.1.252 MFS-CHUNKSERVER2 192.168.1. ...
- bigger is greater
题目: Lexicographical order is often known as alphabetical order when dealing with strings. A string i ...