#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<vector>
#include <iostream>
#include<unordered_map>
using namespace std;
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
unordered_map<int, bool> map;
for (int num : nums) {
if (map[num]) return num;
map[num] = true;
}
return -1;
}
};
int main()
{
int out, outt;
vector<int> vec{ 2, 3, 1, 0, 2, 5, 3 };
for (int i = 0; i < 5; i++) {
cout << vec[i] << endl;
}
vector<int>& vect = vec;
cout << "============" << endl;
out = Solution().findRepeatNumber(vec);
cout << out << endl;
outt = Solution().findRepeatNumber(vect);
cout << outt << endl;
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
// 函数声明
void swap(int& x, int& y);
int main()
{
// 局部变量声明
int a = 100;
int b = 200;
int& c = a;
int& d = b;
cout << "交换前,c 的值:" << c << endl;
cout << "交换前,d 的值:" << d << endl;
/* 调用函数来交换值 */
swap(c, d);
cout << "交换后,c 的值:" << c << endl;
cout << "交换后,d 的值:" << d << endl;
return 0;
}
// 函数定义
void swap(int& x, int& y)
{
int temp;
temp = x; /* 保存地址 x 的值 */
x = y; /* 把 y 赋值给 x */
y = temp; /* 把 x 赋值给 y */
return;
}
#include <iostream>
using namespace std;
// 函数声明
void swap(int& x, int& y);
int main ()
{
// 局部变量声明
int a = 100;
int b = 200;
cout << "交换前,a 的值:" << a << endl;
cout << "交换前,b 的值:" << b << endl;
/* 调用函数来交换值 */
swap(a, b);
cout << "交换后,a 的值:" << a << endl;
cout << "交换后,b 的值:" << b << endl;
return 0;
}
// 函数定义
void swap(int& x, int& y)
{
int temp;
temp = x; /* 保存地址 x 的值 */
x = y; /* 把 y 赋值给 x */
y = temp; /* 把 x 赋值给 y */
return;
}
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<vector>
#include <iostream>
#include<unordered_map>
using namespace std;
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
unordered_map<int, bool> map;
for (int num : nums) {
if (map[num] == true) return num;
map[num] = true;
for (auto iter = map.begin(); iter != map.end(); ++iter) {
cout << "<" << iter->first << ", " << iter->second << ">" << endl;
}
cout << "+++++" << endl;
}
for (auto iter = map.begin(); iter != map.end(); ++iter) {
cout << "<" << iter->first << ", " << iter->second << ">" << endl;
}
return -1;
}
};
int main()
{
int out, outt;
vector<int> vec{ 2, 3, 1, 0, 2, 5, 3 };
for (int i = 0; i < 7; i++) {
cout << vec[i] << endl;
}
vector<int>& vect = vec;
cout << "============" << endl;
out = Solution().findRepeatNumber(vec);
cout << out << endl;
//outt = Solution().findRepeatNumber(vect);
//cout << outt << endl;
return 0;
}
具体来讲,unordered_map 容器和 map 容器一样,以键值对(pair类型)的形式存储数据,存储的各个键值对的键互不相同且不允许被修改。但由于 unordered_map 容器底层采用的是哈希表存储结构,该结构本身不具有对数据的排序功能,所以此容器内部不会自行对存储的键值对进行排序。
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<vector>
#include <iostream>
#include<unordered_map>
#include<map>
using namespace std;
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
map<int, bool> map;
for (int num : nums) {
if (map[num] == true) return num;
map[num] = true;
for (auto iter = map.begin(); iter != map.end(); ++iter) {
cout << "<" << iter->first << ", " << iter->second << ">" << endl;
}
cout << "+++++" << endl;
}
for (auto iter = map.begin(); iter != map.end(); ++iter) {
cout << "<" << iter->first << ", " << iter->second << ">" << endl;
}
return -1;
}
};
int main()
{
int out, outt;
vector<int> vec{ 2, 3, 1, 0, 2, 5, 3 };
for (int i = 0; i < 7; i++) {
cout << vec[i] << endl;
}
vector<int>& vect = vec;
cout << "============" << endl;
out = Solution().findRepeatNumber(vec);
cout << out << endl;
//outt = Solution().findRepeatNumber(vect);
//cout << outt << endl;
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
// 参数:
// numbers: 一个整数数组
// length: 数组的长度
// duplication: (输出) 数组中的一个重复的数字
// 返回值:
// true - 输入有效,并且数组中存在重复的数字
// false - 输入无效,或者数组中没有重复的数字
bool duplicate(int numbers[], int length, int* duplication)
{
if (numbers == nullptr || length <= 0)
return false;
for (int i = 0; i < length; ++i)
{
if (numbers[i] < 0 || numbers[i] > length - 1)
return false;
}
for (int i = 0; i < length; ++i)
{
while (numbers[i] != i)
{
if (numbers[i] == numbers[numbers[i]])
{
*duplication = numbers[i];
return true;
}
// 交换numbers[i]和numbers[numbers[i]]
int temp = numbers[i];
numbers[i] = numbers[temp];
numbers[temp] = temp;
}
}
return false;
}
// ====================测试代码====================
bool contains(int array[], int length, int number)
{
for (int i = 0; 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[] = { 2, 1, 3, 1, 4 };
int duplications[] = { 1 };
test("Test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
}
// 重复的数字是数组中最大的数字
void test2()
{
int numbers[] = { 2, 4, 3, 1, 4 };
int duplications[] = { 4 };
test("Test2", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
}
// 数组中存在多个重复的数字
void test3()
{
int numbers[] = { 2, 4, 2, 1, 4 };
int duplications[] = { 2, 4 };
test("Test3", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), true);
}
// 没有重复的数字
void test4()
{
int numbers[] = { 2, 1, 3, 0, 4 };
int duplications[] = { -1 }; // not in use in the test function
test("Test4", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int), false);
}
// 没有重复的数字
void test5()
{
int numbers[] = { 2, 1, 3, 5, 4 };
int duplications[] = { -1 }; // 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[] = { -1 }; // not in use in the test function
test("Test6", numbers, 0, duplications, sizeof(duplications) / sizeof(int), false);
}
int main()
{
test1();
test2();
test3();
test4();
test5();
test6();
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
int countRange(const int* numbers, int length, int start, int end);
// 参数:
// numbers: 一个整数数组
// length: 数组的长度
// 返回值:
// 正数 - 输入有效,并且数组中存在重复的数字,返回值为重复的数字
// 负数 - 输入无效,或者数组中没有重复的数字
int getDuplication(const int* numbers, int length)
{
if (numbers == nullptr || length <= 0)
return -1;
int start = 1;
int end = length - 1;
while (end >= start)
{
int middle = ((end - start) >> 1) + start;
int count = countRange(numbers, length, start, middle);
if (end == start)
{
if (count > 1)
return start;
else
break;
}
if (count > (middle - start + 1))
end = middle;
else
start = middle + 1;
}
return -1;
}
int countRange(const int* numbers, int length, int start, int end)
{
if (numbers == nullptr)
return 0;
int count = 0;
for (int i = 0; i < length; i++)
if (numbers[i] >= start && numbers[i] <= end)
++count;
return count;
}
// ====================测试代码====================
void test(const char* testName, int* numbers, int length, int* duplications, int dupLength)
{
int result = getDuplication(numbers, length);
for (int i = 0; i < dupLength; ++i)
{
if (result == duplications[i])
{
std::cout << testName << " passed." << std::endl;
return;
}
}
std::cout << testName << " FAILED." << std::endl;
}
// 多个重复的数字
void test1()
{
int numbers[] = { 2, 3, 5, 4, 3, 2, 6, 7 };
int duplications[] = { 2, 3 };
test("test1", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}
// 一个重复的数字
void test2()
{
int numbers[] = { 3, 2, 1, 4, 4, 5, 6, 7 };
int duplications[] = { 4 };
test("test2", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}
// 重复的数字是数组中最小的数字
void test3()
{
int numbers[] = { 1, 2, 3, 4, 5, 6, 7, 1, 8 };
int duplications[] = { 1 };
test("test3", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}
// 重复的数字是数组中最大的数字
void test4()
{
int numbers[] = { 1, 7, 3, 4, 5, 6, 8, 2, 8 };
int duplications[] = { 8 };
test("test4", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}
// 数组中只有两个数字
void test5()
{
int numbers[] = { 1, 1 };
int duplications[] = { 1 };
test("test5", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}
// 重复的数字位于数组当中
void test6()
{
int numbers[] = { 3, 2, 1, 3, 4, 5, 6, 7 };
int duplications[] = { 3 };
test("test6", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}
// 多个重复的数字
void test7()
{
int numbers[] = { 1, 2, 2, 6, 4, 5, 6 };
int duplications[] = { 2, 6 };
test("test7", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}
// 一个数字重复三次
void test8()
{
int numbers[] = { 1, 2, 2, 6, 4, 5, 2 };
int duplications[] = { 2 };
test("test8", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}
// 没有重复的数字
void test9()
{
int numbers[] = { 1, 2, 6, 4, 5, 3 };
int duplications[] = { -1 };
test("test9", numbers, sizeof(numbers) / sizeof(int), duplications, sizeof(duplications) / sizeof(int));
}
// 无效的输入
void test10()
{
int* numbers = nullptr;
int duplications[] = { -1 };
test("test10", numbers, 0, duplications, sizeof(duplications) / sizeof(int));
}
void main()
{
int i = 1, j = 7, out;
out = j - i;
std::cout << out << std:: endl;
int middle = ((j - i) >> 1);//右移运算符
std::cout << middle<< std::endl;
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
test10();
}