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 ...
随机推荐
- 基类方法的反隐藏 反private 秘籍
class GoodStudent:private Mentor,private Student { public : using Mentor::GetInfo; ///------------ ...
- IIC 概述之24c系列存储器内存分析
IIC 型号 容量 器件/业面寻址字节 可寻址位 模块 24C01 128B (1010)(A2)(A1)(A0)(0或 ...
- AngularJS自定义表单验证器
<!doctype html> <html ng-app="myApp"> <head> <script src="G:\\So ...
- pod应用--备用
一.概要 iOS开发时,项目中会引用许多第三方库,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用来方便的统一管理这些第三方库. 二.安装 由于 ...
- Eclipse快捷键集结
Debug快捷键 F5单步调试进入函数内部. F6单步调试不进入函数内部,如果装了金山词霸2006则要把“取词开关”的快捷键改成其他的. F7由函数内部返回到调用处. F8一直执行到下一个 ...
- gitosis使用笔记
gitosis是Git下的权限管理工具,通过一个特殊的仓库(gitosis-admin.git)对Git权限进行管理. 1:服务端安装并配置gitosis (1)通过以下方式获取到安装包 root@w ...
- CCI_chapter 19 Moderate
19 1 Write a function to swap a number in place without temporary variables void swap(int &a, i ...
- 给Qt生成的exe执行程序添加版本信息
Windows下的.exe可执行文件的属性中有版本这个信息,含有版本信息.描述.版权等.对于qt程序,要含有这样的信息,那就请如下操作:新建<工程名>.rc文件,在rc文件填入下的信息: ...
- 【Xamarin 开发 IOS --IOS ViewController生命周期】
ViewController ViewController是IOS开发中MVC模式中的C,ViewController是view的controller,ViewController的职责主要包括管理内 ...
- 【VC编程技巧】文件☞2.3CArchive的用法
CArchive 对象提供了一个类型安全缓冲机制CArchive 对象提供了一个类型安全缓冲机制.用于将可序列化对象写入CFile 对象或从中读取可序列化对象.通常,CFile 对象表示磁盘文件:但是 ...