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 ...
随机推荐
- Activiti工作流学习-----基于5.19.0版本(6)
七. BPMN的简介 读者了解到这里,应付一般的工作流开发已经足够了.此处应该有华丽的分割线,在工作流项目中核心开发人员主要是对工作流业务设计以及实现,而初级开发人员是对业务功能的代码实现.以后将主要 ...
- Android-4
显式Intent 隐式Intent Intent过滤器相关选项 通过浏览器链接启动本地Activity
- 使用typedef语句定义数组类型
使用typedef语句定义数组类型 1. 一维数组类型的定义格式 typedef <元素类型关键字><数组类型名>[<常量表达式>]; 例如: (1) ty ...
- cf B. Eight Point Sets
http://codeforces.com/contest/334/problem/B #include <cstdio> #include <cstring> #includ ...
- 如何将Icon转成Bitmap(对ICON的内部格式讲的比较清楚)
最近工作中有个需求是将Icon转成带Alpha通道的Bitmap, 虽然网上有不少这方面的文章,但很多都是错的, 这里记录下,或许对后来人有用. 要实现这个功能,我们首先需要理解Icon的格式,我 ...
- Powershell 条件操作符
Powershell 中的比较运算符-eq :等于-ne :不等于-gt :大于-ge :大于等于-lt :小于-le :小于等于-contains :包含-notcontains :不包含 进行比较 ...
- Spring Boot使用redis做数据缓存
1 添加redis支持 在pom.xml中添加 <dependency> <groupId>org.springframework.boot</groupId> & ...
- NOI2013 树的计数
题目:http://uoj.ac/problem/122 85%做法: 动态规划. 首先重编号,BFS序变成1...n,然后DFS序相应重编号. 记pos[i]为i号点在DFS中的位置,即pos[d[ ...
- SecureCRT 安装及初始化配置
安装 SecureCRT 7.3.4 安装以及破解方法 SecureCRT 6.5.0 汉化解压版 初始化配置 这里配置以SecureCRT 6.5.0 汉化解压版为例 1.调整SecureCRT终端 ...
- 无语的index hint:手工分配哈希区,5小时不出结果,优化后20分钟
同事说,有个语句5个小时不出结果,叫我帮忙看看,于是叫同事发过来.不看不知道,一看吓一跳,3个表关联,强制使用了2个index hint,当中一个表9g,一个表67g,另一个小表40Mb.开发者,总以 ...