作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


[LeetCode]

题目地址:https://leetcode.com/problems/single-number/

  • Total Accepted: 183838
  • Total Submissions: 348610
  • Difficulty: Easy

题目描述

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

题目大意

数组中除了有一个数字只出现了一次之外,其余的数字都出现了偶数次。找出这个只出现了一次的叛徒。

解题方法

异或

标准的异或运算!

异或运算是可以交换顺序的运算,也就是说和元素的排列顺序无关,自己异或自己等于0,0异或别人等于别人。故,

we use bitwise XOR to solve this problem :

first , we have to know the bitwise XOR in java
1. 0 ^ N = N
2. N ^ N = 0
So… if N is the single number

N1 ^ N1 ^ N2 ^ N2 Nx ^ Nx ^ N

= (N1^N1) ^ (N2^N2) (Nx^Nx) ^ N

= 0 ^ 0 ^ …^ 0 ^ N

= N

即,只要把所有的数字异或一遍,如果出现两次的数字,进行异或之后自动消失,剩余的就是只出现一次的那个数字。

java代码如下。

public class Solution {
public int singleNumber(int[] nums) {
int returnNum=0;
for(int i=0; i<nums.length; i++){
returnNum ^=nums[i];
}
return returnNum;
}
}

AC:1ms


二刷 python。同样使用异或,对于Python2来说可以直接使用reduce函数,就所有数字进行运算的结果。

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

字典

别只记得位运算,忘记了最简单的数字统计啊!可以使用Counter直接求只出现一次的数字即可。

class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count = collections.Counter(nums)
return count.most_common()[-1][0]

日期

2017 年 1 月 7 日
2018 年 3 月 14 日 – 霍金去世
2018 年 11 月 9 日 —— 睡眠可以

【LeetCode】136. Single Number 解题报告(Java & Python)的更多相关文章

  1. LeetCode 136 Single Number 解题报告

    题目要求 Given a non-empty array of integers, every element appears twice except for one. Find that sing ...

  2. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  3. leetcode 136 Single Number, 260 Single Number III

    leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...

  4. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  5. LeetCode 136. Single Number(只出现一次的数字)

    LeetCode 136. Single Number(只出现一次的数字)

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. LeetCode 136. Single Number C++ 结题报告

    136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int ...

  8. [LeetCode] 136. Single Number 单独数

    Given a non-empty array of integers, every element appears twice except for one. Find that single on ...

  9. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

随机推荐

  1. 3D-DNA 挂载染色体

    3D-DNA是一款简单,方便的处理Hi-C软件,可将contig提升到染色体水平.其githup网址:https://github.com/theaidenlab/3d-dna 3D-DNA流程简介 ...

  2. ansible-playbook 编译安装nginx

    mkdir /etc/ansible/roles/nginx/{files,templates,tasks,handlers,vars,default,meta} -pv └── nginx ├── ...

  3. 基于 vue-cli 的 lib-flexible 适配

    基于 vue-cli3.0 的 lib-flexible 适配方案 第一步:下载安装相关依赖 第二步:创建 vue.config.js 文件并配置 第三步:在 main.js 中引入 lib-flex ...

  4. 【1】Embarrassingly Parallel(易并行计算问题)

    1.什么是Embarrassingly Parallel(易并行计算问题) 易并行计算问题:A computation that can be divided into a number of  co ...

  5. [转]sizeof计算空间大小的总结

    原文链接:http://www.cnblogs.com/houjun/p/4907622.html 关于sizeof的总结 1.sizeof的使用形式:sizeof(var_name)或者sizeof ...

  6. 转 Android Monkey压力测试使用

    转自:https://www.jianshu.com/p/c8844327f5e9 一.Monkey简介: Monkey是Android中的一个命令行工具,可以运行在模拟器里或者现实设备中,向系统发送 ...

  7. mysql_取分组后的前几行值

    --方法一: select a.id,a.SName,a.ClsNo,a.Score from Table1 a left join Table1 b on a.ClsNo=b.ClsNo and a ...

  8. 【编程思想】【设计模式】【行为模式Behavioral】Publish_Subscribe

    Python版 https://github.com/faif/python-patterns/blob/master/behavioral/publish_subscribe.py #!/usr/b ...

  9. BigDecimal中要注意的一些事

    一.关于public BigDecimal(double val) BigDecimal中三个主要的构造函数 1 public BigDecimal(double val) 将double表示形式转换 ...

  10. 二进制转换为ip地址

    #include <stdio.h> #include<math.h> int power(int b)//定义幂函数 { int i = 2, j = 1; if (b == ...