当前位置:网站首页>Leetcode question 136 [single number]
Leetcode question 136 [single number]
2022-06-24 18:14:00 【yscisco】
c Language implementation leetcode The first 136 subject
Subject requirements
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.
analysis
The title stem says that all integer elements in a non empty array appear in pairs , Only one element appears once , Find this element .
in addition , The topic also requires that the complexity should be linear , In this case, simple loop nesting will not work , Because the complexity of nested loops is nonlinear .
When looking up data , Found a more ingenious method , Record here . This method makes use of the property of XOR operation in computer bit operation .
0^0=0
1^0=1
1^1=0
0^1=1
- 0 Exclusive or 0 by 0
- 0 XOR any number for itself
- 1 XOR any number negates it
- Any number XOR is itself 0
a ^ 0 = a
a ^ a = 0
Same as 0, Different for 1.
Besides , XOR operation also follows commutative law and associative law . namely
a^b^c^a^b
=(a^a)^(b^b)^c
=0^0^c
=c
Based on the above rules , The train of thought of this question is also there . XOR all the elements in the array , The last value is the element value that appears only once .
c Language implementation
#include <stdio.h>
int singleNumber(int* nums, int numsSize){
int i=0;
int t=0;
for (i=0;i<numsSize;i++){
t = nums[i]^t;
}
return t;
}
int main(){
int arr[9] = {
6,5,11,4,3,3,4,5,6};
int t;
t = singleNumber(arr, 9);
printf("%d\n",t);
}
边栏推荐
- What if the database table structure changes? Smartbi products support one click synchronization
- NVM download, installation and use
- Common MySQL commands of installation free version
- Is it safe to open an account online? What should I do?
- 视频平台如何将旧数据库导入到新数据库?
- A solution to the problem that the separator of WordPress title - is escaped as -
- 1. Leveldb getting started
- Leveldb source code analysis -- writing data
- Design topic: MATLAB cellular automata personnel evacuation
- C language - structure II
猜你喜欢
Two micro service interviews where small companies suffer losses
Constantly changing the emergency dialing of harmonyos ETS during the new year
Recommend 14 commonly used test development tools
How can an enterprise successfully complete cloud migration?
He "painted" what a smart city should look like with his oars
Using flex to implement common layouts
NVM download, installation and use
Nacos cluster starts throwing set of SQL_ SELECT_ LIMIT is not support
LC 300. Longest increasing subsequence
Millions of dollars worth of NFT were stolen in the attack, and Google issued an emergency warning to 3.2 billion users worldwide | February 21 global network security hotspot
随机推荐
13 ways to reduce the cost of cloud computing
On the principle of cloud streaming multi person interaction technology
Four security issues of low code and no code development
congratulate! The first dragon lizard community annual outstanding contribution award is announced. Check it now
Service not found troubleshooting and resolution of error messages in the secondary development of the source code of the open source platform easydarwin
How to decompile APK files
Easyplayer streaming media player plays HLS video. Technical optimization of slow starting speed
Design topic: MATLAB cellular automata personnel evacuation
Users of the Tiktok open platform are authorized to obtain the user's fan statistics and short video data
NVM download, installation and use
About whether arm's large and small end mode is related to CPU or compiler
布隆过滤器综述文章论文阅读:Optimizing Bloom Filter: Challenges, Solutions, and Comparisons
Regression testing strategy for comprehensive quality assurance system
【你真的会用ES吗】ES基础介绍(一)
视频平台如何将旧数据库导入到新数据库?
Easygbs video platform TCP active mode streaming exception repair
Ten software development indicators for project managers
Ten excellent business process automation tools for small businesses
-Bash: wget: command not found
腾讯云荣获“可信云技术最佳实践-虚拟化”