当前位置:网站首页>leetcode/只出现一次的数字
leetcode/只出现一次的数字
2022-07-26 01:38:00 【xcrj】
package com.xcrj;
import java.util.HashMap;
import java.util.Map;
/** * 剑指 Offer II 004. 只出现一次的数字 * 给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。 * 考察: * 散列表统计 * 或运算拼接 result |= (1 << i); */
public class Solution4 {
/** * 普通蛮力法 遍历 */
public int singleNumber(int[] nums) {
int count = 0;
int result = 0;
for (int i = 0; i < nums.length; i++) {
result = nums[i];
for (int j = 0; j < nums.length; j++) {
if (result == nums[j]) {
count++;
}
if (count > 1) {
break;
}
}
if (count == 1) {
return result;
}
count = 0;
}
return result;
}
/** * 散列表 */
public int singleNumber2(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
// findFirst可能有多个只出现1次的数
return map.entrySet().stream().filter(entry -> entry.getValue() == 1).findFirst().get().getKey();
}
/** * 依次计算出现1次元素的每一个二进制位,再使用或运算拼接出出现1次元素 */
public int singleNumber3(int[] nums) {
int result = 0;
// 32因为int是32位的
for (int i = 0; i < 32; ++i) {
int total = 0;
/** * 求数组中所有元素第i个二进制位的和total * 因为数组中只有出现3次或1次的元素 * 因此total=3*m*1+3*n*0+0/1,m第i位为1的出现3次的元素的个数,n第i位为0的出现3次的元素的个数,出现1次的元素的第i位是0或者1 * 因此出现1次的元素的第i位=total%3 */
for (int num : nums) {
total += ((num >> i) & 1);
}
// 已知 出现1次元素的每一位 通过或运算拼接出出现1次的元素。0左移之后还是0不用或运算拼接
if (total % 3 == 1) {
result |= (1 << i);
}
}
return result;
}
public static void main(String[] args) {
Solution4 solution4 = new Solution4();
System.out.println(solution4.singleNumber3(new int[]{
1, 2, 1, 1}));
}
}
边栏推荐
- Case when of SQL
- 旅行 (拆点分层)
- 机器学习:贝叶斯网络
- Ideal Path(UVA - 1599)
- Should we test the Dao layer?
- C语言进阶(一)动态分配内存
- 4QAM, 16QAM modulation and demodulation simulation circuit, observe and analyze QAM constellation and bit error rate curve [matlab code]
- Mysql_ Note1
- Server available resources query script
- Easyrecovery15 data recovery software with high recovery rate and high download volume
猜你喜欢

Special topic of distributed micro service e-commerce (I) - Project Introduction

网络之IP地址

MulDA: A Multilingual Data Augmentation Framework for Low-Resource Cross-Lingual NER 阅读笔记

U++学习笔记 UStruct、UEnum声明以及函数库简单函数实现
![[ickim 2022] the Fourth International Conference on knowledge and information management](/img/e0/1d7aebc6a6fe42c4f4ddd47a2045ec.jpg)
[ickim 2022] the Fourth International Conference on knowledge and information management

U++ learning notes ustruct, uenum declaration and function library simple function implementation

Fastjason handles generics

Handler消息机制-FWK层

机器学习:贝叶斯网络

谷歌浏览器调试工具使用基础版(一)
随机推荐
Causes of signal degradation in optical fiber communication
Google gson usage details
Nodejs builds cloud native microservice applications based on dapr, a quick start guide from 0 to 1
Arthas watch command to view the properties of objects in the array
言语理解中心理解总结
Big view +500 cases, software teams should improve R & D efficiency in this way
Tutorial on principles and applications of database system (054) -- MySQL query (16): usage of date time function
Y77. Chapter IV Prometheus' monitoring system and practice -- Prometheus' service discovery mechanism (VIII)
Linked list related interview questions
电视机软件烧录
谷歌浏览器调试工具使用基础版(一)
Speech comprehension center comprehension summary
Stack Title: basic calculator
Network performance evaluation tool ping/mtr
How to obtain the cash flow data of advertising services to help analyze the advertising effect?
【ICKIM 2022】第四届知识与信息管理国际会议
Advanced C language (I) dynamic memory allocation
【Go】如何控制协程的最大并发数
Dijkstra 求最短路
Kubernetes pod start process