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 ...
随机推荐
- iOS - Xcode 插件
Xcode 插件 Xcode 插件安装目录: ~/library/Application Support/Developer/Shared/Xcode/Plug-ins Xcode 插件大全 http ...
- 【CSU1808】地铁
ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci 号线,位于站 ai,bi 之间,往返均需要花费 ti 分钟(即从 ...
- Android 自定义波浪动画 --"让进度浪起来~"
原文链接:http://www.jianshu.com/p/0e25a10cb9f5 一款效果不错的动画,实现也挺简单的,推荐阅读学习~ -- 由 傻小孩b 分享 waveview <Andro ...
- struts2学习:配置篇之namespace
把namespace单独拉出来讲一方面是因为它实际上不是一个element,而只是一个attribute,前面已经说了,它是package的一个attribute:另外一方面是因为这个属性是我接触St ...
- shell编程—— EOF
在shell编程中,”EOF“通常与”<<“结合使 用,“<<EOF“表示后续的输入作为子命令或子shell的输入,直到遇到”EOF“,再次返回到主调shell,可将其理解为分 ...
- js根据当前时间获取当前季度,月份,和第几周
function jidu() { var getMonthWeek = function (a, b, c) { var date = new Date(a, parseInt(b) - ...
- MySQL浮点计算存在的问题与解决方案
如有疑问请联系微信:onesoft007 在计算机中,浮点数往往很难精确表示,那么浮点数运算结果也往往难以精确表示.MySQL同样也存在这个问题,并表现在如下几个方面. 问题 1.相同的输入,可 ...
- Cocos Code IDE新建lua工程报错解决方案
今天想用cocos code IDE新建一个工程,但是控制台报错:Read json file null failed, the reason is:null.我下载的是官方3.5源码,sdk,ndk ...
- sql注入基于错误-单引号-字符型
查找注入点 在url中: 1. ' 2. and 1=1/and 1=2 3. 随即输入(整形) 4. -1/+1回显上下页面(整形) 5. and sleep(5) (判断页面返回时间) 判断有 ...
- cf
Financing a capital project with equity may be a signal to investors that a company's prospects are ...