[小米OJ] 2. 找出单独出现的数字
解法一:
map
1.45 ms
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std; int main()
{
int temp;
map<int, int> mapping;
while (scanf("%d", &temp) != EOF)
{
mapping[temp]++;
}
for (map<int, int>::iterator it = mapping.begin(); it != mapping.end(); it++)
{
if (it->second == )
{
printf("%d", it->first);
}
}
system("pause");
return ;
}
解法二:
因为题目提出“其中仅有一个数字出现过一次,其他数字均出现过两次”,即可以利用异或计算
一个数字异或它自己结果为0,异或0结果为它自己即a^a=0,a^0=a,且异或满足a^b^c=a^(b^c)。
因此我们可以设置一个ret异或每个元素,最后相同的都抵消为0,那个唯一的数字异或0为它自己即为答案。
1.97 ms
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std; int main()
{
int temp;
int arr[];
int i = ;
while (scanf("%d", &temp) != EOF)
{
arr[i] = temp;
i++;
}
int ret = ;
for (int j = ; j < i; j++)
{
ret ^= arr[j];
}
printf("%d",ret);
system("pause");
return ;
}
[小米OJ] 2. 找出单独出现的数字的更多相关文章
- 【小米oj】找出单独的数字
题目链接:https://code.mi.com/problem/list/view?id=2&cid=0&sid=26251#codearea 描述 给出N个数字.其中仅有一个数字出 ...
- [小米 Online Judge]找出单独出现的数字
描述: 给出N个数字.其中仅有一个数字出现过一次,其他数字均出现过两次,找出这个出现且只出现过一次的数字.要求时间和空间复杂度最小. 输入: 输入多个数字,每个数字以空格分开,回车结束 输出: 输出内 ...
- 小米OJ 12. 找出可能的合的组合
利用dfs解决,从给出的数组左边或右边开始遍历,对每一个数字进行判断,有三种情况: 1. 加上当前数字的值,遍历下一个数字 2. 加上当前数字的值,继续遍历该数字 3. 不加上当前的数字的值,遍历下一 ...
- [小米OJ] 5. 找出旋转有序数列的中间值
排序,输出 #include <bits/stdc++.h> using namespace std; int main() { string input; while (cin > ...
- 剑指Offer38 数组所有数字出现两次,只有两个出现了一次,找出这两个数字
/************************************************************************* > File Name: 38_Number ...
- Java初学者作业——编写Java程序,在控制台中输入一个数字,要求定义方法实现找出能够整除该数字的所有数字。
返回本章节 返回作业目录 需求说明: 编写Java程序,在控制台中输入一个数字,要求定义方法实现找出能够整除该数字的所有数字. 实现思路: 定义方法findNums(),用于实现查找所有能够整除指定数 ...
- 九度OJ 1035:找出直系亲属(二叉树)
题目1035:找出直系亲属 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1309 解决:521 题目描述: 如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如 ...
- 15. 3Sum_左右开工,遍历找出符合目标的数字
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
- 【SQLSERVER】如何找出字符串中的数字
可以通过写自定义函数实现,以下提供两种思路来解决: 1.通过正则匹配,找到字符串中的数字,一个一个拼起来 /*方法一: 一个一个找出来*/ CREATE FUNCTION [dbo].[Fun_Get ...
随机推荐
- Delphi For Linux Compiler
Embarcadero is about to release a new Delphi compiler for the Linux platform. Here are some of the k ...
- stdlib.h,string.h,wchar.h的函数列表(cplusplus.com就有,很清楚)goodx
Multibyte characters mblen Get length of multibyte character (function ) mbtowc Convert multibyte se ...
- 剖析Qt的事件机制原理(源代码级别)
在用Qt写Gui程序的时候,在main函数里面最后依据都是app.exec();很多书上对这句的解释是,使Qt程序进入消息循环.下面我们就到exec()函数内部,来看一下他的实现原理.Let's go ...
- sqlserver/mysql按天,按小时,按分钟统计连续时间段数据
文 | 子龙 有技术,有干货,有故事的斜杠青年 一,写在前面的话 最近公司需要按天,按小时查看数据,可以直观的看到时间段的数据峰值.接到需求,就开始疯狂百度搜索,但是搜索到的资料有很多都不清楚,需要自 ...
- 基于Common.Logging + Log4Net实现的日志管理
前言 Common.Logging 是Commons-Logging(apache最早提供的日志门面接口,提供了简单的日志实现以及日志解耦功能) 项目的.net版本.其目的是为 "所有的.n ...
- node.js中模块,require
在php,C++中都有命名空间的概念,命名空间主要是用来解决引入文件存在函数,类,变量重名的问题,在node.js中,没有命名空间这么复杂的概念,在node中,有模块的概念,也就是将功能性的代码都放在 ...
- C# 死锁 TaskCompletionSource
在异步转同步时,使用不当容易造成死锁(程序卡死) 看如下案例: 有一个异步方法 private static async Task TestAsync() { Debug.WriteLine(&quo ...
- Apple官文中的KVO 与 FBKVOController
前言 本文将主要介绍以下内容: 详细列出Apple官文中KVO的注意事项(Apple KVO相关的引用皆摘自Apple官文). 介绍FBKVOController,以及它如何避免系统提供的KVO坑点. ...
- Js笛卡尔乘积
self.getDescartesSku = function (selSaleProp, i, nowLst, allALst) { if (selSaleProp.length = ...
- ArrayList的add方法实现
ArrayList的底层是由数组实现,所以所有的操作都是围绕数组展开,要想理解add方法,就得先了解数组的增加,所以我们先实现一个数组的add,数组的添加可以从尾部增加或者其他位置插入, 如果在数组的 ...