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 ...
随机推荐
- Shortest Word Distance 解答
Question Given a list of words and two words word1 and word2, return the shortest distance between t ...
- android中的本地定时推送到通知栏
一.使用系统定义的Notification 以下是使用示例代码: import android.app.Notification; import android.app.NotificationMan ...
- java生成字符串md5函数类(javaSE)
//实现生成MD5值 import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.B ...
- ECMAScript 6新特性介绍
箭头函数 箭头函数使用=>语法来简化函数.在语句结构上和C#.Java 8 和 CoffeeScript相似,支持表达式和函数体. . =>`操作符左边为输入的參数.而右边则是进行的操作以 ...
- Flashback version/Transaction Query,FlashbackTable
Flashback version Query相对于Flashback Query 只能看到某一点的对象状态, Oracle 10g引入的Flashback Version Query可以看到过去某个 ...
- Global.asax使用2
ASP.NET中利用Application和Session统计在线人数.历史访问量 先来简单说一下ASP.NET中的Application和Session 下图是我们非常熟悉的Web应用程序的结构: ...
- iOS之本地推送(前台模式与后台模式)
#import "AppDelegate.h" #import "GlobalDefine.h" @interface AppDelegate () @end ...
- Java IO6 :IO总结
字节流.字符流继承关系 前几篇文章讲解了字节流.字符流的使用,不过Java提供给用户的流类远不止此,限于篇幅原因,没办法一一讲解,而且也没有必要一一讲解,就像我在写博客的时候多次提到的,有问题的时候学 ...
- C++学习笔录4
1.容器=数据结构+算法.相当于是为复杂的数据设计一种专门用于存放该数据的东西.用于开发中传递复杂的数据. 2.模版函数只能写在头文件中.不能单独做声明. 3.STL容器类分为三类: (1).顺序容器 ...
- OpenSSl编译
1.下载openssl代码,下载地址:http://www.openssl.org/source/ ,如果使用winrar解压失败的话(提示不能创建符号链接),可以关闭UAC.2.下载安装Active ...