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<[email protected], [email protected]>
#
# 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)

时间: 2024-10-10 07:27:03

leetCode —— Single Number的相关文章

leetCode: Single Number II [137]

[题目] Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? [题意] 给定一个整数以外,其中除了一个整数只出现一次以外,其他

LeetCode: Single Number Ⅱ

1 /** 2 * 3 */ 4 package solution; 5 6 import java.util.Arrays; 7 8 /** 9 * @author whh 10 * 11 * Given an array of integers, every element appears three times except 12 * for one. Find that single one. 13 * 14 * Note: Your algorithm should have a li

LeetCode: Single Number [136]

[题目] 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? [题意] 给定一个整数数组,其中除了一个数以外,其他数都是成对出现的,找出这

LeetCode——Single Number(II)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Single Number II Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm shou

leetcode Single Number III

题目连接 https://leetcode.com/problems/single-number-iii/ Single Number III Description Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only

LeetCode Single Number I / II / III

[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个相同的数异或为0,那么把这串数从头到尾异或起来,最后的数就是要求的那个数. 代码如下: class Solution { public: int singleNumber(vector<int>& nums) { int sum = 0; for(int i=0;i<nums.siz

LeetCode Single Number I II Python

Single Number Given an array of integers, every element appears twice except for one. Find that single one. def singleNumber(self, A): l = len(A) if l < 2: return A[0] A.sort() for i in range(0,l-1,2): if A[i] != A[i+1]: return A[i] return A[l-1] 思路:

leetcode Single Number II python

Single Number II Given an array of integers, every element appears three times except for one. Find that single one. python code: class Solution: # @param {integer[]} nums # @return {integer} def singleNumber(self, nums): B={} for eachint in nums: if

leetcode Single Number python

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: #建

LeetCode: Single Number I &amp;&amp; II

I title: 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? 思路:异或 class Solution { public: int