当前位置:网站首页>【无标题】
【无标题】
2022-07-05 22:27:00 【CodeStars码星人】
136. Single Number
难度简单2468收藏分享切换为中文接收动态反馈
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Example 1:
Input: nums = [2,2,1]
Output: 1
Example 2:
Input: nums = [4,1,2,1,2]
Output: 4
Example 3:
Input: nums = [1]
Output: 1
Constraints:
1 <= nums.length <= 3 * 104-3 * 104 <= nums[i] <= 3 * 104- Each element in the array appears twice except for one element which appears only once.
答案
方法一:位运算
如果不考虑时间复杂度和空间复杂度的限制,这道题有很多种解法,可能的解法有如下几种。
使用集合存储数字。遍历数组中的每个数字,如果集合中没有该数字,则将该数字加入集合,如果集合中已经有该数字,则将该数字从集合中删除,最后剩下的数字就是只出现一次的数字。
使用哈希表存储每个数字和该数字出现的次数。遍历数组即可得到每个数字出现的次数,并更新哈希表,最后遍历哈希表,得到只出现一次的数字。
使用集合存储数组中出现的所有数字,并计算数组中的元素之和。由于集合保证元素无重复,因此计算集合中的所有元素之和的两倍,即为每个元素出现两次的情况下的元素之和。由于数组中只有一个元素出现一次,其余元素都出现两次,因此用集合中的元素之和的两倍减去数组中的元素之和,剩下的数就是数组中只出现一次的数字。
上述三种解法都需要额外使用 O(n)O(n) 的空间,其中 nn 是数组长度。
如何才能做到线性时间复杂度和常数空间复杂度呢?

边栏推荐
- Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
- The difference between MVVM and MVC
- Assign the output of a command to a variable [repeat] - assigning the output of a command to a variable [duplicate]
- Cobaltstrike builds an intranet tunnel
- Oracle advanced query
- Calculation method of boundary IOU
- 2022-07-05:给定一个数组,想随时查询任何范围上的最大值。 如果只是根据初始数组建立、并且以后没有修改, 那么RMQ方法比线段树方法好实现,时间复杂度O(N*logN),额外空间复杂度O(N*
- Oracle views the data size of a table
- 元宇宙中的三大“派系”
- Hcip day 16
猜你喜欢

Qtquick3d real time reflection

Alternating merging strings of leetcode simple questions

What about data leakage? " Watson k'7 moves to eliminate security threats

南京:全面启用商品房买卖电子合同

boundary IoU 的计算方式

點到直線的距離直線的交點及夾角

A trip to Suzhou during the Dragon Boat Festival holiday

Stored procedures and stored functions

Leetcode simple question ring and rod

U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
随机推荐
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Promql demo service
Overview of concurrency control
EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
Overriding equals() & hashCode() in sub classes … considering super fields
opencv 判断点在多边形内外
Platformio create libopencm3 + FreeRTOS project
Two stage locking protocol for concurrency control
Distributed resource management and task scheduling framework yarn
Distance from point to line intersection and included angle of line
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
Assign the output of a command to a variable [repeat] - assigning the output of a command to a variable [duplicate]
BFC block level formatting context
Oracle views the data size of a table
Alternating merging strings of leetcode simple questions
U盘的文件无法删除文件怎么办?Win11无法删除U盘文件解决教程
[groovy] groovy dynamic language features (automatic type inference of function arguments in groovy | precautions for function dynamic parameters)
Draw a red lantern with MATLAB
QT creator 7 beta release