algorithm & bitwise operation & the best leetcode solutions

leetcode 136 single-number

the better solution

/**
* @param {number[]} nums
* @return {number}
*/
var singleNumber = function(nums) {
return nums.reduce((sum, i) => sum ^ i, 0);
};
// Time complexity : O(n)
// Space complexity : O(1)

my solution

solution 1

"use strict";

/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-08-015
* @modified
*
* @description 136 single-number
* @difficulty Easy
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
*/ const log = console.log; var singleNumber = function(nums) {
let len = nums.length;
// obj unique key
const obj = {};
while(len) {
const value = nums[len - 1];
if(obj[value] === undefined) {
obj[value] = 1;
} else {
obj[value] += 1;
}
len--;
}
// log(`\nkeys`, Object.keys(obj))
// log(`values`, Object.values(obj))
// log(`entries`, Object.entries(obj))
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
const item = obj[`${key}`];
if(item === 1) {
return key;
}
}
}
};
// Time complexity : O(n)
// Space complexity : O(n) /* 输入: [2,2,1]
输出: 1 输入: [4,1,2,1,2]
输出: 4 */ const test = [2,2,1];
const result = singleNumber(test);
log(`result =`, result)
// 1 const test2 = [4,1,2,1,2];
const result2 = singleNumber(test2);
log(`result2 =`, result2)
// 4

如何使用 js 计算两个数组的交集、差集、并集、补集

https://www.hangge.com/blog/cache/detail_1862.html

refs

https://leetcode-cn.com/problems/single-number/

https://leetcode.com/problems/single-number/



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


algorithm & bitwise operation & the best leetcode solutions的更多相关文章

  1. JS魔法堂:再识Bitwise Operation & Bitwise Shift

    Brief linkFly的<JavaScript-如果...没有方法>中提及如何手写Math.round方法,各种奇技淫招看着十分过瘾,最让我惊叹的是 ~~(x + )) ,完全通过加法 ...

  2. a bitwise operation 广告投放监控

    将随着时间不断增大的数字N个依次编号为1到N的N个球,颜色每次随机为红黑蓝,时间上先后逐个放入篮子中,计算离现在最近的24个球的红.黑.蓝颜色数 广告投放监控 a bitwise operation ...

  3. HDL之Bitwise operation

    1 Verilog 1.1 Bitwise operator Bitwise operators perform a bit wise operation on two operands. They ...

  4. js bitwise operation all in one

    js bitwise operation all in one 位运算 & 按位与 | 按位或 ^ 按位异或 / XOR let a = 5; // 000000000000000000000 ...

  5. LeetCode Solutions : Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  6. C语言之Bit-wise Operation和Logical Operation

    首先第一点:十六进制位运算和逻辑运算 都是先转化二进制,后输出结果(十六进制,二或十)Bit-Wise Operations (位运算)包括:& 按位与 | 按位或 ^ 按位异或 ~ 取反 & ...

  7. LeetCode Solutions : Reorder List

    →-→Ln-1→Ln, reorder it to: L→Ln-2→- You must do this in-place without altering the nodes' values. Fo ...

  8. [Algorithm] Bitwise Operators

    "|" can be used as assign "&" can be used as check // Read, Write, Execute / ...

  9. LeetCode Algorithm

    LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...

随机推荐

  1. 深度漫谈数据系统架构——Lambda architecture

    https://mp.weixin.qq.com/s/whmhm2yzug2WVdH3dTq8hg

  2. 调度 GMP

    小结: 1. 当M从P的本地运行队列获取G时, 如果发现本地队列为空会尝试从其他P盗取一半的G过来,这个机制叫做Work Stealing, 2. Q M一定需要p吗? A 不一定.M正在执行原生代码 ...

  3. 服务降级 托底预案 Nginx中使用Lua脚本检测CPU使用率,当达到阀值时开启限流,让用户排队

    https://mp.weixin.qq.com/s/FZAcQQAKomGEe95kln1HCQ 在京东我们是如何做服务降级的 https://mp.weixin.qq.com/s/FZAcQQAK ...

  4. Spark DataSource Option 参数

    Spark DataSource Option 参数 1.parquet 2.orc 3.csv 4.text 5.jdbc 6.libsvm 7.image 8.json 9.xml 9.1读选项 ...

  5. MySQL数据库操作生成UUID

    问题描述: 通过数据库操作,生成无横线的uuid,同时插入至数据库之中. 因为我要给项目做一些测试数据,项目的主键为32位无'-'的uuid,然后在数据库中,通过数据库操作,然后插入一些测试数据. 生 ...

  6. hbase远程api调用, 远程连接开发,环境构建相关问题总结

    平时在开发的过程中,都是将hbase的环境装在虚拟机或者双系统上的,开发是直接在unix或者linux上进行的,这样对于使用上,有些繁琐. 下面我将介绍我在使用windows远程开发过程中的一些总结: ...

  7. 存储过程demo返回银行名称

    create or replace procedure TEST_NUMO(Cityno IN VARCHAR2,--输入参数 Deptid IN VARCHAR2,--输入参数 Retval OUT ...

  8. (3)UNIX/Linux系统结构

    UNIX/Linux 系统可以粗糙地抽象为 3 个层次,如图所示.底层是 UNIX/Linux 操作系统,即系统内核(Kernel):中间层是Shell层,即命令解释层:高层则是应用层. 1) 内核层 ...

  9. 使用timeout-decorator为python函数任务设置超时时间

    需求背景 在python代码的实现中,假如我们有一个需要执行时间跨度非常大的for循环,如果在中间的某处我们需要定时停止这个函数,而不停止整个程序.那么初步的就可以想到两种方案:第一种方案是我们先预估 ...

  10. 怎样将Sublime Text 设置成中文版(完整教程)

    1.打开Sublime Text,使用快捷键Shift+Ctrl+P,弹出查找栏,如图: 2.在搜索框中输入关键字 install ,出现下拉选项,点击选择其中的:Package Control: I ...