《剑指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 处理后 ...
随机推荐
- python接口自动化 -参数关联(一)
原文地址https://www.cnblogs.com/yoyoketang/p/6886610.html 原文地址https://www.cnblogs.com/yoyoketang/ 原文地址ht ...
- iOS UI基础-2.0按钮操作与形变
按钮状态 1.normal:默认状态 Default 对应的枚举常量:UIControlStateNormal 2.highlighted(高亮状态) 按钮被按下去的时候(未松开) 对应的枚举常量 ...
- Hibernate—部分
数据持久化的3种方式: merge()方法: 先得到对象的副本:再判断, 如果副本为瞬时状态,则用save()插入 如果副本为游离状态,则用update()更新 最终都是不改变传入对象的状态 save ...
- MIPSsim使用说明
MIPSsim下载:https://files.cnblogs.com/files/jiangxinnju/MIPSsim.zip 启动模拟器 双击MIPSsim.exe,即可启动该模拟器.MIPSs ...
- mybatis项目启动报错 The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collection*,discriminator?)".
启动项目报错 2018-02-26 17:09:51,535 ERROR [org.springframework.web.context.ContextLoader] - Context initi ...
- 解决window.open被拦截问题
最近在项目中有一个在浏览器中新开一个窗口的需求,如果不需要做任何的判断,只是直接新开窗口的话,用a标签即可. 但是如果需要做一些判断再新开一个窗口的话,就不能使用a标签了.window.open确实可 ...
- ubuntu 更改python3为默认版本
ubuntu 自带两个python版本,一个是python2一个是python3 默认版本是python2的,想要更改ubuntu python3 为默认版本, 只需要两行命令: sudo updat ...
- Visual Leak Detector简明使用教程
Visual Leak Detector是一款内存泄漏检测软件,主要的作用就是检测可能或者是存在内存泄露的地方,具体的功能的话,可以百度下,今天主要简单介绍下怎么使用 首先下载Visual Leak ...
- 函数引用参数加const
Fun(const Type& type); 在引用传递的时候,在函数内部改变参数,会改变参数实际值. 加上了const就不能被修改.
- cogs 362. [CEOI2004]锯木厂选址
★★★ 输入文件:two.in 输出文件:two.out 简单对比 时间限制:0.1 s 内存限制:32 MB 从山顶上到山底下沿着一条直线种植了n棵老树.当地的政府决定把他们砍下来. ...