当前位置:网站首页>Leetcode-136-只出现一次的数(用异或来解答)
Leetcode-136-只出现一次的数(用异或来解答)
2022-07-07 13:53:00 【_春天_】
题目描述
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
说明:
你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
示例 1:
输入: [2,2,1]
输出: 1
示例 2:
输入: [4,1,2,1,2]
输出: 4
Tips:用异或来解决
基础知识
异或的特性:
1.恒定律:A ^ 0 = A
2.归零率:A ^ A = 0
3.交换律:A ^ B = B ^ A
4.结合律:(A ^ B) ^ C = A ^ (B ^ C)
应用点
可以通过异或来判断两值是否相等:a ^ b == 0,则a与b相等。
用异或来消除出现两次的数
解题思路
利用异或的归零率,使所有出现过两次的数变为0,而只出现过一次的数与0运算还能保持本身。可以看下面这个例子。
假设所有的数组为:abcbcda
a ^ b ^ c ^ b ^ c ^ d ^ a
= a ^ a ^ b ^ b ^ c ^ c ^ d
= 0 ^ 0 ^ 0 ^ d
= d
代码
解法一:
我第一次的辣鸡解法,楞找。
时间复杂度高,最后一个测试用例没有通过。
def singleNumber(nums):
"""
:type nums: List[int]
:rtype: int
"""
validated = []
for i, num in enumerate(nums):
if num in validated:
continue
single = True
for j in range(len(nums) - i - 1):
print(num, nums[j+i+1])
if num == nums[j + i + 1]:
validated.append(num)
single = False
if single:
print(num)
break
解法二:优秀的算法:
def excellent_singleNumber(nums):
a = 0
for num in nums:
a ^= num
return a
第一眼看到这个算法的时候觉得太惊艳了……
边栏推荐
- Vite path alias @ configuration
- Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
- Excessive dependence on subsidies, difficult collection of key customers, and how strong is the potential to reach the dream of "the first share of domestic databases"?
- Postman generate timestamp, future timestamp
- Three. JS introductory learning notes 10:three JS grid
- LeetCode3_ Longest substring without duplicate characters
- Tkinter after how to refresh data and cancel refreshing
- 47_Opencv中的轮廓查找 cv::findContours()
- leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
- Async and await
猜你喜欢

C4D learning notes 1- animation - animation key frames

Cocos creator collision and collision callback do not take effect

numpy--疫情数据分析案例

Postman generate timestamp, future timestamp

分步式监控平台zabbix

Three. JS introductory learning notes 03: perspective projection camera

Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"

C4D learning notes 2- animation - timeline and time function

Three. JS introductory learning notes 19: how to import FBX static model

讲师征集令 | Apache SeaTunnel(Incubating) Meetup 分享嘉宾火热招募中!
随机推荐
webgl_ Graphic transformation (rotation, translation, zoom)
Function: JS Click to copy content function
Three. Introduction to JS learning notes 17: mouse control of 3D model rotation of JSON file
Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
航运船公司人工智能AI产品成熟化标准化规模应用,全球港航人工智能/集装箱人工智能领军者CIMC中集飞瞳,打造国际航运智能化标杆
The unity vector rotates at a point
Summary of knowledge points of xlua hot update solution
XMIND frame drawing tool
C4D learning notes 3- animation - animation rendering process case
Unity3D_ Class fishing project, control the distance between collision walls to adapt to different models
TS as a general cache method
Asynchronous application of generator function
Getting started with webgl (1)
无线传感器网络--ZigBee和6LoWPAN
Iterator and for of.. loop
Three. JS introductory learning notes 05: external model import -c4d into JSON file for web pages
Shader basic UV operations, translation, rotation, scaling
numpy---基础学习笔记
Numpy --- basic learning notes
Notification uses full resolution