Lonely Integer
https://www.hackerrank.com/challenges/lonely-integer
def main():
n = int(raw_input())
s = dict()
a = 0 arr = map(int,raw_input().strip().split())
for _ in range(n):
num = arr[_]
if not s.get(num):
s.update({num:1})
else:
a = s.get(num)
a += 1
s.update({num:a}) for i in s:
if s.get(i) % 2 != 0:
print i
break main()
学习
怎样利用新的数据结构“字典”来解决问题
//另一个方式:过滤
#!/usr/bin/py
def lonelyinteger(a):
answer = 0
answer = reduce(lambda x,y: x^y, a)
return answer
if __name__ == '__main__':
a = input()
b = map(int, raw_input().strip().split(" "))
print lonelyinteger(b)
学习
http://jeffxie.blog.51cto.com/1365360/328207 三个过滤函数
神奇的xor运算
异或运算有两个特性:
1、一个数异或本身恒等于0,如5^5恒等于0;
2、一个数异或0恒等于本身,如5^0恒等于5。
所以比如 print 0 ^ 0 ^ 1 ^ 2 ^ 1
根据结合律,所有相同的都消除为0,而0和最后孤单的值,成为孤单值本身
Lonely Integer的更多相关文章
- 【HackerRank】Lonely Integer
There are N integers in an array A. All but one integer occur in pairs. Your task is to find out the ...
- [LeetCode] Lonely Pixel II 孤独的像素之二
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- [LeetCode] 533. Lonely Pixel II 孤独的像素 II
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- LeetCode 7. Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you ...
- Integer.parseInt 引发的血案
Integer.parseInt 处理一个空字符串, 结果出错了, 程序没有注意到,搞了很久, 引发了血案啊!! 最后,终于 观察到了, 最后的部分: Caused by: java.lang.NoC ...
- 由一个多线程共享Integer类变量问题引起的。。。
最近看到一个多线程面试题,有三个线程分别打印A.B.C,请用多线程编程实现,在屏幕上循环打印10次ABCABC- 看到这个题目,首先想到的是解决方法是定义一个Integer类对象,初始化为0,由3个线 ...
- [LeetCode] Integer Replacement 整数替换
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If ...
- [LeetCode] Integer Break 整数拆分
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
随机推荐
- canvas个人总结
今天做了大量的canvas作业,发现很多的步奏都是一样的,我自己就封装了一个画直线形2D图形函数.功能不是很强大. function drawModule(Json,strokeStyle,fillS ...
- Swift—属性观察者-备
为了监听属性的变化,Swift提供了属性观察者.属性观察者能够监听存储属性的变化,即便变化前后的值相同,它们也能监听到. 属性观察者主要有以下两个: willSet:观察者在修改之前调用. didSe ...
- ROC曲线和PR曲线
转自:http://www.zhizhihu.com/html/y2012/4076.html分类.检索中的评价指标很多,Precision.Recall.Accuracy.F1.ROC.PR Cur ...
- QEventLoop 的使用两例
熟悉的陌生人 Qt 是事件驱动的,所以当你用Qt的时候,几乎时时刻刻和 QEventLoop 打交道.,只是你可能没有意识到: QCoreApplicaton::exec() QApplication ...
- VS IDE环境下,windows GUI(Qt MFC,win32)使用控制台实时打印调试信息
在工程属性的页面下,点击Build Events,在Build Events下点击Post-Build Event. 然后再Command Line里面输入以下命令: editbin /SUBSYST ...
- logstash tomcat catalina.out zabbix 插件不会引起崩溃
input { file { type => "zj_api" path => ["/data01/applog_backup/zjzc_log/zj-api ...
- JAVA在线基础教程!
http://www.runoob.com/java/java-tutorial.html http://www.51zxw.net/list.aspx?cid=380 http://www.weix ...
- Lambda 表达式的示例-来源(MSDN)
本文演示如何在你的程序中使用 lambda 表达式. 有关 lambda 表达式的概述,请参阅 C++ 中的 Lambda 表达式. 有关 lambda 表达式结构的详细信息,请参阅 Lambda 表 ...
- c语言枚举型常量
#include <stdio.h> //代表百度工程师的级别 enum level { //如果没有指定初始的值 那么c语言会自动分配一个编号 整数编号 T14=,T13=,T12=,T ...
- python数据类型-布尔值
布尔是计算机里最基本的判断单位,布尔只有两个值:真或假,即True False,也就是1或0. 在以后学习流程控制时会经常用到布尔值. 先来看简单的小例子: >>> 1+1 > ...