当前位置:网站首页>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);
}
边栏推荐
- Use BPF to count network traffic
- EasyNVR使用Onvif探测设备失败,显示“无数据”是什么原因?
- Business based precipitation component = & gt; manage-table
- How can programmers reduce bugs in development?
- (Video + graphics) introduction to machine learning series - Chapter 11 support vector machines
- Gateway solves cross domain access
- Number of occurrences of numbers in the array (medium difficulty)
- [JS Framework] Failed to execute the callback function:
- [golang] leetcode intermediate - jumping game & different paths
- 投资理财产品的钱能随时取出来吗?
猜你喜欢

Four security issues of low code and no code development

How to decompile APK files

Ten excellent business process automation tools for small businesses

NVM download, installation and use
Issue 39: MySQL time class partition write SQL considerations

Mcu-08 interrupt system and external interrupt application
[North Asia data recovery]_ mdb_ catalog. Mongodb database data recovery case in case of WT file corruption

Overall planning and construction method of digital transformation

How can programmers reduce bugs in development?

Eight recommended microservice testing tools
随机推荐
Skills of writing test cases efficiently
An analysis of the comments on the TV series Douban by procedural apes
Dunhuang Research Institute and Tencent have launched a new strategic cooperation to take you around the digital new silk road with AI
SQL basic tutorial (learning notes)
Six configuration management tools that administrators must know
From file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql The GPG key to retrieve the key source "MySQL 5.7 community server" is installed, but not applicable to
Redis series (3) - sentry highly available
Operation and maintenance guide | cos back source setting practice
Explanation of MySQL indexing principle
这个巡检平台你还不知道,真是亏大了!
Implementation of pure three-layer container network based on BGP
03. Tencent cloud IOT device side learning -- overview of mqtt control package
Error reported after NPM I
The 'ng' entry cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Check the spelling of the name. If you include a path, make sure the path is correct, and then
Nine practical guidelines for improving responsive design testing
Leetcode topic [array] -216- combined sum III
(Video + graphics) introduction to machine learning series - Chapter 11 support vector machines
EasyGBS视频平台TCP主动模式拉流异常情况修复
[North Asia data recovery]_ mdb_ catalog. Mongodb database data recovery case in case of WT file corruption
Ten software development indicators for project managers