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 ...
随机推荐
- virtualbox下centos实现主宿互访
1.网络连接方式 NAT 桥接 Host-Only NAT: 网络地址转换,virtualbox默认采用这种连接方式,特点: 1.虚拟机配置稍作修改就能连上外网 2.虚拟机可以ping通主机,主机不能 ...
- 3721:和数-poj
总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个正整数序列,判断其中有多少个数,等于数列中其他两个数的和. 比如,对于数列1 2 3 4, 这个问题的答案就是2, 因为3 = ...
- 【设计模式系列】之OO面向对象设计七大原则
1 概述 本章叙述面向向对象设计的七大原则,七大原则分为:单一职责原则.开闭原则.里氏替换原则.依赖倒置原则.接口隔离原则.合成/聚合复用原则.迪米特法则. 2 七大OO面向对象设计 2.1 单一 ...
- Http简单思维导图
- Django的ModelForm组件
创建类 from django.forms import ModelForm from django.forms import widgets as wd from app01 import mode ...
- HTTP / 1.1 RFC from W3C
这是一篇关于http协议的学习资料: HTTP / 1.1 RFC: https://www.w3.org/Protocols/rfc2616/rfc2616.html HTTP协议的完整资料,请参考 ...
- (翻译)使用Api分析器与Windows兼容包来编写智能的跨平台.NET Core应用
本文翻译自Scott Hanselman博客: https://www.hanselman.com/blog/WritingSmarterCrossplatformNETCoreAppsWithThe ...
- Linux下编译memecaced
安装memecached的时候要先把依赖的软件全部的安装上! 第一步: 在limux编译memcached需要 :yum install gcc make libtool autoconf 着 ...
- 用phpmailer发送邮件提示SMTP Error: Could not connect to SMTP host解决办法
之前做项目的时候做了一个用phpmailer发送邮件的功能<CI框架结合PHPmailer发送邮件>,昨天步署上线(刚开始用新浪云,嫌贵,换成阿里了),测试的时候,发送邮件却意外报错了.. ...
- YiShop_网上商城系统多少钱
电子商务的发展,网上商城系统的开发也变得越来越热门.商城系统开发对于企业来说是非常有必要的,一个好的网上商城系统对于企业来说是非常重要.那么到底网上商城系统多少钱呢?下面YiShop带大家去了解. 网 ...