Question

Given an array of integers, every element appears twice except for one. Find that single one.

Solution 1 -- Set

We can use a hash set to record each integer's appearing time. Time complexity O(n), space cost O(n)

 public class Solution {
public int singleNumber(int[] nums) {
Set<Integer> counts = new HashSet<Integer>();
int length = nums.length, result = nums[0];
for (int i = 0; i < length; i++) {
int tmp = nums[i];
if (counts.contains(tmp))
counts.remove(tmp);
else
counts.add(tmp);
}
for (int tmp : counts)
result = tmp;
return result;
}
}

Solution 2 -- Bit Manipulation

The key to solve this problem is bit manipulation. XOR will return 1 only on two different bits. So if two numbers are the same, XOR will return 0. Finally only one number left.

 public int singleNumber(int[] A) {
int x = 0;
for (int a : A) {
x = x ^ a;
}
return x;
}

Single Number 解答的更多相关文章

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

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

  2. 异或巧用:Single Number

    异或巧用:Single Number 今天刷leetcode,碰到了到题Single Number.认为解答非常巧妙,故记之... 题目: Given an array of integers, ev ...

  3. Single Number 普通解及最小空间解(理解异或)

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

  4. [LeetCode] Single Number III 单独的数字之三

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  5. [LeetCode] Single Number II 单独的数字之二

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  6. [LeetCode] Single Number 单独的数字

    Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...

  7. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  8. LeetCode——Single Number II(找出数组中只出现一次的数2)

    问题: Given an array of integers, every element appears three times except for one. Find that single o ...

  9. [LintCode] Single Number 单独的数字

    Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Have you met this question in ...

随机推荐

  1. POJ Oulipo (KMP)

    题目大意 : 在一个字符串中找出目标单词的个数 代码: #include<iostream> #include<cstdio> #include<cstdlib> ...

  2. 计算机程序的思维逻辑 (63) - 实用序列化: JSON/XML/MessagePack

    上节,我们介绍了Java中的标准序列化机制,我们提到,它有一些重要的限制,最重要的是不能跨语言,实践中经常使用一些替代方案,比如XML/JSON/MessagePack. Java SDK中对这些格式 ...

  3. pyqt一个简单的动画

    import sys from PyQt4.QtGui import QApplication , QGraphicsEllipseItem , QGraphicsItemAnimationfrom ...

  4. web前端之 HTML标签详细介绍

    html标签的分类 点我查看完整的html标签介绍 在html中,标签一般分为块级标签和行内标签 块级标签:块元素一般都从新行开始,它可以容纳内联元素和其他块元素,常见块元素是段落标签"p& ...

  5. 分割视图控制器(UISplitViewController) 改_masterColumnWidth 导致在 IOS 10中出现闪退

    默认UISplitViewController的Master和Detail的宽度是固定的,可以通过下面的方式来改变 [splitViewController setValue:[NSNumber nu ...

  6. Android开发 ADB server didn't ACK, failed to start daemon解决方案

    有时候在打开ddms的时候,会看到adb会报如题的错误,解决方案是打开任务管理器,(ctrl+shift+esc),然后关掉adb.exe的进程,重启eclipse就ok了. 还有许多无良商家开发的垃 ...

  7. 用phantomjs 进行网页整页截屏

    写截取整个网页程序是一个做前台的哥们所托,要做一些漂亮的界面原形,参考一些不错的网站设计就帮他弄了个截屏的程序. phantomjs   是一个基于js的webkit内核无头浏览器 也就是没有显示界面 ...

  8. Linux进程控制——exec函数族

    原文:http://www.cnblogs.com/hnrainll/archive/2011/07/23/2114854.html 1.简介 在Linux中,并不存在exec()函数,exec指的是 ...

  9. 使用indent命令帮助排版源代码

    在写代码时候,特别是在vim中编辑代码时候,你可能会不太注意代码风格问题,比如‘{’符号放在行末还是下一行行首等等, 这样你把自己代码与别人的代码merge时候,就会出现代码风格不一的问题,这里就推荐 ...

  10. Tomcat 服务器的端口号的修改

    在系统中找到Tomcat安装目录下的conf文件夹下的servlet.xml文件. (1)在servlet.xml文件中找到以下代码: <connector port="8080&qu ...