剑指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的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
随机推荐
- js-原生js触发器使用
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- STL lower_bound upper_bound 用法
1.lower_bound(begin,end,x) 返回第一个>=x的位置,找不到return .end() 2.upper_bound (begin,end,x) 返回第一个>x的位置 ...
- [NOIP]模拟17 题解
A.入阵曲 部分分很肥,正解写得常数稍大就会和暴力一个分,考试的时候写什么自己考虑.(滑稽 部分分的循环边界手抖写错了-25 (原本暴力分中的10分都没了啊啊啊) 没写挂的话应该有75,其实就是二维前 ...
- mybatis的核心对象图解
- js中的函数声明和函数表达式的区别
目录 一.声明与表达式的格式 1.1 声明式的格式: 1.2 表达式的格式: 二.区别 2.1 函数表达式可以直接在后面加括号执行,而函数声明不可以. 2.2 函数表达式可以被提前解析出来 2.3 命 ...
- 为了避免hexo博客换了电脑应该提前做的准备。
个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io 本文转自:https://www.jiansh ...
- 第48章 MDK的编译过程及文件类型全解
Frm: http://www.cnblogs.com/firege/p/5806134.html 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教 ...
- 在Linux下面无法使用向上箭头自动补全上条命令
会用^[[A替代. 参考:http://stackoverflow.com/a/8641302/941650
- vue-router 路由配置
前提: 项目由 vue-cli 脚手架创建. 首先,先下载 vue-router npm install vue-router 安装完成后,运行项目 npm run dev 打开 main.js , ...
- Java通道
通道(Channel)是数据源和Java程序之间的开放连接,用于执行I/O操作.Channel接口在java.nio.channels包中.通道(Channel)接口只声明了两个方法:close()和 ...