首页 技术 正文
技术 2022年11月8日
0 收藏 387 点赞 2,043 浏览 2194 个字

Given an array of integers, every element appears twice except for one. Find that single one.

思路:

最经典的方法,利用两个相同的数异或结果为0的性质,则将整个数组进行异或,相同的数俩俩异或,最后得到的就是那个single number,复杂度是O(n)

代码:

class Solution {
public:
int singleNumber(vector<int>& nums) {
int res = 0;
for( int i = 0; i < nums.size(); i++ ){
res ^= nums[i];
}
return res;
}
};

Given an array of integers, every element appears three times except for one. Find that single one.

思路:


这题与上一题不同的地方在于,除了single number,其他数字都出现了3次,这样的话,整个异或就无法消去出现3次的数字,那我们有什么其他的办法呢?如果继续按照位来考虑,每一位可能出现的数字只有0或者1,而且如果某一位出现1,那么在这一位,所有数字(除了single num)出现1的次数总数一定是3的倍数,(比如某一位有5个不同数字是1,那么一共1在这一位出现了5*3 = 15次)那我们可以利用这个性质,统计每一位上1出现的总个数,然后总数除以3取它的余数(如果single number在这1位也是1,那么1一共出现16次,16%3 = 1),余数就是single num在这一位的数字。时间复杂度O(32N)

代码:

class Solution {
public:
int singleNumber(vector<int>& nums) {
int count[32] = {0};
int result = 0;
for (int i = 0; i < 32; i++) {
for (int j = 0; j < nums.size(); j++) {
if ((nums[j] >> i) & 1) {
count[i]++;
}
}
result |= ((count[i] % 3) << i);
}
return result;
}
};

思路2:  

那么有没有直接是O(N)的算法呢?答案是肯定的。这边使用了bitmask的概念,开始设3个flag,代表数字第一次出现,第二次出现,第三次出现如果出现1次,则one赋值,出现2次时候,one置0,twos赋值,出现3次时候,one,twos,都置0,threes赋值。具体可以参考https://discuss.leetcode.com/topic/2926/accepted-code-with-proper-explaination-does-anyone-have-a-better-idea/2

代码:

class Solution {
public:
int singleNumber(vector<int>& nums) {
int ones = 0, twos = 0, threes = 0;
for (int i = 0; i < nums.size(); i++) {
twos |= ones & nums[i];
ones ^= nums[i];
threes = ones & twos;
ones &= ~threes;
twos &= ~threes;
}
return ones;
}
};

 

Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.

For example:

Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

思路:

如果对所有元素进行异或,那么最后剩下的是两个single number的异或值,那么如何区分这两个single num呢?我们对最后的异或值进行分析,因为两个数字不同,那最后异或值一定不为0,如果某一位出现1表示这一位上,这两个数字是不同的,那我们就可以利用这个性质,通这一位,对整个数组进行分组,分为这一位是0的和这以为是1的,这样,我们就把这两个single number分到了两个数组中,问题就回到了single number1当中了。(via 剑指offer)

代码:

class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
int res = 0;
for( int i = 0; i < nums.size(); i++ ){
res ^= nums[i];
}
res &= -res;
vector<int> resoult {0,0};
for( int i = 0; i < nums.size(); i++ ){
if( (nums[i] & res) == 0 ){
resoult[0] ^= nums[i];
}
else{
resoult[1] ^= nums[i];
}
}
return resoult;
}
};

  这边比较难理解的是res &= -res;加入res = 2则二进制表示为10; -2的二进制表示为1111111111111111111111111111111111111111111111111111111111111110,则最后位与值为2,其他的数同理,最后得到的是从低位向高位的第一位不同的值。

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845