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. 就是通过事件方法,在window.loaction.href里追加了参数字符串

    参考博文:https://www.kancloud.cn/digest/yvettelau/137669

  2. Centos 7 Rabbitmq 安装并开机启动

    准备工作 安装wget yum install -y wget rabbitmq安装需要依赖erlang,erlang安装参考:https://www.cnblogs.com/swyy/p/11582 ...

  3. tricks - 思维

    编辑 目录 tricks 系列 随机的性质 bitmask 建图 最基本的 黑白染色 Kruskal重构树 并查集维护值域 带根号的数三元环 根号分治 调和级数哈希 多属性哈希 时光倒流 时光反复横跳 ...

  4. Atlas 2.1.0 实践(4)—— 权限控制

    Atlas的权限控制非常的丰富,本文将进行其支持的各种权限控制的介绍. 在atlas-application.properties配置文件中,可以设置不同权限的开关. atlas.authentica ...

  5. spark-submit提交spark任务的具体参数配置说明

    spark-submit提交spark任务的具体参数配置说明 1.spark提交任务常见的两种模式 2.提交任务时的几个重要参数 3.参数说明 3.1 executor_cores*num_execu ...

  6. elasticsearch7.8权限控制和规划

    由于在版本7开始,x-pack可以免费使用了,但是权限控制免费的不够细,但是控制到索引级别都基本够用了.付费的可以体验更细致的权限控制.本文的基础是已经有了es集群的基础上进行的. 官网:https: ...

  7. 基于C#/Winform实现的Win8MetroLoading动画

    非常喜欢Metro风格的界面,所以想模仿一下一些UI效果的实现,网上找到了很多,但都是CSS3,WPF等实现,对于XAML和CSS3一窍不通,无奈下只有自己开始写. 下面是源码: 1 using Sy ...

  8. docker部署 springboot 多模块项目+vue

    之前学习了docker,今天就来试试将这个项目打包成docker镜像并通过运行一个镜像来运行项目.这里使用的项目是el-admin.是一个开源的springboot后端管理框架(前端vue),有兴趣的 ...

  9. B、小花梨的三角形(解题报告)

    通过set进行标记(思想很简单,实现起来有点容易错)set(红黑树:效率高) 思路: 对行列和长度进行枚举: 对三个顶点进行排序 储存顶点后计数输出 #include<iostream> ...

  10. hdu5365Shortest Path (floyd)

    Problem Description There is a path graph G=(V,E) with n vertices. Vertices are numbered from 1 to n ...