[Algorithm] 136. Single Number
2024-09-03 00:39:55
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: 1Example 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
最新文章
- 移动混合应用HTML5数据查询优化
- Winform 生成不需要安装的exe可执行文件 ILMerge使用
- 学习hadoop遇到的问题
- nginx参数说明
- InterBase数据库迁移到MySQL(说明)
- NEC学习 ---- 布局 -三列,左侧自适应
- linux下查看一个进程的启动时间和运行时间
- 在线白板,基于socket.io的多人在线协作工具
- Bootstrap--组件之Glyphicons字体图标
- wget获取https资源
- 超声波 HC-SR04
- jq事件
- C# 设置Excel数据自适应行高、列宽的2种情况
- qml demo分析(text-字体展示)
- pta编程总结3
- 使用cmd命令导入SQL文件
- Python JSON 基本使用
- 免费工资总额管控系统-JXHR2016
- 删除SQL Server大容量日志的方法(转)
- Win10系列:UWP界面布局进阶6
热门文章
- TP5接口开发之异常处理接管
- keil 编译器V6 定义函数在ram中运行-和在指定地址定义常量
- Win10 UEFI 系统安装教程
- C#中datatable操作
- Nginx优化配置,轻松应对高并发
- vue中对象数组去重
- redis-存储命令
- InvalidOperationException: No file provider has been configured to process the supplied file.
- SpringBoot2.x搭建Eureka
- 微信小程序 - scroll-view的scroll-into-view属性 - 在页面打开后滚动到指定的项