Find Unique pair in an array with pairs of numbers 在具有数字对的数组中查找唯一对
给定一个数组,其中每个元素出现两次,除了一对(两个元素)。找到这个唯一对的元素。
输入:
第一行输入包含一个表示测试用例数的整数T。然后T测试用例如下。每个测试用例由两行组成。每个测试用例的第一行包含整数N表示数组的大小,第二行包含N个空格分隔元素。
输出:
对于每个测试用例, 在新行中以增加的顺序打印唯一对。
约束:
1 <= T <= 100
1 <= N <= 10 3
1 <= A [i] <= 10 3
示例:
输入:
2
6
2 2 5 5 6 7
4
1 3 4 1
输出:
6 7
3 4
这个题算是最简单的一个题目,写下我的解题思路。
核心在于怎么找出来这一对元素:假设数组存放在b数组中,因为数组元素的大小是1-1000,所以新建数组a使其大小是1000,初始化为0。
依次将b中元素对应于a中的下标进行递增。如果最终a中是奇数的就是目标答案。只需要遍历一遍就可以了。
下面是具体代码实现:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;//具体执行几次
scanf("%d",&n);
int *num=(int*)malloc(sizeof(int)*n);//num数组存放每个数组的大小.
int i,j;//i是循环变量
;i<n;i++)
{
scanf("%d\n",&num[i]);//读入第i个数组元素的个数。
int *b=(int*)malloc(sizeof(int)*num[i]);//分配b数组空间
;j<num[i];j++)
scanf("%d",&b[j]);//依次读入数组元素.
]={};//初始化为0
;j<num[i];j++)
a[b[j]-]++;//对数组a,b进行处理。
;j<;j++)
{
==)
printf());
}
printf("\n");
}
;
}
如果存在任何不理解,请留言。
Find Unique pair in an array with pairs of numbers 在具有数字对的数组中查找唯一对的更多相关文章
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
- 【leetcode】Merge Sorted Array(合并两个有序数组到其中一个数组中)
题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assum ...
- [Swift]LeetCode34. 在排序数组中查找元素的第一个和最后一个位置 | Find First and Last Position of Element in Sorted Array
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
- C#LeetCode刷题之#34-在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4970 访问. 给定一个按照升序排列的整数数组 nums,和一个目 ...
- 【LeetCode】1095. 山脉数组中查找目标值 Find in Mountain Array
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetco ...
- LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- 28.earch in Rotated Sorted Array(排序旋转数组中查找)
Level: Medium 题目描述: Suppose an array sorted in ascending order is rotated at some pivot unknown to ...
- Leetcode34.Find First and Last Position of Element in Sorted Array在排序数组中查找元素的位置
给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...
- [LeetCode] K-diff Pairs in an Array 数组中差为K的数对
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...
随机推荐
- 安卓自定义控件(四)实现自定义Layout
本来我是不准备写这篇文章的,我实在想不出有什么样奇怪的理由,会去继承ViewGroup然后自定义一个布局,大概是我的项目经验还不够吧,想了好久,想到了这样一个需求: 需求 如图:在项目中经常有一个这样 ...
- Less的条件表达式
Less的条件表达式 当需要根据表达式,而不是参数的值或数量进行匹配时,条件表达式(Guards)就显得非常有用.如果你熟悉函数式编程的话,对条件表达式也不会陌生. 为了尽可能地接近CSS的语言结构, ...
- Tinc VPN
服务端配置 安装 $ apt-get install tinc 配置 $ mkdir -p /etc/tinc/dock/hosts $ cd /etc/tinc/dock 配置 tinc.conf ...
- ShoneSharp语言(S#)的设计和使用介绍系列(2)— 掀开盖头
ShoneSharp语言(S#)的设计和使用介绍 系列(2)- 掀开盖头 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/ShoneSharp. ...
- Self Hosting WebServer 的几种方式
写在前面: IIS是Windows平台非常关键的组件,它是微软自带的Web服务器,可以很方便的帮助我们运行起一个网站,WebApi等服务,提供给外部来访问.即使它被很多java或者ruby的同学各种鄙 ...
- 浅谈IM(InstantMessaging) 即时通讯/实时传讯
一.IM简要概述 IM InstantMessaging(即时通讯,实时传讯)的缩写是IM,互动百科大致解释是一种可以让使用者在网络上建立某种私人聊天(chatroom)的实时通讯服务. 大部 ...
- 五十个小技巧提高PHP执行效率(二)
更详细具体的总结如下: 1.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量, 单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的 ...
- jQuery选取所有复选框被选中的值并用Ajax异步提交数据
昨天和朋友做一个后台管理系统项目的时候涉及到复选框批量操作,如果用submit表单提交挺方便的,但是要实现用jQuery结合Ajax异步提交数据就有点麻烦了,因为我之前做过的项目中基本上没用Ajax来 ...
- Android测试:Testing Apps on Android
原文:https://developer.android.com/training/testing/index.html 测试你的App是开发过程中的重要组成部分.通过对应用程序持续的运行测试,你可以 ...
- CS Round#50 D min-races
Min Races Time limit: 1000 msMemory limit: 256 MB In a racing championship there are N racing driv ...