algorithm & bitwise operation & the best leetcode solutions
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的更多相关文章
- JS魔法堂:再识Bitwise Operation & Bitwise Shift
Brief linkFly的<JavaScript-如果...没有方法>中提及如何手写Math.round方法,各种奇技淫招看着十分过瘾,最让我惊叹的是 ~~(x + )) ,完全通过加法 ...
- a bitwise operation 广告投放监控
将随着时间不断增大的数字N个依次编号为1到N的N个球,颜色每次随机为红黑蓝,时间上先后逐个放入篮子中,计算离现在最近的24个球的红.黑.蓝颜色数 广告投放监控 a bitwise operation ...
- HDL之Bitwise operation
1 Verilog 1.1 Bitwise operator Bitwise operators perform a bit wise operation on two operands. They ...
- js bitwise operation all in one
js bitwise operation all in one 位运算 & 按位与 | 按位或 ^ 按位异或 / XOR let a = 5; // 000000000000000000000 ...
- LeetCode Solutions : Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- C语言之Bit-wise Operation和Logical Operation
首先第一点:十六进制位运算和逻辑运算 都是先转化二进制,后输出结果(十六进制,二或十)Bit-Wise Operations (位运算)包括:& 按位与 | 按位或 ^ 按位异或 ~ 取反 & ...
- 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 ...
- [Algorithm] Bitwise Operators
"|" can be used as assign "&" can be used as check // Read, Write, Execute / ...
- LeetCode Algorithm
LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...
随机推荐
- 对于Spring MVC 拦截器的一些了解
Spring MVC 拦截器的执行顺序 应用场景 假设请求 localhost:8080/ 则要求直接重定向到 localhost:8080/login ; 定义拦截器顺序 permission lo ...
- 扒一扒ELF文件
ELF文件(Executable Linkable Format)是一种文件存储格式.Linux下的目标文件和可执行文件都按照该格式进行存储,有必要做个总结. 目录 1. 链接举例 2. ELF文件类 ...
- 改造xxl-job的客户端日志文件生成体系
为什么要改造XXL-JOB原有的日志文件生成体系 xxl-job原本自己的客户端日志文件生成策略是:一个日志记录就生成一个文件,也就是当数据库存在一条日志logId,对应的客户端就会生成一个文件, ...
- Jenkins部署静态资源文件
Jenkins部署静态资源文件 1.Jenkins部署静态资源文件 1.1 设置项目名称.参数(环境.分支)等 1.2 配置源码管理 1.3 配置构建参数 2.项目实战demo 2.1 demo1 部 ...
- 浅聊ARP
今天借用思科公司的Cisco Packet Tracer Student这款软件浅聊ARP 什么是ARP? ARP即地址解析协议(Address Resolution Protocol),是根据Ip地 ...
- f5 http和tcp_80 monitor
f5上的http和tcp_80 monitor是有区别的.假如使用http为monitor,即使80端口是通的,但是有的情况f5也会根据http的访问返回值情况判断站点不可用. 如, telnet I ...
- java中的四种内部类使用(1)
内部类 (一) 概述 把类定义在另一个类的内部,该类就被称为内部类. 举例:把类Inner定义在类Outer中,类Inner就被称为内部类. class Outer { class Inner { } ...
- 一文读懂云上DevOps能力体系
简介: 阿里云ECS自动化运维套件架构师,深度拆解云上运维能力体系建设:自动化运维等级金字塔.自动化运维的进阶模式.DevOps的基础核心.云上标准化部署三大能力-- 序言 云计算行业已经有十多年的发 ...
- 三维CAD——基于B_rep的建模操作
内容来自高老师的<三维CAD建模>课,本文就主要介绍半边结构和欧拉操作以及代码实现. 1. 边界表示法及其数据结构 · 拓扑结构 a.拓扑元素:面.边.点.体 b.拓扑关系:9种.V{V} ...
- hdu3559 Frost Chain (概率dp+记忆化搜索)
Problem Description In the unimaginable popular DotA game, the hero Lich has a wonderful skill: Fros ...