Single Number 解答
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 解答的更多相关文章
- LeetCode 136. Single Number C++ 结题报告
136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int ...
- 异或巧用:Single Number
异或巧用:Single Number 今天刷leetcode,碰到了到题Single Number.认为解答非常巧妙,故记之... 题目: Given an array of integers, ev ...
- Single Number 普通解及最小空间解(理解异或)
原题目 Given a non-empty array of integers, every element appears twice except for one. Find that singl ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- [LintCode] Single Number 单独的数字
Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Have you met this question in ...
随机推荐
- ASP.NET应用程序和ASP.NET网站所共有的文件: App_Browsers 等
App_Browsers 包含 ASP.NET 用于标识个别浏览器并确定其功能的浏览器定义 (.browser) 文件.有关更多信息,请参见浏览器定义文件架构(browsers 元素)和如何:在 A ...
- POJ 2182 Lost Cows (线段树)
题目大意: 有 n 头牛,编号为 1 - n 乱序排成一列,现已知每头牛前面有多少头牛比它的编号小,从前往后输出每头牛的编号. 思路: 从后往前推,假如排在最后的一头牛比他编号小的数量为a,那么它的编 ...
- obiz
ofbiz 安装 1. 由 binary 安装: 由 binary 安装非常简单, 以下是安装方法: 下载ofbiz-2.0-beta1-complete.tar.gz, 注意不是ofbiz-2.0- ...
- HTTPS证书生成原理和部署细节
看看下面,部分电信用户访问京东首页的时候,会看到右下角有一个浮动广告: 小白用户以为是京东有意放置的,细心的用户会发现,这个 iframe 一层嵌一层的恶心广告很明显是电信/中间人通过 DNS 劫持注 ...
- Hive 8、Hive2 beeline 和 Hive jdbc
1.Hive2 beeline Beeline 要与HiveServer2配合使用,支持嵌入模式和远程模式 启动beeline 打开两个Shell窗口,一个启动Hive2 一个beeline连接hi ...
- 常用的Eclipse快捷键
alt+shift+r 修改名字 ctrl+shift+r 查找源类 Eclipse快捷键功能1. [ALT+/] --->提示此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不 ...
- 【转】ffserver用法小结
我们可以通过ffserver以及ffmpeg做一个简单的视频监控系统,ffserver用于视频的转发调度,ffmpeg用于转码 而对于ffserver最基本也是最重要的就是对它的ffserver.co ...
- java匿名内部类,多态,接口练习
1多态以及接口调用方法: public class Interface { public static void main(String[] args) { Al x = new Al(); jian ...
- 前端JS模版库kino.razor - 原理流程分析 - 改进版轮子RazorJs
1.前言 从后台获取数据,在前端JS里面拼接字符串,不累吗?敢不敢找一款前端使使... 现在这种模板库比较多了,我用过的jquery-template .JsRender .听说过的一堆,还有各种MV ...
- jQuery 事件 - error() 方法
实例 如果图像不存在,则用一段预定义的文本取代它: $("img").error(function(){ $("img").replaceWith(" ...