当前位置:网站首页>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}));
}
}
边栏推荐
- Mysql_ Note2
- Huawei wireless device WDS configuration command
- poj1521
- Leetcode537. Complex multiplication (yes, solved)
- 学习笔记:原码, 反码, 补码
- 机器学习:贝叶斯网络
- Is it safe to buy funds on e fund? Professional answers
- [go] III. The simplest restful API server
- Go operation excel library excel use
- Tutorial on principles and applications of database system (053) -- MySQL query (XV): usage of character functions
猜你喜欢

C语言中的整型数据类型(你真的了解吗)

Record a failure caused by a custom redis distributed lock

《分布式微服务电商》专题(一)-项目简介

Big view +500 cases, software teams should improve R & D efficiency in this way

聚势|海泰方圆亮相第五届数字中国建设峰会

CPU的三种模式

8. Learn Mysql to create data tables

Leetcode 537. complex multiplication (netizens' thoughts, ashamed)

Recommend a super good UI automation tool: uiautomator2!

两阶段提交和三阶段提交
随机推荐
PTGui Pro12垂直线纠正
[ickim 2022] the Fourth International Conference on knowledge and information management
Shell summary (1)
Tutorial on principles and applications of database system (055) -- MySQL query (XVII): usage of mathematical functions
4QAM、16QAM 调制与解调仿真电路,观察并分析QAM星座图和误码率曲线【matlab代码】
Zombie's treasure test (enumeration)
01. MySQL transaction isolation level and concurrent database access
Cross-lingual Transfer of Correlations between Parts of Speech and Gaze Features 阅读笔记
旅行 (拆点分层)
如何获取广告服务流量变现数据,助力广告效果分析?
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2 personal problem sets
Codisvsrediscluster: which cluster scheme should I choose?
机器学习:贝叶斯网络
图像批处理高斯滤波降噪+峰值信噪比计算
当博客被黑客攻击时该怎么办?
Leetcode 537. complex multiplication (netizens' thoughts, ashamed)
Dijkstra find the shortest path
【数据挖掘】生成模型和判别模型的区别及优缺点
Detailed explanation of redis data structure, combined with books
Travel (split points and layers)