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 - Project 项目
1.项目流程 1.1 分析项目的架构 iOS 常见的几种架构 标签式 Tab Menu 列表式 List Menu 抽屉式 Drawer 瀑布式 Waterfall 跳板式 Springborad 陈 ...
- mysql中Timestamp,time,datetime 区别
一.TIMESTAMP[(M)] 时间戳.范围是’1970-01-01 00:00:00’到2037年. TIMESTAMP列用于INSERT或UPDATE操作时记录日期和时间. 如果你不分配一个值, ...
- redis 数据导出
一.导出所有的keys echo "keys 201*" |./redis-cli -h localhost -p 6379 -a password >> 1.txt ...
- 初学者用div+css结构写网页的几个误区
1.用div+css结构制作静态html网页不等于彻底抛弃古老的table写法.之所以不建议用table来布局网页是因为在网页加载很慢的时候要等table结构加载完成才能看到网页,其次是table的布 ...
- Sealed密封类
using System; using System.Collections.Generic; using System.Linq; using System.Text; #region 概述 //在 ...
- MemCache的LRU删除机制详解
本节主要讲解一下MC的LRU的删除机制和一些参数的限制 MC的过期数据惰性删除 1.当某个值过期后,并没有从内存中删除,因此,我们在进行st ats统计信息的时候,curr_items有其信息(它的数 ...
- eclipse中配置tomcat后,运行jsp时出现Server Tomcat v7.0 Server at localhost failed to start.
最近在进行jsp开发学习,在配置上还是遇到很多问题. 在连接好数据库后,写了第一个jsp测试页面,结果在运行eclipse中运行toamcat时出现了错误提示:Server Tomcat v7.0 S ...
- src与href
href是指向网络资源所在位置,建立和当前元素(锚点)或当前文档(链接)之间的链接,用于超链接. src是指向外部资源的位置,指向的内容将会嵌入到文档中当前标签所在位置:在请求src资源时会将其指向的 ...
- flash透明效果代码分享~~~
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...
- [转]CSS,font-family,常用网页字体
http://www.zreading.cn/ican/2014/10/css-font-family/ CSS,font-family,好看常用的中文字体 2014-10-14 例1(小米米官网): ...