码迷,mamicode.com
首页 > 编程语言 > 详细

leetcode Single Number python

时间:2015-06-14 06:58:03      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

 

python code:

class Solution:
# @param {integer[]} nums
# @return {integer}
def singleNumber(self, nums):
  B={}
  for i in nums:
    if i not in B:      #建立一个dict,遍历此list,将其值作为key,并简单的将其出现次数离散化为1和2作为value
      B[i]=1
    else:
      B[i]=2
  for i in B:
    if B[i] == 1:
      ret=i        #遍历dict,如果某个key对应的value为1,则返回该值
      break
  return ret

leetcode Single Number python

标签:

原文地址:http://www.cnblogs.com/bthl/p/4574519.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!