在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3
// test14.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<string>
#include<cctype>
#include <vector>
#include<exception>
#include <initializer_list>
using namespace std;
class Solution {
public:
// Parameters:
//numbers: an array of integers
//length: the length of array numbers
//duplication: (Output) the duplicated number in the array number
// Return value: true if the input is valid, and there are some duplications in the array number
// otherwise false
bool duplicate(int numbers[], int length, int* duplication) {
int k = 0;//统计duplication数组中的元素个数
bool result = false; //返回结果
//对numbers数组进行排序
for (int i = 0; i < length; i++)
{
for (int j = i+1; j < length; j++)
{
if (numbers[i] > numbers[j])
{
int t = numbers[j];
numbers[j] = numbers[i];
numbers[i] = t;
}
}
}
//cout << "numbers:";
//for (int i = 0; i < length; i++)
//{
// cout << numbers[i] << " ";
//}
//cout << endl;
int temp = numbers[0];
for (int i = 1; i < length; i++)
{
if (temp == numbers[i])//如果有重复
{
if(k==0)//duplication中没有数据进行的处理
{
duplication[k++] = temp;
result = true;
}
else if (temp != duplication[k - 1])//duplication中有数据要进行的判断,如果没有存储,需要存储
{
duplication[k++] = temp;
result = true;
}
else // duplication中有数据要进行的判断,如果已经存储,不需要做处理
{
result = true;
}
}
else//如果和之前数据没有相同的,temp等于这个数据
{
temp = numbers[i];
}
}
/* cout << "duplication:";
for (int i = 0; i < k; i++)
{
cout << duplication[i] << " ";
}
cout << endl;*/
return result;
}
};
int main()
{
int a[7] = { 2,3,1,0,2,5,3 };
int b[7] = { 0 };
int* duplication=b;
Solution so;
so.duplicate(a, 7, duplication);
return 0;
}
思路:先排序,然后统计计算
在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3的更多相关文章
- 【c语言】数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字
题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 比如输入一个长度为9的数组{1,2.3.2,2.2.5,4.2}, 因为数组中数字2出现了5次,超过数组的长度的一半,因此输出2 ...
- 找出字符串中第一个不重复的字符(JavaScript实现)
如题~ 此算法仅供参考,小菜基本不懂高深的算法,只能用最朴实的思想去表达. //找出字符串中第一个不重复的字符 // firstUniqueChar("vdctdvc"); --& ...
- 最短路径(给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动一步。)
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 例: 输入: [ [1,3,1], [1,5,1], [ ...
- 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
package algorithms; /* 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. public class ListNode { int val; ListNo ...
- 如何快速找出Linux中的重复文件
md5sum | sort | uniq -w32 --all-repeated=separate [1]find -not -empty -type f -printf “%s\n” :find是查 ...
- 数据库中通过group by找出表中的重复数据
有时候在做数据割接时会碰到数据插入失败的情况,大部分都是导出的数据中存在重复导致的.我们可以通过查询语句带分组条件来确认是否有重复数据.例如我现在有表 t_wlf_info,其中有个 username ...
- 优雅的找出ArrayList中重复的元素
https://blog.csdn.net/caoxiaohong1005/article/details/54286384
- 最接近的三数之和(给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数, 使得它们的和与 target 最接近。返回这三个数的和)
例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 思路:首先对数组进行排序 ...
- 【SQLSERVER】如何找出字符串中的数字
可以通过写自定义函数实现,以下提供两种思路来解决: 1.通过正则匹配,找到字符串中的数字,一个一个拼起来 /*方法一: 一个一个找出来*/ CREATE FUNCTION [dbo].[Fun_Get ...
随机推荐
- php 提交保存成功页面 倒计时 跳转
前几天做了一个简单的成功提示页面! 有需要的可以拿去用,写的不好 欢迎指正!~~ 因为工程是在CI下面做的,url 自己用的话需要改正下函数!site_url() 这个函数式CI框架的 <ht ...
- 细说SQL 连接
连接条件可在FROM或WHERE子句中指定,建议在FROM子句中指定连接条件.WHERE和HAVING子句 也可以包含搜索条件,以进一步筛选连接条件所选的行. ...
- AMQ学习笔记 - 14. 实践方案:基于ZooKeeper + ActiveMQ + replicatedLevelDB的主从部署
概述 基于ZooKeeper + ActiveMQ + replicatedLevelDB,在Windows平台的主从部署方案. 主从部署可以提供数据备份.容错[1]的功能,但是不能提供负载均衡的功能 ...
- css笔记——关于css中写上charset “utf-8”
当css文件中写上 charset "utf-8" 时需要将body和html的样式分开写 例如: html,body{margin:0;padding:0;font-family ...
- arcgis直连oracle
参数:service :sde:oracle10g(客户端的数据库版本)username:sdepassword:sde@s19(配置的网络连接名) 黄色背景为需要根据实际情况更改的参数内容.如更改为 ...
- Headfirst设计模式的C++实现——观察者模式(Observer)
WeatherData.h #ifndef WEATHERDATA_H_INCLUDED #define WEATHERDATA_H_INCLUDED #include <set> #in ...
- DEDECMS中,文章页直接输出字段名
文章页中,可直接输出字段名
- ubuntu设置关闭屏幕和锁定
见链接:http://askubuntu.com/questions/177348/how-do-i-disable-the-screensaver-lock If you want to wrap ...
- grep恢复误删除文件内容(转)
在 Linux 上如果事先没有用别名(alias)修改默认的 rm 功能,rm 后文件就会丢失,幸运的是,在一般的删除文件操作中,Linux 并不会立即清空存储该文件的 block 内容,而只会释放该 ...
- javascript里面技巧整理
web develop tools secrets: http://jinlong.github.io/blog/2013/08/29/devtoolsecrets/ 1.Date new Date( ...