// 面试题3(一):找出数组中重复的数字
// 题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,
// 也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。例如,如果输入长度为7的数组{2, 3, 1, 0, 2, 5, 3},
// 那么对应的输出是重复的数字2或者3。 #include <iostream>
using namespace std; bool duplicate(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;
} for (int i = ; i < length; i++)//核心思想:轮流找自己位置,如果那个位置是你,那你就是重复的
{
while (numbers[i] != i)
{
if (numbers[i] == numbers[numbers[i]])
{
num = numbers[i];
return true;
}
int temp = numbers[i];
numbers[i] = numbers[temp];
numbers[temp] = temp;
}
}
return false;
} // ====================测试代码====================
bool contains(int array[], int length, int number)
{
for (int i = ; i < length; ++i)
{
if (array[i] == number)
return true;
} return false;
} void test(const char* testName, int numbers[], int lengthNumbers, int expected[], int expectedExpected, bool validArgument)
{
printf("%s begins: ", testName); int duplication;
bool validInput = duplicate(numbers, lengthNumbers, duplication); if (validArgument == validInput)
{
if (validArgument)
{
if (contains(expected, expectedExpected, duplication))
printf("Passed.\n");
else
printf("FAILED.\n");
}
else
printf("Passed.\n");
}
else
printf("FAILED.\n");
} // 重复的数字是数组中最小的数字
void test1()
{
int numbers[] = { , , , , };
int duplications[] = { };
test("Test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
} // 重复的数字是数组中最大的数字
void test2()
{
int numbers[] = { , , , , };
int duplications[] = { };
test("Test2", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
} // 数组中存在多个重复的数字
void test3()
{
int numbers[] = { , , , , };
int duplications[] = { , };
test("Test3", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
} // 没有重复的数字
void test4()
{
int numbers[] = { , , , , };
int duplications[] = { - }; // not in use in the test function
test("Test4", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), false);
} // 没有重复的数字
void test5()
{
int numbers[] = { , , , , };
int duplications[] = { - }; // not in use in the test function
test("Test5", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), false);
} // 无效的输入
void test6()
{
int* numbers = nullptr;
int duplications[] = { - }; // not in use in the test function
test("Test6", numbers, , duplications, sizeof(duplications) / sizeof(int), false);
} void main()
{
test1();
test2();
test3();
test4();
test5();
test6(); system("pause");
} //这个代码写的我很难受
//首先是子函数duplicate(),写的时候错了很多地方,比如括号位置,return等等,应该还是不熟练
//其次是人家写的测试代码,我的天,真是,写的太细了
//关注sizeof(numbers) / sizeof(int)

《剑指offer》第三_一题(找出数组中重复的数字,可改变数组)的更多相关文章

  1. 《剑指offer》第十八题(删除链表中重复的结点)

    // 面试题18(二):删除链表中重复的结点 // 题目:在一个排序的链表中,如何删除重复的结点?例如,在图3.4(a)中重复 // 结点被删除之后,链表如图3.4(b)所示. #include &l ...

  2. 剑指offer——python【第54题】字符流中第一个不重复的字符

    题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...

  3. 【剑指offer】面试题68(补充) 0到n-1中缺失的数字(二分法的进一步应用)

    题目 一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字都在范围0到n-1之内. 在范围0到n-1的n个数字中有且只有一个数字不在该数组中,请找出这个数字. 输出 输入:[0,1,2, ...

  4. 剑指offer——python【第56题】删除链表中的重复节点

    题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后 ...

  5. 《剑指offer》第五十题(字符流中第一个只出现一次的字符)

    // 面试题50(二):字符流中第一个只出现一次的字符 // 题目:请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从 // 字符流中只读出前两个字符"go"时,第一 ...

  6. 《剑指offer》面试题53 - II. 0~n-1中缺失的数字

    问题描述 一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字都在范围0-n-1之内.在范围0-n-1内的n个数字中有且只有一个数字不在该数组中,请找出这个数字. 示例 1: 输入: [ ...

  7. 剑指Offer(三十二):把数组排成最小的数

    剑指Offer(三十二):把数组排成最小的数 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/b ...

  8. 剑指Offer(三十七):数字在排序数组中出现的次数

    剑指Offer(三十七):数字在排序数组中出现的次数 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.n ...

  9. 剑指Offer(三十六):两个链表的第一个公共结点

    剑指Offer(三十六):两个链表的第一个公共结点 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...

  10. 剑指Offer(三十五):数组中的逆序对

    剑指Offer(三十五):数组中的逆序对 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/bai ...

随机推荐

  1. selenium webdriver窗口切换(下)

    多窗口切换有时候需要在不同的窗口切换,从而操作不同的窗口上的元素.在selenium1.0 中这个问题比较难处理.但WebDriver 提供了switcTo.window()方法可以切换到任意的窗口. ...

  2. Java缓存学习之六:Spring单独使用EhCache

      通过在Application Context中配置EhCacheManagerFactoryBean和EhCacheFactoryBean,我们就可以把对应的EhCache的CacheManage ...

  3. mustache语法

    mustache 模板,用于构造html页面内容.在实际工作中,当同一个模板中想要调用不同的函数来渲染画面,在已经自定义好了的前提下,可以在渲染页面时对传入的参数进行手动判断.  以下是学习笔记内容: ...

  4. window开机启动项设置和取消方法

    window开机启动项1.添加开机启动项:开始-->所有程序-->启动-->双击(xp系统)或右键打开,把需要启动的软件快捷键拖放进去即可,遇到安全软件的拦截,只需选择 " ...

  5. java多线程-----volatile

    谈谈Java中的volatile   内存可见性 留意复合类操作 解决num++操作的原子性问题 禁止指令重排序 总结 内存可见性 volatile是Java提供的一种轻量级的同步机制,在并发编程中, ...

  6. ACM题目————区间覆盖问题

    题目描述 设x1 , x2,... , xn是实直线上的n个点.用固定长度的闭区间覆盖这n个点,至少需要多少个这样的固定长度闭区间?设计解此问题的有效算法,并证明算法的正确性.编程任务:对于给定的实直 ...

  7. python的时间处理-time模块

    time模块 时间的表示方法有三种: 时间戳:表示的是从1970年1月1日0点至今的秒数 格式化字符串表示:这种表示更习惯我们通常的读法,如2018-04-24 00:00:00 格式化元祖表示:是一 ...

  8. SNMP学习笔记之SNMP的安装及Python的调用

    0x00 概述 本文是介绍SNMP在Windows和Linux(Ubuntu)下的安装,以及通过Python调用其接口的文章. 0x01 开发环境 Python 3.5.1 Windows 10 64 ...

  9. c++ sleep(windows/linux)

    c标准中包含了一个sleep用以实现当前线程暂停执行n毫秒,如下所示: 函数名: sleep 功 能: 执行挂起一段时间 用 法: unsigned sleep(unsigned seconds); ...

  10. PHP安装包TS和NTS的区别

    原文链接:http://blog.csdn.net/zhuifengshenku/article/details/38796555 TS指Thread Safety,即线程安全,一般在IIS以ISAP ...