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?

 [暴力解法]:

时间分析:n

空间分析:新建n的数组来存储

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

[一句话思路]:

 ^= 或与位运算,负负得正

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

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(1) 没有额外开辟空间

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

[关键模板化代码]:

[其他解法]:

[Follow Up]:

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

137. Single Number II 出现3次

 [代码风格] :

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

 

posted @ 2018-03-13 21:38  苗妙苗  阅读(130)  评论(0编辑  收藏  举报