《剑指offer》第三_二题(不修改数组找出重复的数字)
// 面试题3(二):不修改数组找出重复的数字
// 题目:在一个长度为n+1的数组里的所有数字都在1到n的范围内,所以数组中至
// 少有一个数字是重复的。请找出数组中任意一个重复的数字,但不能修改输入的
// 数组。例如,如果输入长度为8的数组{2, 3, 5, 4, 3, 2, 6, 7},那么对应的
// 输出是重复的数字2或者3。 #include <iostream>
using namespace std;
int counter(const int*, int, int, int); bool getDuplication(const int* numbers, int length, int& num)
{
if (numbers == NULL || length <= )//判断输入是否对
return false; for (int i = ; i < length; i++)//判断是否满足题目条件
{
if (numbers[i] < || numbers[i] > length)
return false;
} int start = , end = length - ; while (end >= start)
{
int middle = ((end - start) >> ) + start;
int count = counter(numbers, length, start, middle);//查找落在二分左区间内个数 //cout << "start=" << start << endl << "middle=" << middle << endl << "end=" << end << endl<< "count=" << count << endl; if (start == end)//二分不动了,停止,判断这个值count值
{
if (count > )
{
num = start;
return true;
}
else
break; } if (count > (middle - start) + )//如果落在左区间的个数大于区间范围,则这里面一定有重复,否则就去右区间看看
end = middle;
else
start = middle + ;
} return false;
} int counter(const int* numbers, int length, int start, int middle)
{
int count = ;
if (numbers == NULL || start > middle || start < )
return count; for (int i = ; i < length; i++)
{
if (numbers[i] >= start&&numbers[i] <= middle)
count++;
}
return count;
} void test(const char* testName, int* numbers, int length, int* duplications, int dupLength)
{
int result;
bool flag = getDuplication(numbers, length, result); if (!flag)
result = -; for (int i = ; i < dupLength; ++i)
{
if (result == duplications[i])
{
std::cout << testName << " passed." << std::endl;
return;
}
}
std::cout << testName << " FAILED." << std::endl;
} // 多个重复的数字
void test1()
{
int numbers[] = { , , , , , , , };
int duplications[] = { , };
test("test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 一个重复的数字
void test2()
{
int numbers[] = { , , , , , , , };
int duplications[] = { };
test("test2", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 重复的数字是数组中最小的数字
void test3()
{
int numbers[] = { , , , , , , , , };
int duplications[] = { };
test("test3", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 重复的数字是数组中最大的数字
void test4()
{
int numbers[] = { , , , , , , , , };
int duplications[] = { };
test("test4", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 数组中只有两个数字
void test5()
{
int numbers[] = { , };
int duplications[] = { };
test("test5", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 重复的数字位于数组当中
void test6()
{
int numbers[] = { , , , , , , , };
int duplications[] = { };
test("test6", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 多个重复的数字
void test7()
{
int numbers[] = { , , , , , , };
int duplications[] = { , };
test("test7", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 一个数字重复三次
void test8()
{
int numbers[] = { , , , , , , };
int duplications[] = { };
test("test8", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 没有重复的数字
void test9()
{
int numbers[] = { , , , , , };
int duplications[] = { - };
test("test9", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
} // 无效的输入
void test10()
{
int* numbers = nullptr;
int duplications[] = { - };
test("test10", numbers, , duplications, sizeof(duplications) / sizeof(int));
} void main()
{
test1();
test2();
test3();//判断边界
test4();//判断边界
test5();
test6();
test7();
test8();
test9();
test10(); system("pause");
}
《剑指offer》第三_二题(不修改数组找出重复的数字)的更多相关文章
- 一起来刷《剑指Offer》——不修改数组找出重复的数字(思路及Python实现)
数组中重复的数字 在上一篇博客中<剑指Offer>-- 题目一:找出数组中重复的数字(Python多种方法实现)中,其实能发现这类题目的关键就是一边遍历数组一边查满足条件的元素. 然后我们 ...
- 剑指Offer(三十二):把数组排成最小的数
剑指Offer(三十二):把数组排成最小的数 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/b ...
- 【Java】 剑指offer(2) 不修改数组找出重复的数字
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 在一个长度为n+1的数组里的所有数字都在1到n的范围内,所以数组中至少 ...
- 【Offer】[3-2] 【不修改数组找出重复的数字】
题目描述 思路分析 Java代码 代码链接 题目描述 在一个长度为n+1的数组里的所有数字都在1~n的范围内,所以数组中至少有一个数字是重复的. 请找出数组中任意一个重复的数字,但不能修改输入的数组. ...
- 【剑指 Offer】03.1.不修改数组找出重复的数字
找出数组中重复的数字. 在一个长度为 n + 1 的数组 nums 里的所有数字都在 1-n 的范围内.所以数组中至少有一个是重复的.请找出数组中任意一个重复的数字. 示例 1: 输入: [2, 3, ...
- 《剑指offer》第五十三题(0到n-1中缺失的数字)
// 面试题53(二):0到n-1中缺失的数字 // 题目:一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字 // 都在范围0到n-1之内.在范围0到n-1的n个数字中有且只有一个数 ...
- 《剑指offer》第二十二题(链表中倒数第k个结点)
// 面试题22:链表中倒数第k个结点 // 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯, // 本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点, ...
- 《剑指offer》第十二题(矩阵中的路径)
// 面试题:矩阵中的路径 // 题目:请设计一个函数,用来判断在一个矩阵中是否存在一条包含某字符串所有 // 字符的路径.路径可以从矩阵中任意一格开始,每一步可以在矩阵中向左.右. // 上.下移动 ...
- 剑指offer——python【第56题】删除链表中的重复节点
题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后 ...
随机推荐
- Linux系统——PXE高效能批量网络装机
PXE:Pre-boot Excution Environment,预启动执行环境,石油Intel公司开发的网络引导技术,工作在Client.Server模式,允许客户机通过网络从远程服务器下载阴道镜 ...
- TypeScript 基础入门(一)
1.TypeScript是什么? TypeScript 是 JavaScript 的一个超集,TypeScript 在 JavaScript 的基础上添加了可选的 静态类型 和基于 类 的面向对象编程 ...
- linux服务后台管理
把进程放到后台有两种方法 1.cmmand & 2.ctrl+z 暂停到后台 查看后台服务 jobs 把后台进程移到前台 fg %2 工作号 恢复到前台 后台服务继续执行 bg ...
- Makefile小结
Makefile最基本的规则:target....:prerequisites..... command 或:target....:prerequisites.....;command target: ...
- 20154312 曾林 EXP9 Web安全基础
目录 -0.webgoat Could not find source file -1.基础问题回答 -2.环境配置 -3.Injection Flaws ----3.1.Numeric SQL In ...
- MSF渗透测试-CVE-2017-11882(MSOffice漏洞)
1.测试环境 2.测试前准备 3.测试过程 -3.1虚拟机环境测试 -3.2局域网靶机测试 4.测试感想 1.测试环境 攻击机: OS:kail IP:192.168.15.132/192.168.1 ...
- linux常用命令:tr 命令
tr 命令实现字符转换功能,其功能类似于 sed 命令,但是,tr 命令比 sed 命令简单.也就是说,tr 命令能实现的功能,sed 命令都可以实现.尽管如此,tr 命令依然是 Linux 系统下处 ...
- linux服务器---配置bind
配置bind 1.确定已经安装bind软件,需要安装3 个bind.bind-chroot.bind-util [root@localhost wj]# yum install –y bind bin ...
- Linux服务器---关闭selinux
关闭selinux 1.通过命令“getenforce”获取selinux状态, [root@localhost ~]# getenforce Enforcing //enforcein ...
- 汽车OBD接口定义
汽车上的OBD-II接口(母): ELM327用到的引脚: 2: SAE-J1850 PWM和SAE-1850 VPW总线(+) 4. 车身地 5. 信号地 6. CAN high (ISO 157 ...