algorithm-base/animation-simulation/求次数问题/只出现一次的数.md

237 lines
8.6 KiB
Java
Raw Normal View History

2021-03-20 08:44:27 +00:00
> **[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>进入。
#### [136. ](https://leetcode-cn.com/problems/single-number/)
2021-03-19 07:35:25 +00:00
>
1:
> : [2,2,1]
> : 1
2:
> : [4,1,2,1,2]
> : 4
### HashMap
####
HashMap
![](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/哈希表解法.1kefww8xsig0.png)
####
```java
class Solution {
public int singleNumber(int[] nums) {
//特殊情况
if (nums.length == 1) {
return nums[0];
}
//HashMap
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
//将其存入哈希表中含义为若该元素不存在则存入表中并计数为1若已经存在获取次数并加1.
for (int x : nums) {
map.put(x , map.getOrDefault(x,0) + 1);
}
//遍历出出现次数为1的情况
for (int y : map.keySet()) {
if(map.get(y) == 1){
return y;
}
}
return 0;
}
}
```
###
####
![](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/排序.6sp72k3iaqw0.gif)
####
```java
class Solution {
public int singleNumber(int[] nums) {
if (nums.length == 1){
return nums[0];
}
//排序
Arrays.sort(nums);
for (int i = 1; i < nums.length-1; i+=2){
if (nums[i] == nums[i-1]){
continue;
}else{
return nums[i-1];
}
}
return nums[nums.length-1];
}
}
```
### HashSet
####
HashSet HashSet HashMap HashSet HashSet HashSet () HashSet HashSet
![HashSet](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/HashSet.4b6dcxwj07c0.gif)
####
```java
class Solution {
public int singleNumber(int[] nums) {
if (nums.length == 1){
return nums[0];
}
HashSet<Integer> set = new HashSet<>();
//循环遍历
for (int x : nums){
//已经存在,则去除
if(set.contains(x)){
set.remove(x);
}
//否则存入
else{
set.add(x);
}
}
//返回仅剩的一个元素
return set.iterator().next();
}
}
```
###
####
使
![](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/栈.6mzstgebww00.gif)
####
```java
class Solution {
public int singleNumber(int[] nums) {
if (nums.length == 1) {
return nums[0];
}
Arrays.sort(nums);
Stack<Integer> stack = new Stack<>();
for (int x : nums){
if (stack.isEmpty()) {
stack.push(x);
continue;
}
//不同时直接跳出
if (stack.peek() != x) {
break;
}
//相同时出栈
stack.pop();
}
return stack.peek();
}
}
```
###
####
HashSet HashSet setsumnumsum setsum * 2 - numsum
![](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/求和解法.2tds49a3vzq0.png)
SetSum * 2 - NumSum = z
####
```java
class Solution {
public int singleNumber(int[] nums) {
if (nums.length == 1){
return nums[0];
}
HashSet<Integer> set = new HashSet<>();
int setsum = 0;
int numsum = 0;
for (int x : nums) {
//所有元素的和
numsum += x;
if (!set.contains(x)) {
//HashSet内元素的和
setsum += x;
}
set.add(x);
}
//返回值
return setsum * 2 - numsum;
}
}
```
###
####
^
> (XOR)^ 10
> 0a0 = a
> 0aa = 0
> aba = (aa)b = 0b = b
![](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/异或运算.1myeo11xgqo0.png)
1 0 0
![image-20201129120648802](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io.photo@master/photo/image-20201129120648802.2ajgng6zzd7o.png)
####
```java
class Solution {
public int singleNumber(int[] nums) {
int num = 0;
//异或
for (int x : nums) {
num ^= x;
}
return num;
}
}
```
6