Leetcode SingleNumber I & II & III 136/137/260
SingleNumber I:
题目链接:https://leetcode-cn.com/problems/single-number/
题意:
给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
说明:
你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
示例 1:
输入: [2,2,1]
输出: 1
示例 2:
输入: [4,1,2,1,2]
输出: 4
分析:
利用异或(xor)运算,两个相同的数异或为0
代码如下:
class Solution {
public:
int singleNumber(int A[], int n) {
int ans = ;
for(int i = ; i < n; ++i) {
ans ^= A[i];
}
return ans;
}
};
SingleNumber II:
题目链接:https://leetcode-cn.com/problems/single-number-ii/
借鉴:https://www.cnblogs.com/grandyang/p/4263927.html
题意:
给定一个非空整数数组,除了某个元素,其余每个元素均出现了三次。找出那个没出现过三次的元素。
说明:
你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
示例 1:
输入: [2,2,3,2]
输出: 3
示例 2:
输入: [0,1,0,1,0,1,99]
输出: 99
分析1:
与第一题的不同是这里相同的数出现了3次,如果能定义一种3进制异或运算,就可以以第一题的方式解出这道题了。
代码如下:
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res = ;
for (int i = ; i < ; ++i) {
int sum = ;
for (int j = ; j < nums.size(); ++j) {
// 取nums[j]的第i位值,然后加起来
sum += (nums[j] >> i) & ;
}
// sum目前的有进位相加的结果,模3以后就是无进位相加的结果
res |= (sum % ) << i;
}
return res;
}
};
分析2:
还有一种解法,思路很相似,用3个整数来表示INT的各位的出现次数情况,one表示出现了1次,two表示出现了2次。当出现3次的时候该位清零。最后答案就是one的值。
ones代表第ith 位只出现一次的掩码变量twos代表第ith 位只出现两次次的掩码变量threes代表第ith 位只出现三次的掩码变量
代码如下:
class Solution {
public:
int singleNumber(vector<int>& nums) {
int one = , two = , three = ;
for (int i = ; i < nums.size(); ++i) {
// one & nums[i]的结果某一位上为1意味着这一位在上一轮出现过一次,在这一轮又出现过一次,那么也就意味着出现了两次
// two此时包含所有出现过2次和3次的
two |= one & nums[i];
// one的某一位为1,表示之前出现过1次。如果nums[i]对应位为0,one的某一位没有变化;如果nums[i]对应位为1,one的某一位变为0,表示这一位出现2次了。
// one的某一位为0,表示之前出现过0次或2次。如果nums[i]对应位为0,one的某一位没有变化;如果nums[i]对应位为1,one的某一位变为1,表示这一位出现过1次或3次。
// one此时包含所有出现过1次和3次的
one ^= nums[i];
three = one & two;
// 消掉3次的
one &= ~three;
two &= ~three;
}
return one;
}
};
分析3:
和分析2一样,不过这次只需要2个变量。
ones代表第ith 位只出现一次的掩码变量twos代表第ith 位只出现两次次的掩码变量
ones[i]原:ones第i位二进制位原来的值
twos[i]原:twos第i位二进制位原来的值
x[i]:所要异或的数的第i位二进制位的值
ones[i]:ones第i位二进制位异或后的值
ones[i]:ones第i位二进制位异或后的值
| ones[i]原 | twos[i]原 | x[i] | ones[i] | twos[i] |
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 0 | 1 |
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 0 | x | x |
| 0 | 0 | 1 | 1 | 0 |
| 0 | 1 | 1 | 0 | 0 |
| 1 | 0 | 1 | 0 | 1 |
| 1 | 1 | 1 | x | x |
| x\ones原twos原 | 00 | 01 | 10 | 11 |
| 0 | 0 | 0 | 1 | x |
| 1 | 1 | 0 | 0 | x |
| x\onestwos原 | 00 | 01 | 10 | 11 |
| 0 | 0 | 1 | 0 | x |
| 1 | 1 | 0 | 0 | x |
得到公式为:
ones = (ones ^ x) & ~twos;
twos = (twos ^ x) & ~ones;
代码如下:
class Solution {
public:
int singleNumber(vector<int>& nums) {
int twos = , ones = ;
for (int i = ; i < nums.size(); ++i) {
// ones ^ nums[i]后ones的某一位1可能表示原来出现过0次,也可能表示原来出现过2次
// & ~twos表示把原来出现过2次的部分删掉
ones = (ones ^ nums[i]) & ~twos;
// twos ^ nums[i]后twos的某一位1可能表示目前出现过2次,也可能表示目前出现过1次
// & ~ones表示把目前出现过1次的部分删掉
twos = (twos ^ nums[i]) & ~ones;
}
return ones | twos;
}
};
SingleNumber III:
题目链接:https://leetcode-cn.com/problems/single-number-iii/
题意:
给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。
示例 :
输入:[1,2,1,3,2,5]
输出:[3,5]
注意:
- 结果输出的顺序并不重要,对于上面的例子,
[5, 3]也是正确答案。 - 你的算法应该具有线性时间复杂度。你能否仅使用常数空间复杂度来实现?
- 结果输出的顺序并不重要,对于上面的例子,
分析:
将所有数异或一遍,那个异或完后一定有某一位是1,这意味着两个数在这一位上不同,因此只要把这一位上为0的数和为1的数分别全部异或一遍就能得到只出现一次的2个元素了。
代码如下:
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
vector<int> ans;
int a = , b = , lowbit = ;
for(int i = ; i < (int)nums.size(); ++i) {
lowbit ^= nums[i];
}
lowbit = lowbit & (-lowbit);
for(int i = ; i < (int)nums.size(); ++i) {
if(nums[i] & lowbit) a ^= nums[i];
else b ^= nums[i];
}
ans.push_back(a);
ans.push_back(b);
return ans;
}
};
Leetcode SingleNumber I & II & III 136/137/260的更多相关文章
- Leetcode 136 137 260 SingleNumber I II III
Leetccode 136 SingleNumber I Given an array of integers, every element appears twice except for one. ...
- 136.137.260. Single Number && 位运算
136. Single Number 意思就是给你一堆数,每个数都出现了两次,只有一个数只出现了一次,找出这个数 位运算(和c艹一样) &:按位与 |:按位或 ^:异或(一样为0,不一样为1) ...
- [LeetCode] Contains Duplicate(II,III)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- Leetcode 137. Single Number I/II/III
Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XO ...
- 买卖股票的最佳时机I II III IV
I 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. II 假设有一个数组,它的第i个元素是一个给定的股票 ...
- [LeetCode]singleNumber
题目:singleNumber Given an array of integers, every element appears twice except for one. Find that si ...
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
随机推荐
- docker实战练习(一)
systemctl start docker systemctl pause docker systemctl unpause docker systemctl start docker system ...
- php面试题整理(一)
str_split() print_r(str_split("Shanghai"));Array ( [0] => S [1] => h [2] => a [3] ...
- IO的详细解释:It's all about buffers: zero-copy, mmap and Java NIO
There are use cases where data need to be read from source to a sink without modification. In code t ...
- 如何在C#Asp.Net MVC使用Redis缓存
为什么要在Asp.Net MVC项目中使用Redis缓存呢?系统是按照高负载高并发来设计的,这就涉及服务器集群带来的问题,Session存储验证码或登录信息,在系统登录的时候,可能展示登录界面和存储验 ...
- KazaQ's Socks (找规律)
#include<iostream> using namespace std; #define ll long long ll n, m; ll t; int main(){ while ...
- ubantu下装Docker
Docker 要求 Ubuntu 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的 Ubuntu 版本是否支持 Docker. 通过 uname -r 命令查看你当前的内核版本 unam ...
- SQLNET.AUTHENTICATION_SERVICES操作系统认证登录的设定
$ORACLE_HOME/network/admin/sqlnet.ora 如果使用了SQLNET.AUTHENTICATION_SERVICES=(NTS)则说明可以使用OS认证就,只要conn / ...
- 在Java中,将ExecutorService转为守护程序
问题描述 我正在Java 1.6中使用一个ExecutoreService,简单地开始 ExecutorService pool = Executors.newFixedThreadPool(THRE ...
- Luogu 3793 由乃救爷爷
\(\verb|Luogu 3793 由乃救爷爷|\) rmq,数据随机 \(n,\ m\leq 2\times10^7\) lxl ST表 分块,大小设为 \(x\) 预处理每个块两端到块内每个点的 ...
- Java多线程(十)——线程优先级和守护线程
一.线程优先级的介绍 java 中的线程优先级的范围是1-10,默认的优先级是5.“高优先级线程”会优先于“低优先级线程”执行. java 中有两种线程:用户线程和守护线程.可以通过isDaemon( ...