Skip to content

Commit

Permalink
single number easy
Browse files Browse the repository at this point in the history
  • Loading branch information
weiy committed Sep 12, 2018
1 parent 9fdd7ee commit e6ecd13
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Array/SingleNumber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Given a non-empty 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?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
给定一个非空数组,除了一个元素外,其余均出现两次。找出它。
需要在线性时间内,且不用额外空间。
用到了 missing number 的思路,利用异或的性质,相同的异或会抵消掉。
直接在原数组上操作,用了 i 变量,一个变量都不用要怎么写?
Discuss里也没找到相关的。
测试地址:
https://leetcode.com/problems/single-number/description/
"""

class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""

for i in range(1, len(nums)):
nums[i] = nums[i] ^ nums[i-1]

return nums[-1]

0 comments on commit e6ecd13

Please sign in to comment.