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. ZooKeeper架构设计及其应用

    ZooKeeper是一个开源的分布式服务框架,它是Apache Hadoop项目的一个子项目,主要用来解决分布式应用场景中存在的一些问题,如:统一命名服务.状态同步服务.集群管理.分布式应用配置管理等 ...

  2. pyQt事件处理

    Qt事件处理01 Qt处理事件的第二种方式:"重新实现QObject::event()函数",通过重新实现event()函数,可以在事件到达特定的事件处理器之前截获并处理他们.这种 ...

  3. 基础总结篇之五:BroadcastReceiver应用详解

    問渠那得清如許?為有源頭活水來.南宋.朱熹<觀書有感> 据说程序员是最爱学习的群体,IT男都知道,这个行业日新月异,必须不断地学习新知识,不断地为自己注入新鲜的血液,才能使自己跟上技术的步 ...

  4. python学习之路-5 基础进阶篇

    本篇涉及内容 双层装饰器字符串格式化 双层装饰器 装饰器基础请点我 有时候一个功能需要有2次认证的时候就需要用到双层装饰器了,下面我们来通过一个案例详细介绍一下双层装饰器: 执行顺序:自上而下 解释顺 ...

  5. Deep Compression Compressing Deep Neural Networks With Pruning, Trained QuantizationAnd Huffman Coding

    转载请注明出处: http://www.cnblogs.com/sysuzyq/p/6200613.html by 少侠阿朱

  6. springMVC3学习(六)--SimpleFormController

    SimpleFormController提交表单流程例如以下: login.jsp <form action="login" method="post"& ...

  7. 一、cocos2dx概念简介

    cocos2dx概念介绍 1)scene,继承自CCScene 场景,一个游戏运行期间的显示界面,一个应用里面可以有多个场景,但是每次只能有一个是激活状态,也可以理解为一次只能显示一个界面. 例如,你 ...

  8. 不用position,让div垂直居中

    先弄懂after伪类的用法,就可以很容易理解了. <!DOCTYPE html> <html lang="en"><head> <meta ...

  9. 前端--关于客户端javascript

    浏览器中的Javascript 客户端javascript就是运行在浏览器中的javascript,现代的浏览器已经有了很好的发展,虽然它是一个应用程序,但完全可以把它看作是一个简易的操作系统,因为像 ...

  10. QSS 样式表 (一)

    在开发应用程序时,往往对界面的美观有一定的要求.Qt 引入了 QSS 机制,使得界面的美化工作变的轻轻松松.嗯,QSS听着有点耳熟.是的,QSS的语法和CSS类似.在此做些总结. 先来看一个简单的例子 ...