当前位置:网站首页>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}));
}
}
边栏推荐
- Zombie's treasure test (enumeration)
- Tutorial on principles and applications of database system (055) -- MySQL query (XVII): usage of mathematical functions
- Browser development and use skills
- Is it safe to buy funds on e fund? Professional answers
- Ideal Path(UVA - 1599)
- 元素和小于等于阈值的正方形的最大边长(来源:力扣(LeetCode))
- Dot screen precautions
- MulDA: A Multilingual Data Augmentation Framework for Low-Resource Cross-Lingual NER 阅读笔记
- Special topic of distributed micro service e-commerce (I) - Project Introduction
- Fastjason handles generics
猜你喜欢

MDK compilation process and arm compilation tool chain

Analysis of zeromq

01. MySQL transaction isolation level and concurrent database access

Handler message mechanism - FWK layer

Zero copy of network file transfer

Pycharm automatically adds header comments when creating py files

图像批处理高斯滤波降噪+峰值信噪比计算

Understand Linglong platform unified access service from simple to deep Monet

言语理解-片段阅读的结构剖析练习

Test questions and answers of the latest Beijing Construction eight (materialman) mock examination in 2022
随机推荐
[untitled]
"Yuanqi Cola" is not the end point, "China Cola" is
Google Gson用法详解
【数据挖掘】生成模型和判别模型的区别及优缺点
Oracle - isupplier portal Invoicing error
[ickim 2022] the Fourth International Conference on knowledge and information management
聚势|海泰方圆亮相第五届数字中国建设峰会
Test questions and answers of the latest Beijing Construction eight (materialman) mock examination in 2022
3059. 雕塑(jzoj)
8. Learn Mysql to create data tables
What should I do when my blog is attacked by hackers?
Zombie‘s Treasure Chest(枚举)
Case when of SQL
poj1521
Handler message mechanism - FWK layer
Linked list related interview questions
快速创建题目文件夹
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2 personal problem sets
Spark-SQL中根据年月日显示周几用date_format(date,‘u‘)
Speech comprehension center comprehension summary