// 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的更多相关文章

  1. 【c语言】数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字

    题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. 比如输入一个长度为9的数组{1,2.3.2,2.2.5,4.2}, 因为数组中数字2出现了5次,超过数组的长度的一半,因此输出2 ...

  2. 找出字符串中第一个不重复的字符(JavaScript实现)

    如题~ 此算法仅供参考,小菜基本不懂高深的算法,只能用最朴实的思想去表达. //找出字符串中第一个不重复的字符 // firstUniqueChar("vdctdvc"); --& ...

  3. 最短路径(给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动一步。)

    给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 例: 输入: [ [1,3,1], [1,5,1], [ ...

  4. 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。

    package algorithms; /* 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null. public class ListNode { int val; ListNo ...

  5. 如何快速找出Linux中的重复文件

    md5sum | sort | uniq -w32 --all-repeated=separate [1]find -not -empty -type f -printf “%s\n” :find是查 ...

  6. 数据库中通过group by找出表中的重复数据

    有时候在做数据割接时会碰到数据插入失败的情况,大部分都是导出的数据中存在重复导致的.我们可以通过查询语句带分组条件来确认是否有重复数据.例如我现在有表 t_wlf_info,其中有个 username ...

  7. 优雅的找出ArrayList中重复的元素

    https://blog.csdn.net/caoxiaohong1005/article/details/54286384

  8. 最接近的三数之和(给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数, 使得它们的和与 target 最接近。返回这三个数的和)

    例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 思路:首先对数组进行排序     ...

  9. 【SQLSERVER】如何找出字符串中的数字

    可以通过写自定义函数实现,以下提供两种思路来解决: 1.通过正则匹配,找到字符串中的数字,一个一个拼起来 /*方法一: 一个一个找出来*/ CREATE FUNCTION [dbo].[Fun_Get ...

随机推荐

  1. JS闭包理解_摘

    原文地址1:http://www.cnblogs.com/mzwr1982/archive/2012/05/20/2509295.html 闭包是一个比较抽象的概念,尤其是对js新手来说.书上的解释实 ...

  2. SQL中各数据类型的长度、精度

    在 Microsoft? SQL Server? 中,每个列.局部变量.表达式和参数都有一个相关的数据类型,这是指定对象可持有的数据类型(整型.字符.money 等等)的特性. SQL Server ...

  3. byte[] 清空

    1. using(byte buff = new byte[Size]){  // 你要用的代码,} 2. Array.Clear(bytes, 0 ,bytes.Length);

  4. trigger()的event事件对象之坑

    问题引入,先贴一段有问题的代码,如果你对 trigger()  这个函数了解不透彻,还真看不出这段代码错在哪.完成的功能是样式转换器,想让页面在加载后自行触发点击事件隐藏三个按钮,但是效果如图并没有隐 ...

  5. 一款类似塔防类的保卫羊村游戏android源码

    一款类似塔防类的保卫羊村游戏源码,这个游戏很像我们平时玩的塔防游戏的,游戏的源码比较完整的,大家可以修改一下或者添加一些广告就可以上线到应用商店了,而且这个游戏目前已经上线国内的一些应用商店了,360 ...

  6. unity 3消 游戏

    3消游戏跟着智能手机流行到现在已经有很长一段时间,unity实现的3消 https://github.com/textcube/match3action 截图如下: 在阅读源码的时候不难发现,Game ...

  7. 无法安装程序包“MIcrosoft.Owin.Security 2.0.2”。您正在尝试将此程序包安装到某个将“.NETFramework,Version=v4.0”作为目标的项目中。

    在VS2010 MVC4项目中,安装NuGet程序包Microsoft.AspNet.SignalR时出现以下错误: 原因是安装的版本是Microsoft.AspNet.SignalR 2.0.2,要 ...

  8. 分享:JS比较两个日期大小

    发布:thatboy   来源:Net     [大 中 小] 本文介绍下,在javascript代码中,比较两个日期大小的方法,有需要的朋友参考下. 转自:http://www.jbxue.com/ ...

  9. this.IsMounted() is not a function

    I'm trying to build a simple React App. It retrieves data from an ajax call and renders it to the pa ...

  10. Microsoft server software support for Microsoft Azure virtual machines

    http://support.microsoft.com/kb/2721672/en-us  Article ID: 2721672 - Last Review: November 22, 2014 ...