#include<cstdio>

 bool duplicate(int numbers[], int length, int* duplication)
{
if (numbers == nullptr || 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]])
{
*duplication = numbers[i]; //
return true;
} int temp = numbers[i];
numbers[i] = numbers[temp];
numbers[temp] = temp; }
return false;
}
}
// test codes
bool contains(int array[], int length, int number)
{
for (int i = ; i < length; ++i)
{
if (array[i] == number)
return true;
}
return false;
}
void test(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 test3()
{
int numbers[] = { ,,,, };
int duplications[] = { , };
test("Test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
} // 无效的输入
void test6()
{
int* numbers = nullptr;
int duplications[] = { - }; // not in use in the test function
test("Test6", numbers, , duplications, sizeof(duplications) / sizeof(int), false);
} // 没有重复的数字
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 test2()
{
int numbers[] = { , , , , };
int duplications[] = { };
test("Test2", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
}
void main()
{
test1();
test2();
test3();
test4();
test5();
test6();
}

<OFFER03>03_01_DuplicationInArray的更多相关文章

  1. 剑指Offer03 逆序输出链表&链表逆序

    多写了个逆序链表 /************************************************************************* > File Name: ...

随机推荐

  1. 在本机搭建vue-cli3项目

    vue-cli3官方网址: https://cli.vuejs.org/zh/ 由于公司开始一个新项目,用到的是vue-cli3搭建的项目,所以自己想搭建一个项目,今天搭建的项目就是一个很简单的项目, ...

  2. SpringMVC 问题 org.springframework.beans.factory.BeanDefinitionStoreException

    HTTP Status 500 – Internal Server Error Type Exception Report Message Servlet.init() for servlet [sp ...

  3. WordPress已占全球网站平台18.9%的份额

    Automattic创始人马特·穆伦维格(Matt Mullenweg)在旧金山的WordCamp会议上谈到了旗下博客平台WordPress的最新发展情况.WordPress平台已成为全球18.9%网 ...

  4. cocos2d-x教程3:用php或DOS批处理命令来转换文件和解压缩zip

    在cocos2d-x使用中,须要不停的转换文件和压缩或解压文件.假设全人工来做,太麻烦了,且easy出错. 我如今把一些用的到批处理贴出来,供大家使用 自己主动把dat文件按数字排序重命名gz.DOS ...

  5. [vue]vue v-on事件绑定(原生修饰符+vue自带事件修饰符)

    preventDefault阻止默认行为和stopPropagation终止传递 event.preventDefault() 链接本来点了可以跳转, 如果注册preventDefault事件,则点了 ...

  6. POJ3096:Surprising Strings(map)

    http://poj.org/problem?id=3096 for循环真是奇妙! #include <string.h> #include <stdio.h> #includ ...

  7. [LeetCode] 785. Is Graph Bipartite?_Medium tag: DFS, BFS

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  8. python 的 json 转换

    python 的 json 转换 本文为原创文章,禁止转载! 本文以 json.dumps()  和 json.loads() 方法进行 Python 数据和 json 格式之间转换,进行讲解 首先比 ...

  9. Django初级手册3-视图层与URL配置

    设计哲学 在Django中一个视图有指定函数和指定模版组成.对于某些特定的应用应该分成若干视图.例如博客系统 Blog主页面 详细页面入口 基于年的页面展示 基于月的页面展示 基于天的页面展示 评论行 ...

  10. 问题排查之'org.apache.rocketmq.spring.starter.core.RocketMQTemplate' that could not be found.- Bean method 'rocketMQTemplate' in 'RocketMQAutoConfiguration' not loaded.

    背景 今天将一个SpringBoot项目的配置参数从原有的.yml文件迁移到Apollo后,启动报错“Bean method 'rocketMQTemplate' in 'RocketMQAutoCo ...