当前位置:网站首页>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}));
}
}
边栏推荐
- Zero copy of network file transfer
- Google gson usage details
- Big view +500 cases, software teams should improve R & D efficiency in this way
- 服务器可用资源查询脚本
- Cross-lingual Transfer of Correlations between Parts of Speech and Gaze Features 阅读笔记
- Analysis of zeromq
- Is it safe to buy funds in stock accounts? Professional answers
- 旅行 (拆点分层)
- Shell exercises
- U++ learning notes ustruct, uenum declaration and function library simple function implementation
猜你喜欢

Linked list related interview questions

3、 Pinda general permission system__ pd-tools-swagger2

如何获取广告服务流量变现数据,助力广告效果分析?

SOC first project hello_ world
![[combinational logic circuit] - encoder](/img/a5/c92e0404c6a970a62595bc7a3b68cd.gif)
[combinational logic circuit] - encoder
![[Go]三、最简单的RestFul API服务器](/img/1f/f6fc8cc9a3891d01a25e709170188d.png)
[Go]三、最简单的RestFul API服务器

MDK编译过程及ARM编译工具链

Codeforces Round #810 (Div. 2)A~C

大咖观点+500强案例,软件团队应该这样提升研发效能

Typora expiration solution, what if typora can't open
随机推荐
服务器可用资源查询脚本
4QAM、16QAM 调制与解调仿真电路,观察并分析QAM星座图和误码率曲线【matlab代码】
QTreeWidget虚线设置
[go] how to control the maximum number of concurrent processes
Format JS code and debug JS code
Special topic of distributed micro service e-commerce (I) - Project Introduction
网络之二三层转发
“蔚来杯“2022牛客暑期多校训练营2 D.[Link with Game Glitch] 二分答案+SPFA判环
Huawei wireless device WDS configuration command
Linear relationship between vectors
Leetcode 537. 复数乘法(网友思路,自愧不如)
Cross linguistic transfer of correlations between parts of speech and Gazette Features Reading Notes
Travel (split points and layers)
Quickly create a topic folder
Prime Ring Problem
Record a failure caused by a custom redis distributed lock
Advanced C language (I) dynamic memory allocation
3059. Sculpture (jzoj)
Codisvsrediscluster: which cluster scheme should I choose?
元素和小于等于阈值的正方形的最大边长(来源:力扣(LeetCode))