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. Mac 禁用动画

    # opening and closing windows and popovers defaults write -g NSAutomaticWindowAnimationsEnabled -boo ...

  2. Golang--Directional Channel(定向通道)

    Directional Channel 通道可以是定向的(directional).在默认情况下,通道将以双向的(bidirectional)形式运作,用户既可以把值放人通道,也可以从通道取出值;但是 ...

  3. Python3爬取猫眼电影信息

    Python3爬取猫眼电影信息 import json import requests from requests.exceptions import RequestException import ...

  4. var_dump和var_export区别

    1.var_dump() :获取结构化的数据,按照数组的层级输出 2.var_export() :获取结构化的数据,返回有效的php代码,保留结构化形式的存储数据,数据类型为字符串. 例如: < ...

  5. 一文弄懂-BIO,NIO,AIO

    目录 一文弄懂-BIO,NIO,AIO 1. BIO: 同步阻塞IO模型 2. NIO: 同步非阻塞IO模型(多路复用) 3.Epoll函数详解 4.Redis线程模型 5. AIO: 异步非阻塞IO ...

  6. 在VirtualBox上安装Ubuntu-20.04

    本文主要介绍如何在VirtualBox上安装Ubuntu-20.04 目录 下载VirtualBox 下载Ubuntu-20.04镜像 新建虚拟机 第一步:打开VirtualBox 第二步:设置虚拟机 ...

  7. CF 1288 E. Messenger Simulator

    CF 1288 E. Messenger Simulator 题目传送门 官方题解 题意想必大家都明白了这里就不赘述了,这里只想重点记录一下几种实现方法 分析 设向前移动的序列为\(a\)序列 对于没 ...

  8. 2019牛客暑期多校训练营(第九场)B Quadratic equation (平方剩余)

    \((x+y)\equiv b\pmod p\) \((x\times y)\equiv c\pmod p\) 由第一个式子可知:\(x+y=b~or~x+y=b+p\) 先任选一个代入到第二个式子里 ...

  9. 2020年10月ICPC & 天梯赛 选拔赛【ACFJ】

    A. 表达式 题意 题解 将所有数字替换为A,运算符替换为O,然后不断合并(AOA),判断表达式最后是否为A即可. 注意将数字替换时判断有无前导零. 代码 #include <bits/stdc ...

  10. HDU6370 Werewolf 【基环内向树】

    HDU6370 Werewolf 题意: 有\(N\)个人玩狼人杀,只有村民和狼人,每个人指定另一个人并指出一个身份,其中:村民是不会说谎的,狼人是有可能说谎的,问在所有情况下必然是狼人的人数和必然是 ...