[Algorithm] 136. Single Number
Given a non-empty 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?
Example 1:
Input: [2,2,1]
Output: 1Example 2:
Input: [4,1,2,1,2]
Output: 4
var singleNumber = function(nums) {
let res = ;
for (let i = ; i < nums.length; i++) {
res = res ^ nums[i]
}
return res;
};
It uses XOR: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_XOR
For example, 4 ^ 2
4: 0100
2: 0010
XOR
0110 => 6
But
2: 0010
2: 0010
XOR
0000 => 0
[Algorithm] 136. Single Number的更多相关文章
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- LeetCode 136. Single Number C++ 结题报告
136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int ...
- LeetCode 136. Single Number(只出现一次的数字)
LeetCode 136. Single Number(只出现一次的数字)
- 136. Single Number - LeetCode
Question 136. Single Number Solution 思路:构造一个map,遍历数组记录每个数出现的次数,再遍历map,取出出现次数为1的num public int single ...
- 【LeetCode】136. Single Number (4 solutions)
Single Number Given an array of integers, every element appears twice except for one. Find that sing ...
- 136. Single Number唯一的数字
[抄题]: Given an array of integers, every element appears twice except for one. Find that single one. ...
- 136. Single Number唯一的一个只出现了一次的数字
[抄题]: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...
- LeetCode Problem 136:Single Number
描述:Given an array of integers, every element appears twice except for one. Find that single one. Not ...
随机推荐
- [Atcoder AGC029C]Lexicographic constraints
题目大意:给定$n$个字符串的长度$a_i$,问至少用几种字符可以构造出字符串$s_1\sim s_n$,满足$|s_i|=a_i$且$s_1<s_2<\cdots<s_n$. $ ...
- 【题解】Palindrome pairs [Codeforces159D]
[题解]Palindrome pairs [Codeforces159D] 传送门:\(Palindrome\) \(pairs\) \([CF159D]\) [题目描述] 给定一个长度为 \(N\) ...
- Kruskal算法&Prim算法
最小生成树是什么? Kruskal算法 图文转载自a2392008643的博客 此算法可以称为"加边法",初始最小生成树边数为0,每迭代一次就选择一条满足条件的最小代价边,加入到最 ...
- HDU2577 How to Type
题目链接 一道DP问题 定义dp[i][j]为敲完第i个字母的最小花费,j=1代表Caps Lock打开,j=0代表Caps Lock关闭,则有: 如果第i个字母为大写: dp[i][1]=min(d ...
- SpringBoot与SpringDateJPA和Mybatis的整合
一.SpringBoot与SpringDateJPA的整合 1-1.需求 查询数据库---->得到数据------>展示到页面上 1-2.整合步骤 1-2-1.创建SpringBoot工程 ...
- python 中 try,except,finally 的执行顺序
写代码的时候发现了好玩的事情,常常作为终止的 return 语句并不总是能够立刻跳出函数 def A(): try: for i in range(10): if i == 5: return pri ...
- Java自学-接口与继承 隐藏
Java中的方法隐藏 与重写类似,方法的重写是子类覆盖父类的对象方法 隐藏,就是子类覆盖父类的类方法 步骤 1 : 父类 父类有一个类方法 :battleWin package charactor; ...
- 【译】Matplotlib:plotting
前言 本教程源于Scipy Lecture Notes,URL:http://www.scipy-lectures.org/ 本教程若有翻译不当或概念不明之处,请大家留言,博主及时更正,以便后来的用户 ...
- Java String 字符串
equals 字符串比较 String str = "furong"; String str1 = new String("furong"); System.o ...
- centos7快速安装coreDns
1.下载二进制文件 wget https://github.com/coredns/coredns/releases/download/v1.5.0/coredns_1.5.0_linux_amd64 ...