mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2026-03-10 11:54:43 +00:00
代码重构 【Github Actions】
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
>
|
||||
>
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
>
|
||||
> 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。
|
||||
>
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
#### [136. 只出现一次的数字](https://leetcode-cn.com/problems/single-number/)
|
||||
|
||||
@@ -90,26 +88,26 @@ public:
|
||||
JS Code:
|
||||
|
||||
```javascript
|
||||
var singleNumber = function(nums) {
|
||||
//特殊情况
|
||||
if (nums.length === 1) {
|
||||
return nums[0];
|
||||
var singleNumber = function (nums) {
|
||||
//特殊情况
|
||||
if (nums.length === 1) {
|
||||
return nums[0];
|
||||
}
|
||||
//HashMap
|
||||
map = {};
|
||||
//将其存入哈希表中,含义为,若该元素不存在则存入表中,并计数为1,若已经存在获取次数并加1
|
||||
for (let x of nums) {
|
||||
if (!(x in map)) {
|
||||
map[x] = 0;
|
||||
}
|
||||
//HashMap
|
||||
map = {};
|
||||
//将其存入哈希表中,含义为,若该元素不存在则存入表中,并计数为1,若已经存在获取次数并加1
|
||||
for (let x of nums) {
|
||||
if (!(x in map)) {
|
||||
map[x] = 0;
|
||||
}
|
||||
map[x] += 1;
|
||||
}
|
||||
//遍历出出现次数为1的情况
|
||||
for (let key in map) {
|
||||
if (map[key] === 1) {
|
||||
return key;
|
||||
}
|
||||
map[x] += 1;
|
||||
}
|
||||
//遍历出出现次数为1的情况
|
||||
for (let key in map) {
|
||||
if (map[key] === 1) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
@@ -133,8 +131,6 @@ class Solution:
|
||||
return y
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 排序搜索法
|
||||
|
||||
#### 解析
|
||||
@@ -187,15 +183,15 @@ public:
|
||||
JS Code:
|
||||
|
||||
```javascript
|
||||
var singleNumber = function(nums) {
|
||||
// 排序
|
||||
nums.sort();
|
||||
for (let i = 1; i < nums.length - 1; i += 2) {
|
||||
if (nums[i] != nums[i - 1]) {
|
||||
return nums[i - 1];
|
||||
}
|
||||
var singleNumber = function (nums) {
|
||||
// 排序
|
||||
nums.sort();
|
||||
for (let i = 1; i < nums.length - 1; i += 2) {
|
||||
if (nums[i] != nums[i - 1]) {
|
||||
return nums[i - 1];
|
||||
}
|
||||
return nums[nums.length - 1];
|
||||
}
|
||||
return nums[nums.length - 1];
|
||||
};
|
||||
```
|
||||
|
||||
@@ -212,8 +208,6 @@ class Solution:
|
||||
return nums[len(nums) - 1]
|
||||
```
|
||||
|
||||
|
||||
|
||||
### HashSet
|
||||
|
||||
#### 解析
|
||||
@@ -272,19 +266,19 @@ public:
|
||||
JS Code:
|
||||
|
||||
```javascript
|
||||
var singleNumber = function(nums) {
|
||||
let set = new Set();
|
||||
//循环遍历
|
||||
for (let x of nums) {
|
||||
//已经存在,则去除
|
||||
if (set.has(x)) {
|
||||
set.delete(x);
|
||||
//否则存入
|
||||
} else {
|
||||
set.add(x);
|
||||
}
|
||||
var singleNumber = function (nums) {
|
||||
let set = new Set();
|
||||
//循环遍历
|
||||
for (let x of nums) {
|
||||
//已经存在,则去除
|
||||
if (set.has(x)) {
|
||||
set.delete(x);
|
||||
//否则存入
|
||||
} else {
|
||||
set.add(x);
|
||||
}
|
||||
return set.values().next().value;
|
||||
}
|
||||
return set.values().next().value;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -306,8 +300,6 @@ class Solution:
|
||||
return set_.pop()
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 栈
|
||||
|
||||
#### 解析
|
||||
@@ -335,7 +327,7 @@ class Solution {
|
||||
break;
|
||||
}
|
||||
//相同时出栈
|
||||
stack.pop();
|
||||
stack.pop();
|
||||
}
|
||||
return stack.peek();
|
||||
}
|
||||
@@ -370,22 +362,22 @@ public:
|
||||
JS Code:
|
||||
|
||||
```javascript
|
||||
var singleNumber = function(nums) {
|
||||
nums.sort();
|
||||
let stack = [];
|
||||
for (let x of nums) {
|
||||
if (!stack.length) {
|
||||
stack.push(x);
|
||||
continue;
|
||||
}
|
||||
//不同时直接跳出
|
||||
if (stack[0] !== x) {
|
||||
break;
|
||||
}
|
||||
//相同时出栈
|
||||
stack.pop();
|
||||
var singleNumber = function (nums) {
|
||||
nums.sort();
|
||||
let stack = [];
|
||||
for (let x of nums) {
|
||||
if (!stack.length) {
|
||||
stack.push(x);
|
||||
continue;
|
||||
}
|
||||
return stack[0];
|
||||
//不同时直接跳出
|
||||
if (stack[0] !== x) {
|
||||
break;
|
||||
}
|
||||
//相同时出栈
|
||||
stack.pop();
|
||||
}
|
||||
return stack[0];
|
||||
};
|
||||
```
|
||||
|
||||
@@ -412,17 +404,15 @@ class Solution:
|
||||
return stack[-1]
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 求和法
|
||||
|
||||
#### 解析
|
||||
|
||||
这个方法也比较简单,也是借助咱们的 HashSet ,具体思路如下,我们通过 HashSet 保存数组内的元素,然后进行求和(setsum),那么得到的这个和则为去除掉重复元素的和,我们也可以得到所有元素和(numsum)。因为我们其他元素都出现两次,仅有一个元素出现一次,那我们通过 setsum * 2 - numsum 得到的元素则为出现一次的数。
|
||||
这个方法也比较简单,也是借助咱们的 HashSet ,具体思路如下,我们通过 HashSet 保存数组内的元素,然后进行求和(setsum),那么得到的这个和则为去除掉重复元素的和,我们也可以得到所有元素和(numsum)。因为我们其他元素都出现两次,仅有一个元素出现一次,那我们通过 setsum \* 2 - numsum 得到的元素则为出现一次的数。
|
||||
|
||||

|
||||
|
||||
上面我们的 SetSum * 2 - NumSum = z 也就是我们所求的值, 是不是感觉很简单呀。
|
||||
上面我们的 SetSum \* 2 - NumSum = z 也就是我们所求的值, 是不是感觉很简单呀。
|
||||
|
||||
#### 题目代码
|
||||
|
||||
@@ -436,13 +426,13 @@ class Solution {
|
||||
int numsum = 0;
|
||||
for (int x : nums) {
|
||||
//所有元素的和
|
||||
numsum += x;
|
||||
numsum += x;
|
||||
if (!set.contains(x)) {
|
||||
//HashSet内元素的和
|
||||
setsum += x;
|
||||
setsum += x;
|
||||
}
|
||||
set.add(x);
|
||||
}
|
||||
}
|
||||
//返回值
|
||||
return setsum * 2 - numsum;
|
||||
}
|
||||
@@ -460,13 +450,13 @@ public:
|
||||
int numsum = 0;
|
||||
for (int x : nums) {
|
||||
//所有元素的和
|
||||
numsum += x;
|
||||
numsum += x;
|
||||
if (set.find(x) == set.end()) {
|
||||
//HashSet内元素的和
|
||||
setsum += x;
|
||||
setsum += x;
|
||||
}
|
||||
set.insert(x);
|
||||
}
|
||||
}
|
||||
//返回值
|
||||
return setsum * 2 - numsum;
|
||||
}
|
||||
@@ -476,21 +466,21 @@ public:
|
||||
JS Code:
|
||||
|
||||
```javascript
|
||||
var singleNumber = function(nums) {
|
||||
let set = new Set();
|
||||
let setsum = 0;
|
||||
let numsum = 0;
|
||||
for (let x of nums) {
|
||||
//所有元素的和
|
||||
numsum += x;
|
||||
if (!set.has(x)) {
|
||||
setsum += x;
|
||||
}
|
||||
//HashSet内元素的和
|
||||
set.add(x);
|
||||
var singleNumber = function (nums) {
|
||||
let set = new Set();
|
||||
let setsum = 0;
|
||||
let numsum = 0;
|
||||
for (let x of nums) {
|
||||
//所有元素的和
|
||||
numsum += x;
|
||||
if (!set.has(x)) {
|
||||
setsum += x;
|
||||
}
|
||||
//返回值
|
||||
return 2 * setsum - numsum;
|
||||
//HashSet内元素的和
|
||||
set.add(x);
|
||||
}
|
||||
//返回值
|
||||
return 2 * setsum - numsum;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -502,18 +492,16 @@ class Solution:
|
||||
return 2 * sum(set(nums)) - sum(nums)
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 位运算
|
||||
|
||||
#### 解析
|
||||
|
||||
这个方法主要是借助咱们的位运算符 ^ 按位异或,我们先来了解一下这个位运算符。
|
||||
|
||||
> 按位异或(XOR)运算符“^”是双目运算符。 其功能是参与运算的两数各对应的二进位相异或,当两对应的二进位相异时,结果为1。相同时为0。
|
||||
> 按位异或(XOR)运算符“^”是双目运算符。 其功能是参与运算的两数各对应的二进位相异或,当两对应的二进位相异时,结果为 1。相同时为 0。
|
||||
|
||||
> 任何数和0异或,仍为本身:a⊕0 = a
|
||||
> 任何数和本身异或,为0:a⊕a = 0
|
||||
> 任何数和 0 异或,仍为本身:a⊕0 = a
|
||||
> 任何数和本身异或,为 0:a⊕a = 0
|
||||
> 异或运算满足交换律和结合律:a⊕b⊕a = (a⊕a)⊕b = 0⊕b = b
|
||||
|
||||
例:
|
||||
@@ -533,7 +521,7 @@ class Solution {
|
||||
public int singleNumber(int[] nums) {
|
||||
int num = 0;
|
||||
//异或
|
||||
for (int x : nums) {
|
||||
for (int x : nums) {
|
||||
num ^= x;
|
||||
}
|
||||
return num;
|
||||
@@ -560,9 +548,9 @@ public:
|
||||
JS Code:
|
||||
|
||||
```javascript
|
||||
var singleNumber = function(nums) {
|
||||
//异或
|
||||
return nums.reduce((num, x) => num ^= x);
|
||||
var singleNumber = function (nums) {
|
||||
//异或
|
||||
return nums.reduce((num, x) => (num ^= x));
|
||||
};
|
||||
```
|
||||
|
||||
@@ -577,5 +565,4 @@ class Solution:
|
||||
return reduce(lambda num, x: num ^ x, nums, 0)
|
||||
```
|
||||
|
||||
本题一共介绍了6种解题方法,肯定还有别的方法,欢迎大家讨论。大家可以在做题的时候一题多解。这样能大大提高自己解题能力。下面我们来看一下这些方法如何应用到其他题目上。
|
||||
|
||||
本题一共介绍了 6 种解题方法,肯定还有别的方法,欢迎大家讨论。大家可以在做题的时候一题多解。这样能大大提高自己解题能力。下面我们来看一下这些方法如何应用到其他题目上。
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
>
|
||||
> 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。
|
||||
>
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
#### [137. 只出现一次的数字 II](https://leetcode-cn.com/problems/single-number-ii/)
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
|
||||
1.通过遍历数组获取所有元素的和以及 HashSet 内元素的和。
|
||||
|
||||
2.(SumSet * 3 - SumNum)/ 2 即可,除以 2 是因为我们减去之后得到的是 2 倍的目标元素。
|
||||
2.(SumSet \* 3 - SumNum)/ 2 即可,除以 2 是因为我们减去之后得到的是 2 倍的目标元素。
|
||||
|
||||
注:这个题目中需要注意溢出的情况 。
|
||||
|
||||
@@ -41,7 +41,7 @@ class Solution {
|
||||
public int singleNumber(int[] nums) {
|
||||
HashSet<Integer> set = new HashSet<>();
|
||||
long setsum = 0;
|
||||
long numsum = 0;
|
||||
long numsum = 0;
|
||||
for (int x : nums) {
|
||||
//所有元素的和
|
||||
numsum += x;
|
||||
@@ -68,13 +68,13 @@ public:
|
||||
long numsum = 0;
|
||||
for (int x : nums) {
|
||||
//所有元素的和
|
||||
numsum += x;
|
||||
numsum += x;
|
||||
if (set.find(x) == set.end()) {
|
||||
//HashSet内元素的和
|
||||
setsum += x;
|
||||
setsum += x;
|
||||
}
|
||||
set.insert(x);
|
||||
}
|
||||
}
|
||||
//返回值
|
||||
return (3 * setsum - numsum) / 2;
|
||||
}
|
||||
@@ -84,21 +84,21 @@ public:
|
||||
JS Code:
|
||||
|
||||
```javascript
|
||||
var singleNumber = function(nums) {
|
||||
let set = new Set();
|
||||
let setsum = 0;
|
||||
let numsum = 0;
|
||||
for (let x of nums) {
|
||||
//所有元素的和
|
||||
numsum += x;
|
||||
if (!set.has(x)) {
|
||||
setsum += x;
|
||||
}
|
||||
//HashSet内元素的和
|
||||
set.add(x);
|
||||
var singleNumber = function (nums) {
|
||||
let set = new Set();
|
||||
let setsum = 0;
|
||||
let numsum = 0;
|
||||
for (let x of nums) {
|
||||
//所有元素的和
|
||||
numsum += x;
|
||||
if (!set.has(x)) {
|
||||
setsum += x;
|
||||
}
|
||||
//返回值
|
||||
return (3 * setsum - numsum) / 2;
|
||||
//HashSet内元素的和
|
||||
set.add(x);
|
||||
}
|
||||
//返回值
|
||||
return (3 * setsum - numsum) / 2;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -176,22 +176,22 @@ public:
|
||||
JS Code:
|
||||
|
||||
```javascript
|
||||
var singleNumber = function(nums) {
|
||||
var singleNumber = function (nums) {
|
||||
let res = 0;
|
||||
for(let i = 0; i < 32; i++){
|
||||
let count = 0;
|
||||
for (let num of nums) {
|
||||
//检查第 i 位是否为 1
|
||||
if ((num >> i & 1) == 1) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count % 3 != 0) {
|
||||
// 将第 i 位设为 1
|
||||
res = res | 1 << i;
|
||||
for (let i = 0; i < 32; i++) {
|
||||
let count = 0;
|
||||
for (let num of nums) {
|
||||
//检查第 i 位是否为 1
|
||||
if (((num >> i) & 1) == 1) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count % 3 != 0) {
|
||||
// 将第 i 位设为 1
|
||||
res = res | (1 << i);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
return res;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -216,32 +216,26 @@ class Solution:
|
||||
return res
|
||||
```
|
||||
|
||||
|
||||
|
||||
我们来解析一下我们的代码:
|
||||
|
||||
> **<<** 左移运算符:运算数的各二进位全部左移若干位,由 **<<** 右边的数字指定了移动的位数,高位丢弃,低位补0。
|
||||
> **<<** 左移运算符:运算数的各二进位全部左移若干位,由 **<<** 右边的数字指定了移动的位数,高位丢弃,低位补 0。
|
||||
>
|
||||
> **>>** 右移运算符:**>> ** 左边的运算数的各二进位全部右移若干位,**>>** 右边的数字指定了移动的位数。
|
||||
|
||||
另外我们的代码中还包含了 a & 1 和 a | 1 这有什么作用呢?继续看下图。
|
||||
|
||||
> **&** 按位与运算符:参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0。
|
||||
> **>>** 右移运算符:**>> ** 左边的运算数的各二进位全部右移若干位,**>>** 右边的数字指定了移动的位数。
|
||||
|
||||
另外我们的代码中还包含了 a & 1 和 a | 1 这有什么作用呢?继续看下图。
|
||||
|
||||
> **&** 按位与运算符:参与运算的两个值,如果两个相应位都为 1,则该位的结果为 1,否则为 0。
|
||||
|
||||

|
||||
|
||||
因为我们 a & 1 中 1 只有最后一位为 1,其余位皆为 0 ,所以我们发现 **a & 1 的作用就是判断 a 的最后一位是否为 1** ,如果 a 的最后一位为 1 ,a & 1 = 1,否则为 0 。所以我们还可以通过这个公式来判断 a 的奇偶性。
|
||||
|
||||
> **|** 按位或运算符:只要对应的二个二进位有一个为1时,结果位就为1。
|
||||
> **|** 按位或运算符:只要对应的二个二进位有一个为 1 时,结果位就为 1。
|
||||
|
||||

|
||||
|
||||
这个公式的作用就是**将我们移位后的 res 的最后一位 0 变为 1**。这个 1 也就代表着我们只出现一次元素的某一位。
|
||||
|
||||
|
||||
|
||||
> 贡献者[@jaredliw](https://github.com/jaredliw)注:
|
||||
>
|
||||
> 这里我想解释一下 python 里的这两行:
|
||||
@@ -251,9 +245,9 @@ class Solution:
|
||||
> res = ~(res ^ 4294967295)
|
||||
> ```
|
||||
>
|
||||
> int 的的符号是由最高位(这题用的是32位)的值决定,1 就是负数,0就是正数。由于 python 的 int 类型理论上是无限大的,这题里的 res 都会被认定为是正数。举个例子,32位的 -4 是这样的:
|
||||
> int 的的符号是由最高位(这题用的是 32 位)的值决定,1 就是负数,0 就是正数。由于 python 的 int 类型理论上是无限大的,这题里的 res 都会被认定为是正数。举个例子,32 位的 -4 是这样的:
|
||||
>
|
||||
> > 11111111111111111111111111111100 (最高位是 1 )= -4
|
||||
> > 11111111111111111111111111111100 (最高位是 1 )= -4
|
||||
>
|
||||
> python 里的则是这样的:
|
||||
>
|
||||
@@ -261,9 +255,9 @@ class Solution:
|
||||
>
|
||||
> 怎么办呢?
|
||||
>
|
||||
> 我们可以先将 res 的后32位取反(与 4294967295 异或,4294967295 的二进制是 32 个 1),得到:
|
||||
> 我们可以先将 res 的后 32 位取反(与 4294967295 异或,4294967295 的二进制是 32 个 1),得到:
|
||||
>
|
||||
> > ...000000000000 00000000000000000000000000000011(最高位是0)= 3
|
||||
> > ...000000000000 00000000000000000000000000000011(最高位是 0)= 3
|
||||
>
|
||||
> 之后再用波浪号按位取反,得到:
|
||||
>
|
||||
@@ -272,4 +266,3 @@ class Solution:
|
||||
> 大家可以自行验证看看:`(res >> n & 1) == 1` ,n 随便填个大于 31 的数字,之前是 false,之后就变成 true (代表第 33 位,第 34 位,……都转成 1 了)。
|
||||
>
|
||||
> 虽然说这种方法有一种脱裤子放屁的感觉 ,而且`res -= 2 ** 32` 也能办到,但由于涉及到 int 存储的问题(我省略了许多,大家自行度娘哈),我觉得还是有必要知道的。
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
> 如果阅读时,发现错误,或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
|
||||
>
|
||||
> 感谢支持,该仓库会一直维护,希望对各位有一丢丢帮助。
|
||||
>
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
> 另外希望手机阅读的同学可以来我的 <u>[**公众号:袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
|
||||
|
||||
#### [260. 只出现一次的数字 III](https://leetcode-cn.com/problems/single-number-iii/)
|
||||
|
||||
@@ -42,7 +42,7 @@ class Solution {
|
||||
int[] arr = new int[2];
|
||||
int i = 0;
|
||||
for (int y : set) {
|
||||
arr[i++] = y;
|
||||
arr[i++] = y;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
@@ -78,23 +78,23 @@ public:
|
||||
JS Code:
|
||||
|
||||
```javascript
|
||||
var singleNumber = function(nums) {
|
||||
let set = new Set();
|
||||
for (let x of nums) {
|
||||
//存在的则移除
|
||||
if (set.has(x)) {
|
||||
set.delete(x);
|
||||
//不存在则存入
|
||||
} else {
|
||||
set.add(x);
|
||||
}
|
||||
var singleNumber = function (nums) {
|
||||
let set = new Set();
|
||||
for (let x of nums) {
|
||||
//存在的则移除
|
||||
if (set.has(x)) {
|
||||
set.delete(x);
|
||||
//不存在则存入
|
||||
} else {
|
||||
set.add(x);
|
||||
}
|
||||
//存到数组里,然后返回
|
||||
let arr = [];
|
||||
for (let y of set) {
|
||||
arr.push(y);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
//存到数组里,然后返回
|
||||
let arr = [];
|
||||
for (let y of set) {
|
||||
arr.push(y);
|
||||
}
|
||||
return arr;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -118,8 +118,6 @@ class Solution:
|
||||
return arr
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 位运算
|
||||
|
||||
#### 解析
|
||||
@@ -132,13 +130,13 @@ class Solution:
|
||||
>
|
||||
> 分组后:
|
||||
>
|
||||
> A组:a, a, b, b, c 异或得到 c
|
||||
> A 组:a, a, b, b, c 异或得到 c
|
||||
>
|
||||
> B组:e, e, f, f, d 异或得到 d
|
||||
> B 组:e, e, f, f, d 异或得到 d
|
||||
|
||||
原理懂了,那么我们应该依据什么规则对其进行分类呢?
|
||||
|
||||
c,d 两个不同的数,那么二进制上必定有一位是不同的,那么我们就可以根据这一位(分组位)来将 c,d 分到两个组中,数组中的其他元素,要么在 A 组中,要么在 B 组中。
|
||||
c,d 两个不同的数,那么二进制上必定有一位是不同的,那么我们就可以根据这一位(分组位)来将 c,d 分到两个组中,数组中的其他元素,要么在 A 组中,要么在 B 组中。
|
||||
|
||||
我们应该怎么得到分组位呢?
|
||||
|
||||
@@ -148,7 +146,7 @@ c,d 两个不同的数,那么二进制上必定有一位是不同的,那
|
||||
|
||||
那么我们应该怎么借助分组位进行分组呢?
|
||||
|
||||
我们处理 c , d 的异或值,可以仅保留异或值的分组位,其余位变为 0 ,例如 101 变成 001或 100。
|
||||
我们处理 c , d 的异或值,可以仅保留异或值的分组位,其余位变为 0 ,例如 101 变成 001 或 100。
|
||||
|
||||
为什么要这么做呢?在第二题提到,我们可以根据 a & 1 来判断 a 的最后一位为 0 还是为 1,所以我们将 101 变成 001 之后,然后数组内的元素 x & 001 即可对 x 进行分组 。同样也可以 x & 100 进行分组。
|
||||
|
||||
@@ -176,7 +174,7 @@ class Solution {
|
||||
//保留最右边的一个 1
|
||||
long group = temp & (-temp);
|
||||
int[] arr = new int[2];
|
||||
for (int y : nums) {
|
||||
for (int y : nums) {
|
||||
//分组位为 0 的组,组内异或
|
||||
if ((group & y) == 0) {
|
||||
arr[0] ^= y;
|
||||
@@ -205,7 +203,7 @@ public:
|
||||
//保留最右边的一个 1
|
||||
int group = temp & (-temp);
|
||||
vector<int> arr(2, 0);
|
||||
for (int y : nums) {
|
||||
for (int y : nums) {
|
||||
//分组位为 0 的组,组内异或
|
||||
if ((group & y) == 0) {
|
||||
arr[0] ^= y;
|
||||
@@ -222,25 +220,25 @@ public:
|
||||
JS Code:
|
||||
|
||||
```javascript
|
||||
var singleNumber = function(nums) {
|
||||
let temp = 0;
|
||||
//求出异或值
|
||||
for (let x of nums) {
|
||||
temp ^= x;
|
||||
var singleNumber = function (nums) {
|
||||
let temp = 0;
|
||||
//求出异或值
|
||||
for (let x of nums) {
|
||||
temp ^= x;
|
||||
}
|
||||
//保留最右边的一个 1
|
||||
let group = temp & -temp;
|
||||
let arr = [0, 0];
|
||||
for (let y of nums) {
|
||||
//分组位为 0 的组,组内异或
|
||||
if ((group & y) == 0) {
|
||||
arr[0] ^= y;
|
||||
//分组位为 1 的组,组内异或
|
||||
} else {
|
||||
arr[1] ^= y;
|
||||
}
|
||||
//保留最右边的一个 1
|
||||
let group = temp & (-temp);
|
||||
let arr = [0, 0];
|
||||
for (let y of nums) {
|
||||
//分组位为 0 的组,组内异或
|
||||
if ((group & y) == 0) {
|
||||
arr[0] ^= y;
|
||||
//分组位为 1 的组,组内异或
|
||||
} else {
|
||||
arr[1] ^= y;
|
||||
}
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
return arr;
|
||||
};
|
||||
```
|
||||
|
||||
@@ -265,4 +263,3 @@ class Solution:
|
||||
arr[1] ^= y
|
||||
return arr
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user