Find one unique integer
On-Site Question 2 - SOLUTION
Problem
Given a list of account ID numbers (integers) which contains duplicates , find the one unique integer. (the list is guaranteed to only have one unique (non-duplicated) integer
Requirements¶
Do not use built-in Python functions or methods
Solution
This should feel very familiar to one of the problems we did in the array section of the course! We can use an XOR operation. The exclusive or operations will take two sets of bits and for each pair it will return a 1 value if one but not both of the bits is 1.
In Python we can use the ^ symbol to perform an XOR.
Now for our solution we can simply XOR all the integers in the list. We start with a unique id set to 0, then every time we XOR a new id from the list, it will change the bits. When we XOR with the same ID again, it will cancel out the earlier change.
By the end, we wil be left with the ID that was unique and only appeared once!
def solution(id_list):
# Initiate unique Id
unique_id = 0
# XOR fo revery id in id list
for i in id_list:
# XOR operation
unique_id ^= i
return unique_id
Good Job!
Find one unique integer的更多相关文章
- java的toString()及包装类的实现--Integer重点学习
1. toString()来源 2. toString()目的 3. toString()实现(JDK8) 1. toString()来源 源于java.lang.Object类,源码如下: /** ...
- neo4j-jersey分嵌入式和服务式连接图形数据库
原文载自:http://blog.csdn.net/yidian815/article/details/12887259 嵌入式: 引入neo4j依赖 <dependency> <g ...
- codeforces754D Fedor and coupons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Kafka设计解析(一)- Kafka背景及架构介绍
本文转发自Jason’s Blog,原文链接 http://www.jasongj.com/2015/01/02/Kafka深度解析 背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅 ...
- POJ1860 Currency Exchange(bellman-ford)
链接:http://poj.org/problem?id=1860 Currency Exchange Description Several currency exchange points are ...
- CF731C. Socks[DFS 贪心]
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- POJ1112 Team Them Up![二分图染色 补图 01背包]
Team Them Up! Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7608 Accepted: 2041 S ...
- Kafka深度解析
本文转发自Jason’s Blog,原文链接 http://www.jasongj.com/2015/01/02/Kafka深度解析 背景介绍 Kafka简介 Kafka是一种分布式的,基于发布/订阅 ...
- codeforces 754D. Fedor and coupons
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- spring boot 整合案例
github : https://github.com/nbfujx/springBoot-learn-demo
- Pycharm远程连接服务器(windows下远程修改服务器代码)
1.写在前面 之前一致用putty,ssh,修改代码,或者本地修改,上传到服务器,各种不爽,现在改用xshell,但是有时候还是不方便感觉,于是自己配置了远程连接pycharm,这样不用总是到 ...
- plsql 执行批量文件
plsql 执行批量文件 plsql>command window @c:\a.sql;@c:\b.sql;@c:\c.sql;
- AJAX服务器返回数据 连接数据库查询数据
getcustomer.asp" 中的源代码负责对数据库进行查询,然后用 HTML 表格返回结果: <% response.expires=-1 sql="SELECT * ...
- 文件的基本操作(python)
文件打开: 1. f = open('yesterday,‘r+’,encoding = ‘utf-8’) 读取的方式加载为Utf-8 r 打开文件并写, 只适用于文字类 r+ 打开文件并读 ...
- nginx 限制ip
转自:https://www.cmsky.com/nginx-deny-ip/ 面对垃圾留言和暴力破解,我们可以封禁IP,前文介绍过Apache环境使用.htacess来屏蔽IP,Nginx也可以做到 ...
- as3 去掉字符串空白问题
去掉内容的所有空白 function trim(str:String):String { })/g,""); } //[ ]内是一个中文空格一个英文空格 {}是说匹配一个到多个 / ...
- Python中的操作符重载
运算符重载是指在方法中拦截内置的操作----当类的实例出现在内置操作中,Python会自动调用自定义的办法,并且返回自定义方法的操作结果. 类可以重载python的操作符 操作符重载使我们的对 ...
- js中怎么写自执行函数
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 数学分析中jensen不等式由浅入深进行教学(转)
中国知网:数学分析中Jensen不等式由浅入深进行教学