[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 ...
随机推荐
- Docker在Windows上的初体验
作为Docker的初学者,我有几个疑问,找到了答案,并实践了一下,希望对和我一样的初学者有帮助: 1.Docker是什么? 大家对虚拟机应该比较熟悉,虚拟机和docker都是为了实现隔离. 虚拟机隔离 ...
- 深入理解AQS
前记 在看JUC中并发相关的源码时经常看到AQS的身影,这到底是个什么鬼?必须要一探究竟. 一. AQS背景了解 JUC包中的锁,包括: Lock接口,ReadWriteLock接口,LockSupp ...
- cocos2d设置窗口标题
//窗口标题 #ifdef WIN32 CCEGLView* pGlView=CCDirector::sharedDirector()->getOpenGLView(); if (pGlView ...
- Nginx Tutorial #1: Basic Concepts(转)
add by zhj: 文章写的很好,适合初学者 原文:https://www.netguru.com/codestories/nginx-tutorial-basics-concepts Intro ...
- Java学习:面向对象三大特征:封装性、继承性、多态性之多态性。
面向对象三大特征:封装性.继承性.多态性之多态性. extends继承或者implemens实现,是多态性的前提. 例如:小菜是一个学生,但同时也是一个人.小菜是一个对象,这个对象既有学生形态,也有人 ...
- Linux常用命令wc
wc名字来源: wc -- word, line, character, and byte count The wc utility displays the number of lines, wor ...
- Configuration property name 'xxx' is not valid
目录 问题 解决 问题 程序出错:Configuration property name ‘xxx’ is not valid, Canonical names should be kebab-cas ...
- 灰度共生矩阵(Gray-level Co-occurrence Matrix,GLCM),矩阵的特征量
又叫做灰度共现矩阵 Prerequisites 概念 计算方式 对于精度要求高且纹理细密的纹理分布,我们取像素间距为d=1d=1,以下是方向的说明: 我们来看,matlab内置工具箱中的灰度共生矩阵的 ...
- 全程实操cdh5.14.4中集成安装kylin2.4.1与使用测试
在cdh5.14.4安装完成并排错完成的情况下,进行如下kylin安装操作: 1.实验环境 三台CentOS 7主机,IP地址 192.168.43.129 cm1 192.168.43.130 cm ...
- 突破Java面试-Redis集群模式的原理
1 面试题 Redis集群模式的工作原理说一下?在集群模式下,key是如何寻址的?寻址都有哪些算法?了解一致性hash吗? 2 考点分析 Redis不断在发展-Redis cluster集群模式,可以 ...