剑指offer:数组中重复的数字
题目描述:
解题思路:
思路一:排序。遍历判断一次。时间复杂度O(nlogn)。
思路二:用空间换时间。利用哈希表对应每一个数字,遍历第一遍,计算每个数字出现次数。遍历第二次,找到第一个重复数字,输出。时间复杂度O(n)。
思路三:不利用辅助空间。
代码:
思路二:
class Solution {
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) {
if(length<=)
return false;
int hash[length];
for(int i=; i<length; i++)
hash[i]=;
for(int i=; i<length; i++)
{
hash[numbers[i]]++;
}
for(int i = ; i<length; i++)
{
if(hash[i]>)
{
*duplication = i;
return true;
}
}
return false;
}
};
思路三:
class Solution {
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) {
if(length<=)
return false;
int temp;
for(int i = ; i<length; i++)
{
if(i==numbers[i])
continue;
else
{
if(numbers[i] == numbers[numbers[i]])
{
*duplication = numbers[i];
return true;
}
else
{
temp = numbers[i];
numbers[i] = numbers[temp];
numbers[temp] = temp;
}
}
}
return false;
}
};
剑指offer:数组中重复的数字的更多相关文章
- 剑指offer数组中重复的数字
package 数组; /*在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的. 也不知道每个数字重复几次.请找出数组中任意一个重复的数字. ...
- 剑指 Offer —— 数组中重复的数字
数组中的重复数字 题目描述 牛课网链接 长度为 n 的数组里,所有数字都在 0 到 n-1 的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一 ...
- 剑指Offer——数组中重复的数字
题目描述: 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度 ...
- 剑指offer 数组中重复的数
在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为7的数组{ ...
- 剑指offer--16.数组中重复的数字
时间限制:1秒 空间限制:32768K 热度指数:198342 本题知识点: 数组 题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复 ...
- 剑指offer 数组中的重复数字
问题描述: 在长度为n的数组中,所有的元素都是0到n-1的范围内. 数组中的某些数字是重复的,但不知道有几个重复的数字,也不知道重复了几次,请找出任意重复的数字. 例如,输入长度为7的数组{2,3,1 ...
- 剑指Offer-数组中重复的数字
package Array; /** * 数组中重复的数字 *在一个长度为n的数组里的所有数字都在0到n-1的范围内. * 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次 ...
- 剑指Offer 数组中只出现一次的数字
题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 思路: 因为有2个数字只出现了一次,而其他的数字都是2次,可以通过异或运算,得到最后这2个只 ...
- python剑指offer数组中出现次数超过一半的数字
题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...
- 剑指Offer——数组中出现次数超过一半的数字——一题多解
看题目: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...
随机推荐
- PDO 对 mysql的基本操作
PDO扩展操作 <?php $dsn = 'mysql:dbname=yii2;host=localhost'; $user = 'root'; $password = '123456'; tr ...
- 控件_ProgressBar
进度条的风格: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...
- Android AbsListView Abs前缀
Android AbsListView Abs abstract:抽象
- SQL的各种连接Join详解
SQL JOIN 子句用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段. 最常见的 JOIN 类型:SQL INNER JOIN(简单的 JOIN).SQL LEFT JOIN.SQL ...
- dubbo报错Data length too large: 10710120处理,及服务提供者协议配置详细说明
工作中遇到以下报错信息 cause: java.io.IOException: Data length too large: 10710120, max payload: 8388608, chann ...
- eclipse 报错Version 1.6.0_45 of the JVM is not suitable for this product. Version:1.7 or greater is required
最近离职来了一家新公司,之前的公司的开发IDE用的是IntelliJIDEA和SpringSourceToolSuit,自己在家里用的也是MyEclipse,所以使用eclipse的经验还是不足.结果 ...
- 一篇文章让你彻底掌握 shell 语言
一篇文章让你彻底掌握 shell 语言 由于 bash 是 Linux 标准默认的 shell 解释器,可以说 bash 是 shell 编程的基础. 本文主要介绍 bash 的语法,对于 linux ...
- Github.Git
Github介绍:http://www.yangzhiping.com/tech/github.html Git:http://res.crossincode.com/wechat/git.html
- java json字符串传递给 js 时 特殊字符转义错误 研究
一些换行 回车等符号需要转义 主要注意 单引号 与双引号. 一 如果js以 双引号接收字符串 则转单引号 " 至 \" 否则js报错 二 如果js以 单引号接收字符串 则转单引号 ...
- 一致性哈希(hash)算法
一.算法背景 一致性哈希算法在1997年由麻省理工学院的Karger等人在解决分布式Cache中提出的,设计目标是为了解决因特网中的热点(Hot spot)问题,初衷和CARP十分类似.一致性哈希修正 ...