136. Single Number [medium] (Python)

题目链接

https://leetcode.com/problems/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?

思路方法

目的是寻找数组中单独出现的某个元素,如果题目没有限制条件很容易想到利用dict来做。然而既然要求O(n)的时间复杂度和O(1)的空间复杂度,那么就要想办法将成对出现的数“消掉”,目前能想到的就是利用“异或”操作实现。

思路一

将所有的数进行异或操作,由于题目说了只有一个单独出现的数,那么最终结果即为所求。

代码

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        res = 0
        for i in nums:
            res ^= i
        return res

思路二

精简代码。。。

代码

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return reduce(lambda x, y: x^y, nums)

思路三

再精简代码。。。

代码

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return reduce(operator.xor, nums)

PS: 写错了或者写的不清楚请帮忙指出,谢谢!

转载请注明:http://blog.csdn.net/coder_orz/article/details/52064716

时间: 2024-08-07 00:17:30

136. Single Number [medium] (Python)的相关文章

【59】136. Single Number

136. Single Number Description Submission Solutions Add to List Total Accepted: 191020 Total Submissions: 360448 Difficulty: Easy Contributors: Admin Given an array of integers, every element appears twice except for one. Find that single one. Note:Y

136. Single Number && 137. Single Number II && 260. Single Number III

136. 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? Subscribe to see which co

LeetCode 136. Single Number & 268. Missing Number

136. Single Number 考察的是异或运算.相同的数异或结果为0,一个数与0异或还是原来的数,以及异或符合交换律.因此,把所有的数都异或起来,结果就是落单的那个数. class Solution { public: int singleNumber(vector<int>& nums) { int res=0; for (int num:nums){ res ^= num; } return res; } }; 268. Missing Number 可以用数学方法直接做,

136. Single Number - LeetCode

Question 136.?Single Number Solution 思路:构造一个map,遍历数组记录每个数出现的次数,再遍历map,取出出现次数为1的num public int singleNumber(int[] nums) { Map<Integer, Integer> countMap = new HashMap<>(); for (int i=0; i<nums.length; i++) { Integer count = countMap.get(nums

136. Single Number C++ 答案

136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int singleNumber(vector<int>& nums) { int s = 0; for(int i = 0; i < nums.size(); i++) { s = s ^ nums[i]; } return s; } }; 参考 LeetCode Problems' So

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

136.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? 思路: 一开始自己的思路非常简单,首先设置一个status的变量,然后通过2层for循环

136. Single Number leetcode做题报告

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 singleNumber(vec

LeetCode 136 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 i