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: 1

Example 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的更多相关文章

  1. leetcode 136 Single Number, 260 Single Number III

    leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...

  2. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

  3. LeetCode 136. Single Number C++ 结题报告

    136. Single Number -- Easy 解答 相同的数,XOR 等于 0,所以,将所有的数字 XOR 就可以得到只出现一次的数 class Solution { public: int ...

  4. LeetCode 136. Single Number(只出现一次的数字)

    LeetCode 136. Single Number(只出现一次的数字)

  5. 136. Single Number - LeetCode

    Question 136. Single Number Solution 思路:构造一个map,遍历数组记录每个数出现的次数,再遍历map,取出出现次数为1的num public int single ...

  6. 【LeetCode】136. Single Number (4 solutions)

    Single Number Given an array of integers, every element appears twice except for one. Find that sing ...

  7. 136. Single Number唯一的数字

    [抄题]: Given an array of integers, every element appears twice except for one. Find that single one. ...

  8. 136. Single Number唯一的一个只出现了一次的数字

    [抄题]: Given a non-empty array of integers, every element appears twice except for one. Find that sin ...

  9. LeetCode Problem 136:Single Number

    描述:Given an array of integers, every element appears twice except for one. Find that single one. Not ...

随机推荐

  1. Python Web 之 Flask SQLalchemy

    Flask-SQLalchemy 一. 一对多 A表中的一条记录与B表中的多天记录关联 语法实现: 在"多"实体类中增加 外键列名 = db.Column(db.Integer, ...

  2. SQL系列(五)—— 排序(order by)

    对查询结果进行排序是日常应用开发中最为常见的需求,在SQL中通过order by实现.order by是select语句中一部分,即子句. 1.order by 1.1 单列排序 其实,检索出的数据并 ...

  3. Elasticsearch 史上最全最常用工具清单

    基础类工具 1.Head插件 1)功能概述: ES集群状态查看.索引数据查看.ES DSL实现(增.删.改.查操作) 比较实用的地方:json串的格式化 2)地址:http://mobz.github ...

  4. 【vue】搭建vue环境以及要安装的所有东西

    参考地址: https://www.cnblogs.com/laizhouzhou/p/8027908.html

  5. mysql给某个用户单个表权限

    CREATE USER systemselect IDENTIFIED BY 'Zbank123456';#只给查询权限 GRANT SELECT ON szkitil.zbank_businesss ...

  6. Git 理解修改

    参考链接:https://www.liaoxuefeng.com/wiki/896043488029600/897884457270432 Git之所以比其他版本控制系统设计得优秀,就是因为Git跟踪 ...

  7. Mysql关键字Explain 性能优化神器

    Explain工具介绍 使用EXPLAIN关键字可以模拟优化器执行SQL语句,分析查询语句或是结构的性能瓶颈.在select语句之前增加explaion关键字,MySQL会在查询上设置一个标记,执行查 ...

  8. 最强在线文件格式转换(支持200+文件格式如常用的PDF,DOCX,JPG,GIF,MP3,MP4,FLV,MOBI)(通用)

    网站展示:http://www.alltoall.net/ 分类简洁 支持的所有文件格式展示: 单独展示文档转换: 单独展示PDF转换:

  9. vue 指令中el 的 parentNode 为空的问题

    在项目中,突然发现在用vue指令的时候,发现元素el的父元素parentNode变成了null. 代码: if (el.parentNode && !Vue.prototype.$_h ...

  10. MySql操作命令创建学生管理系统

    1.创建学生管理系统数据库xscj create detabase 数据库名: 2.打开数据库 use 数据库名: //创建数据库之后,该数据库不会自动成为当前数据库需要用use来指定 3.创建表名 ...