LeetCode #Single Number#

LeetCode #Single Number#_第1张图片

解法一:

    人类需要O(n)去解决问题,于是普罗米修斯不管三七二十一就偷来了Hash...
    Python里面内置的dic好用到不行.这里可以利用Hash把时间复杂度降到O(n),但是这种解法不满足对内存的要求...
""" Programmer : EOF Code date : 2015.03.02 file : sn.py e-mail : [email protected] Code description :        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? In this solution, I used a hash/dictionary table. """
class Solution():

    def singleNumber(self, A) :
        dic = {}
        for i in range(0, len(A)) :
            dic[A[i]] = 0

        for i in range(0, len(A)) :
            if A[i] in dic :
                dic[A[i]] += 1

        for i in range(0, len(A)) :
            if dic[A[i]] == 1 :
                return A[i]

#----------for testing ----------------

array = [1,2,3,4,5,4,3,2,1]

s = Solution()
print s.singleNumber(array)

解法二:

首先最简单的思路不过就是排序,一旦排序起来,只要有数值的,就最好不要用比较排序,基于比较排序最快也就O(n * log(n) ),线性时间排序O(n)不
能再sexy... 但是这种方法不满足对内存的要求(Don't use extra memory).
""" Programmer : EOF Code date : 2015.03.02 file : sn.py e-mail : [email protected] Code description :        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? In this solution, I sort the inputed @array by @counting sort which is a very fast algorithm which's runtime complexity is O(n). BUT I have to use extra memory to solve this problem by this method. """
class Solution():

    def counting_sort(self, array) :
        array_size = len(array)

        max_value = 0

        for i in range(0, array_size) :
            if max_value < array[i] :
                max_value = array[i]

        buf_size = max_value+1

        buf = [0] * buf_size
        ret = [0] * array_size

        for i in range(0, array_size) :
            buf[array[i]] += 1

        for i in range(1, buf_size) :
            buf[i] += buf[i-1]

        for i in range(array_size-1, -1, -1) :
            ret[buf[array[i]] - 1] = array[i]
            buf[array[i]] -= 1

        return ret

    def singleNumber(self, A) :

        A = self.counting_sort(A)
        length = len(A)
        for i in range(0, length, 2) :
            if i < length-2 and A[i] != A[i+1] :
                return A[i]
            elif i == length-1 :
                return A[i]

#----------for testing ----------------

array = [1,2,3,4,5,4,3,2,1]

s = Solution()
print s.singleNumber(array)

解法三:

这种解法是我看到 @Powerxing 的blog时候看到的.利用...异或

节操在这里:http://www.powerxing.com/leetcode-single-number/

""" Programmer : EOF Code date : 2015.03.02 file : sn3.py e-mail : [email protected] Code description : 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? In this solution, I used a XOR operand to solve this problem. """
class Solution():

    def singleNumber(self, A) :

        result = 0
        for i in range(0, len(A)) :
            result ^= A[i]

        return result


#----------for testing ----------------

array = [1,2,3,4,5,4,3,2,1]

s = Solution()
print s.singleNumber(array)




LeetCode #Single Number#_第2张图片

你可能感兴趣的:(LeetCode #Single Number#)