当前位置:网站首页>Leetcode/ numbers that appear only once
Leetcode/ numbers that appear only once
2022-07-26 01:43:00 【xcrj】
package com.xcrj;
import java.util.HashMap;
import java.util.Map;
/** * The finger of the sword Offer II 004. A number that appears only once * Give you an array of integers nums , Except that one element only appears once Outside , Every other element just happens to appear Three times . Please find and return the element that only appears once . * Investigate : * Hash table statistics * Or operation splicing result |= (1 << i); */
public class Solution4 {
/** * Ordinary brute force method Traverse */
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;
}
/** * Hash table */
public int singleNumber2(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for (int num : nums) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
// findFirst There may be many that only appear 1 The number of times
return map.entrySet().stream().filter(entry -> entry.getValue() == 1).findFirst().get().getKey();
}
/** * In turn, the calculation appears 1 Every binary bit of the secondary element , Then use or operation to splice out 1 Sub elements */
public int singleNumber3(int[] nums) {
int result = 0;
// 32 because int yes 32 Bit
for (int i = 0; i < 32; ++i) {
int total = 0;
/** * Find the number of all elements in the array i Sum of bits total * Because only 3 Time or 1 Secondary elements * therefore total=3*m*1+3*n*0+0/1,m The first i Position as 1 Appearance 3 Number of secondary elements ,n The first i Position as 0 Appearance 3 Number of secondary elements , appear 1 Second element i Is it 0 perhaps 1 * So there comes 1 Second element i position =total%3 */
for (int num : nums) {
total += ((num >> i) & 1);
}
// It is known that appear 1 Every bit of the secondary element Splice through or operation to appear 1 Secondary elements .0 After moving left, it's still 0 No or operation splicing
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}));
}
}
边栏推荐
猜你喜欢

Network performance evaluation tool ping/mtr

SVN版本控制分支、合并功能使用

Leetcode537. Complex multiplication (yes, solved)

Network layer 2 and layer 3 forwarding

Prime Ring Problem

AUTOCAD——计算面积的方法

The detailed knowledge summary of MySQL can be collected

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

What is a test case? How to design?

Special topic of distributed micro service e-commerce (I) - Project Introduction
随机推荐
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2 personal problem sets
PTGui Pro12垂直线纠正
Understand Linglong platform unified access service from simple to deep Monet
Dijkstra find the shortest path
leetcode/只出现一次的数字
4QAM, 16QAM modulation and demodulation simulation circuit, observe and analyze QAM constellation and bit error rate curve [matlab code]
Is it safe to buy funds on e fund? Professional answers
“蔚来杯“2022牛客暑期多校训练营2 K.[Link with Bracket Sequence I] 括号序列 DP
npm ERR! code ETIMEDOUTnpm ERR! syscall connectnpm ERR! errno ETIMEDOUTnpm ERR! network request t
Go operation excel library excel use
"Weilai Cup" 2022 Niuke summer multi school training camp 2 h.[take the elevator] maintenance section
Y77. Chapter IV Prometheus' monitoring system and practice -- Prometheus' service discovery mechanism (VIII)
The sales volume has won the championship repeatedly. Is the secret of Wuling's success only low price?
Special topic of distributed micro service e-commerce (I) - Project Introduction
Mysql_ Note1
pt-onnx-ncnn转换的问题记录(接yolov5训练)
Silicon Valley classroom - official account cloud on demand Silicon Valley classroom microservice project practical notes
Ideal Path(UVA - 1599)
推荐系统-协同过滤在Spark中的实现
Spark-SQL中根据年月日显示周几用date_format(date,‘u‘)