Single Number | LeetCode OJ 解题报告

题目网址:https://oj.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?

找一个出现次数仅为一次的数,O(n)时间



题目解答:

初步的想法就是把array排序,然后从前往后配对,遇见不一样的就是第一个数是出现一次的,考虑边界条件(array长度是1或者最后一个数是要找的数)

    def singleNumber(self, A):
        A.sort()
        if len(A) == 1:
            return A[0]
        for i in A:
            if i%2 == 1:
                if A[i] != A[i-1]:
                    return A[i-1]
                else:
                    pass
                    print "hey bitch:" + "A[" + str(i) + "] = " + str(A[i]) + "\n"
            elif i == len(A):
                return A[len(A)]

很可惜这玩意超时了。然后我就想了,list是个超级低效的数据结构,何不用dict,于是:

        #A.sort()
        D = {}
        for i in A:
            if i not in D:
                D[i] = 1
            else:
                D[i] = D[i]+1
        for i in D:
            if D[i] == 1:
                return i

空间、时间复杂度O(n),其实sort不sort差不多,但是sort的时间复杂度为O(nlogn)。但是,题目里说了,怎么才能少用或者不用memory?于是:

        A.sort()
        while len(A) != 1:
            a = A.pop()
            b = A.pop()
            if a == b:
                pass
            else:
                return a
        return A[0]

用了O(1)空间,但是时间又涨到O(nlogn),绞尽脑汁,想不出来啊。于是翻了翻评论,原来还有位运算这回事儿:

        if len(A) == 1:
            return A[0]
        else:
            for a in A[1:]:
                A[0] = a^A[0]
            return A[0]

这才是真正满足题目要求的解,虽然上面的代码都过了吧

时间: 2024-11-03 22:33:20

Single Number | LeetCode OJ 解题报告的相关文章

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

Add Two Numbers | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/add-two-numbers/ 题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i

Two Sum | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/two-sum/ 题目描述: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, whe

LeetCode: Permutations 解题报告

Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. SOLUTION 1: 经典的递归回溯题目,一次ACCEPT. 请也参考上一个题目LeetCode: Combination

[LeetCode]3Sum,解题报告

题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The so

[LeetCode]Candy, 解题报告

前言 回学校写论文差不多快二周的时间了,总结一句话,在学校真舒服.花了7天时间搞定了毕业论文初稿(之所以这么快肯定跟我这三年的工作量相关了),不过今天导师批复第二章还是需要修改.此外,最近迷上打羽毛球,确实很爽,运动量仅此于篮球,可以场地有限,哎,且打且珍惜吧. 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these chi

Single Number leetcode java

问题描述: 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? 问题分析:要求采用线性时间复杂度,并且最好不使用多余的空间. 一.位操作 异

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

Leetcode 65. Valid Number 验证数字 解题报告

1 解题思想 更新下:这道突然就很多访问,想起来好像是lt提交通过率最低的,嗯,我写的也不是特别详细,如有问题可以新浪微博@MebiuW交流~~ 这道题条条框框是在太多了,各种情况..不过简略来说,正确的做法应该是: 1.熟悉数字的表述规则(可以看网上的,也可以看我代码的),这道题关键是要懂所有的数字规则. 2.对输入的数字首先进行必要的检测,是否有abc或者中间空格等非法字符 3.将e前面和e后面分开计算!e前面允许有小数点形式的,e后面不允许有小数点形式的 4.数字的形式一般是 可以有正负号