[Leetcode] single number 找单个数
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
题意:给定一未排序的数组,其中只有一个数仅出现一次,其余都出现两次,找到仅出现一次的这个数。
思路:一般的暴力解法或者先排序,后遍历。显然这两种种做法不行,时间复杂度上没法满足线性。所以采用逻辑异或来解题。逻辑异或有几个特点:
一、异或满足交换律;二、相同两个数异或为0;三、0异或一个数为那个数本身。十位数的异或,先将其转变二进制,然后进行异或。参考了LeetCode OJ代码如下:
class Solution {
public:
int singleNumber(int A[], int n)
{
int res=;
for(int i=;i<n;++i)
{
res^=A[i];
}
return res;
}
};
同样的思路还有一种解法,牛客网@setsail,这种解法也是根据相同数异或为0的特性,找到仅出现一次的那个数。
class Solution {
public:
int singleNumber(int A[], int n)
{
if(n==)
return A[];
while(n>=)
{
A[n-]^=A[n-];
n--;
}
return A[];
}
};
[Leetcode] single number 找单个数的更多相关文章
- LeetCode Single Number (找不不重复元素)
题意:给一个序列,序列中只有1个是单个的,其他都是成对出现的.也就是序列中有奇数个元素.要求找出这个元素. 思路:成对出现用异或最好了.两个同样的数一异或就变零,剩下的,就是那个落单的. class ...
- LeetCode Single Number II 单元素2
题意:给一个序列,其中只有1个元素只出现1次,其他的都一定出现3次.问这个出现一次的元素是多少? 思路: (1)全部元素拆成二进制,那么每个位上的1的个数应该是3的倍数,如果不是3的倍数,则ans的这 ...
- [Leetcode] single number ii 找单个数
Given an array of integers, every element appears three times except for one. Find that single one. ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode:Single Number II
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
随机推荐
- 面试遇到的订单表sql的解决方案
对于以下需求:用户表:users (user_id int)订单表:order_tb(user_id int, or_time date, or_money double)求以下用户:一月下过单, ...
- hdu1421搬寝室(动态规划)
搬寝室 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- Qt-QML-关于两个平级的qml文件中的函数调用问题
这几天还在继续搞我的QML,感悟就QML是坑的同时,也是一门很号的语言,用于快速搭界面是很好的.那么,这几天, 遇到一个问题,在下用一个框框画一下,希望可以理解 抽象派,解释一下,QML1和QML3是 ...
- CentOS安装JMeter
mkdir /usr/local/jmeter 新建jmeter目录 cd /usr/local/jmeter 进入jmeter目录 wget https://archive.apache.org/d ...
- Selenium(Python)调用pywin32上传图片
import unittestfrom time import sleep import osfrom selenium import webdriverimport win32apiimport w ...
- 【wx:for】小程序列表渲染的使用说明
wx:for 控制属性绑定一个数组,即可使用数组中各项的数据重复渲染该组件. 默认数组的当前项的下标变量名默认为 index,数组当前项的变量名默认为 item,即: {{index}} . {{it ...
- Python全栈 Web(HTML基础语法)
原文地址: https://yq.aliyun.com/articles/632672 .............................................. ...
- Python全栈 Web(概述、HTML基础语法)
原文地址: https://yq.aliyun.com/articles/631222 ........................................................ ...
- 配置vConsole调试console
1.使用 npm 安装: npm install vconsole 再使用webpack,然后js代码中 import VConsole from 'vconsole/dist/vconsole.mi ...
- 孤荷凌寒自学python第八十三天初次接触ocr配置tesseract环境
孤荷凌寒自学python第八十三天初次接触ocr配置tesseract环境 (完整学习过程屏幕记录视频地址在文末) 学习Python我肯定不会错过图片文字的识别,当然更重要的是简单的验证码识别了,今天 ...