LEETCODE —— Single Number
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: ScottGu<gu.kai.66@gmail.com, 150316990@qq.com>
#
# Created: 13/11/2014
# Copyright: (c) ScottGu<gu.kai.66> 2014
# Licence: <your licence>
#-------------------------------------------------------------------------------
class Solution:
# @param A, a list of integer
# @return an integer
def singleNumber(self, A):
self.__init__()
for num in A:
if(self.dict.has_key(num)):
self.dict[num]+=1
else:
self.dict[num]=1
for p in self.dict.items():
if(p[1]==1):
return p[0]
def __init__(self):
self.dict={}
def main():
so=Solution()
arr=[1,1,2,2,3,3,4,4,5,6,6]
print arr
print so.singleNumber(arr)
arr=[1,2,2,3,3,4,4,5,6,6]
print arr
print so.singleNumber(arr)
arr=[1,1,2,3,3,4,4,5,6,6]
print arr
print so.singleNumber(arr)
arr=[1,1,2,2,3,3,4,4,5]
print arr
print so.singleNumber(arr)
arr=[1,0,1]
print arr
print so.singleNumber(arr)
arr=[1,0,0]
print arr
print so.singleNumber(arr)
LEETCODE —— Single Number的更多相关文章
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- LeetCode:Single Number II
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
- LeetCode Single Number III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
- [leetcode]Single Number II @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- LeetCode: Single Number I && II
I title: Given an array of integers, every element appears twice except for one. Find that single on ...
- [LeetCode] Single Number III ( a New Questions Added today)
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
随机推荐
- spring+mybatis
---恢复内容开始--- 使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地 ...
- oracle连接本地数据库
连接方式: 通过SQL Developer进行连接: 通过sql plus 进行连接: SQL Developer进行连接1.安装Oracle 11g会自带一个叫做SQL Developer的工具,它 ...
- Centos7下搭建LAMP平台环境 (转载)
1.启用Apache(httpd) Centos7默认已经安装httpd服务,只是没有启动.如果你需要全新安装,可以yum install -y httpd 启动服务:systemctl start ...
- laravel Ajax post方式的使用
以jquery ajax 的post的方式为例 验证邮箱输入格式是否正确 html <div class="fl"> <input type="emai ...
- mysql 中关于周和月份的表示
本周:YEARWEEK(date_format(create_time,'%Y-%m-%d')) = YEARWEEK(now()) 上周:YEARWEEK(date_format(create_ti ...
- [转]Java 8:不要再用循环了
以下内容为转载,没有在jdk8中测试,具体业务场景是否存在BUG或使用需要注意的地方有待测试. ------------------分割线---------------------- 正如我之前所写的 ...
- App压力测试整理
压力测试结果:CRASH:崩溃,应用程序在使用过程中,非正常退出ANR:Application Not Responding MonkeyRunner APIs MonkeyRunner:用来连接设备 ...
- Positive-definite matrix
In linear algebra, a symmetric n × n real matrix M is said to be positive definite if zTMz is positi ...
- android通知-Notification
android中,当app需要向发送一些通知,让使用者注意到你想要告知的信息时,可以用Notification.下面,就来讨论一下,Notification的用法,我们从实际的小例子来进行学习. 1. ...
- BeanUtil体会
把字符串(非纯数字组成的字符串,带有字符的那种)拷贝到int属性中,int属性值设为0 把字符串(纯数字组成的),赋值给double类型,可以直接转换,int类型也可以直接转换成double类型 但是 ...