[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

用异或

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

要有默认返回值:返回0

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

^= 好用

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

//for loop ^=
for (int i = 0; i < nums.length; i++) {
n ^= nums[i];
}

[其他解法]:

[Follow Up]:

137. Single Number II 出现三次:与

260. Single Number III 2个一次

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int singleNumber(int[] nums) {
//ini n
int n = 0; //for loop ^=
for (int i = 0; i < nums.length; i++) {
n ^= nums[i];
} //return
if (n != 0) return n; return 0;
}
}

136. Single Number唯一的一个只出现了一次的数字的更多相关文章

  1. 136. Single Number唯一的数字

    [抄题]: Given an array of integers, every element appears twice except for one. Find that single one. ...

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

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

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

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

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

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

  5. 136. Single Number - LeetCode

    Question 136. Single Number Solution 思路:构造一个map,遍历数组记录每个数出现的次数,再遍历map,取出出现次数为1的num public int single ...

  6. leetcode 136 Single Number, 260 Single Number III

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

  7. Leetcode 136 Single Number 亦或

    题意就是从一堆数中找出唯一的一个只存在一个的数.除了那个数,其他的数都是两个相同的数. 通过亦或的性质: 1)a^a = 0 0^a = a 2)交换律 a^b = b^ a 3)结合律 (a^b)^ ...

  8. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  9. Leetcode 136 Single Number 仅出现一次的数字

    原题地址https://leetcode.com/problems/single-number/ 题目描述Given an array of integers, every element appea ...

随机推荐

  1. Gradle配置IDEA正常识别JPA Metamodel Generator动态生成的代码

    我们在使用JPA动态查询构建查询条件时,为了实现安全的类型检查,常常需要引用Hibernate JPA Metamodel Generator自动为我们生成静态元模型类. 而这些类由于编译时由Hibe ...

  2. Qt图形视图体系结构示例解析(视图、拖拽、动画)

    本博的示例来自与QT Example:C:\Qt\Qt5.9.3\Examples\Qt-5.9.3\widgets\graphicsview\dragdroprobot 将通过分析示例完成主要功能: ...

  3. 51nod 1995 三子棋

    小的时候大家一定玩过“井”字棋吧.也就是在九宫格中,只要任意行.列,或者任意连续对角线上面出现三个相同的,就能获胜.现在小明和小花也在玩三子棋,但是他们不是在九宫格里,而是在3×4的格子里面.现在小明 ...

  4. Codeforces 589F Gourmet and Banquet

    A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...

  5. SqlServer 数据表数据移动

    描述:将Test1表中的数据放到Test2表中 1.Test2表不存在 select A,B,C insert into Test2 from Test1 select * into Test2 fr ...

  6. Xcode7 修改项目名完全攻略

    1.先把整个工程文件夹名改为新的工程名. 2 .将旧项目文件夹和Tests文件名夹修改为新的名称,修改后如下图所示 3.右击 ,选择“show content package”(中文:显示包内容),看 ...

  7. 洛谷 P2212 [USACO14MAR]浇地Watering the Fields

    传送门 题解:计算欧几里得距离,Krusal加入边权大于等于c的边,统计最后树的边权和. 代码: #include<iostream> #include<cstdio> #in ...

  8. Angular2配置文件详解

    初学接触Angular2的时候,通过ng new your-project-name 命令生成一个工程空壳,本文来介绍下每个文件的作用及含义. 先来看看src的文件目录: 文件详解 File 文件 P ...

  9. BZOJ2084:[POI2010]Antisymmetry

    浅谈\(Manacher\):https://www.cnblogs.com/AKMer/p/10431603.html 题目传送门:https://lydsy.com/JudgeOnline/pro ...

  10. Linux环境变量从用户配置改为系统配置

    部署了一个新的tomcat到一个新的用户下,发下启动失败了 /home/personal/apache-tomcat/bin/catalina.sh: line 434: /usr/lib/jvm/j ...