leetcode 136_SingleNumber_easy.py

github: https://github.com/CircleZ3791117

#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'circlezhou'

'''
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?
'''

'''
 Solution
'''

# Method 1: Using Hash_Table:

class Solution(object):
    def singleNumber(self, nums):
        htable = {}
        for num in nums:
            try:
                htable.pop(num)
            except:
                htable[num] = 1
        return htable.popitem()[0]

'''
Time complexity: O(n). Time complexity of for loop is O(n). Time complexity of hash table(dictionary in python) operation pop is O(1).
Space complexity: O(m). The space required by hash_table is equal to the number of elements in nums.
'''

# Method 2: Using mathematics: 2*(a+b+single)-(a+a+b+b+single) = single

class Solution(object):
    def singleNumber(self, nums):
        return sum(set(nums))*2 - sum(nums)

'''
Time complexity: O(n+n) = O(n). sum will call next to iterate through nums. We can see it as sum(list(i, for i in nums)) which means the time complexity is O(n) because of the number of elements(n) in nums.
Space complexity: O(m+m) = O(m). set needs space for the elements in nums
'''

# Method 3: Using XOR: a ^ 0 = a , a ^ a = 0, (a^b)^a = (a^a)^b 

class Solution(object):
    def singleNumber(self, nums):
        sum = 0
        for num in nums:
            sum ^= numß
        return sum

'''
Time complexity: O(n)
Space complexity: O(1)
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值