当前位置:网站首页>Numbers that only appear once
Numbers that only appear once
2022-06-28 14:12:00 【Hua Weiyun】
title: A number that appears only once ( Water is a simple problem )
date: 2022-05-04 19:59:40
tags: Make a little progress every day
subject
subject : A number that appears only once
Given an array of non-empty integers , Except that an element only appears once , Each of the other elements occurs twice . Find the element that only appears once .
explain : Your algorithm should have linear time complexity . Can you do this without using extra space ?
Example 1:
Input : [2,2,1] Output : 1Example 2:
Input : [4,1,2,1,2] Output : 4Answer key :
You can use XOR directly (^, Same as 0, Different for 1) To solve the , Let's start with the plan , All numbers in the XOR array , The last thing left is the number that appears only once
analysis :
a^a = 0 0^b = b , therefore a^a ^ b = b
Take a number for example :
3 ^ 3 = 0011^0011 = 0000 = 0
0 ^ 2 = 0000^0010 = 0010 = 2
therefore 3^3 ^2 = 2
Finally, after all the even number of exclusive or, it becomes 0 了 ,0 In and once ( In fact, the same is true for odd times ) Exclusive or , What we get is the number we want
Code :
class Solution { public int singleNumber(int[] nums) { // Using XOR , because a^b^a = b int res = 0; for (int num : nums) { // therefore res ^= num; } return res; }}These are the numbers that only appear once 【LeetCode】 The whole content of
Copyright notice :
Original Blogger : Cowherd Conan
Personal blog links :https://www.keafmd.top/**
If it helps you , Thank you for clicking on == One key, three links == Support !
[ ha-ha ][ Huai Quan ]

come on. !
Joint efforts !
Keafmd
You can see it here , You know the following , Let's make progress together !
边栏推荐
- To be the Italian Islander? Liuqiangdong cashed out 6.6 billion yuan in two months and made a one-time 560million "emergency transfer" to buy the European maritime Palace
- Other domestic mobile phones failed to fill the vacancy of Huawei, and apple has no rival in the high-end mobile phone market
- 智联招聘基于 Nebula Graph 的推荐实践分享
- PostgreSQL surpasses MySQL
- 2021计算机三级数据库大题总结
- Foreign trade SEO Webmaster Tools
- What are the products of increased life insurance?
- G: maximum flow problem
- DevEco Studio 3.0编辑器配置技巧篇
- Inftnews | technology giants accelerate their march into Web3 and metauniverse
猜你喜欢

一个bug肝一周...忍不住提了issue

Nature子刊 | 绘制植物叶际菌群互作图谱以建立基因型表型关系

基于ASP的勤工俭学管理系统

Research and Simulation of chaotic digital image encryption technology based on MATLAB

Websocket automatically disconnects in 1 minute

推荐四款可视化工具,解决 99% 的可视化大屏项目!

Kubernetes' in-depth understanding of kubernetes (II) declaring organizational objects

Euler equation: a truly perfect formula in the history of mathematics!

A bug liver a week I can't help mentioning issue

中国内地仅四家突围 联想智慧颐和园荣获 “2022年IDC亚太区智慧城市大奖”
随机推荐
RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)
New drug discovery methods, AstraZeneca team improves ab initio molecular design through course learning
正则匹配数字,英文以及英文符号
[binary tree] the minimum string starting from the leaf node
Embedded design and development project - liquid level detection and alarm system
Foreign trade SEO Webmaster Tools
What is the progress of China open source with 7.55 million developers?
只出现一次的数字(水了个简单题)
How to back up MySQL_ The most complete MySQL backup method in history
PostgreSQL surpasses MySQL
CVPR disputes again: IBM's Chinese draft papers were accused of copying idea, who won the second place in the competition
药物发现新方法,阿斯利康团队通过课程学习改进从头分子设计
坐拥755万开发者的中国开源,进度几何?
Connected to rainwater series problems
Yii2 writing the websocket service of swoole
Operation and maintenance thinking | do you know the relationship between CMDB and monitoring?
Is it safe to open an account on the flush
Cat dog queue
Why can't Bert completely kill the BM25??
Make an ink screen electronic clock, cool!