FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

《剑指offer(第2版)》 >>> 面试题56:数组中数字出现的次数 · apple006/java-code@4940491 · GitHub

Repository navigation

Commit 4940491

Browse files
committed
《剑指offer(第2版)》 >>> 面试题56:数组中数字出现的次数
1 parent c62740d commit 4940491

3 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.xdc.basic.algorithm.sword4offer.question56;
2+
3+
public class Solution
4+
{
5+
/**
6+
* 面试题56:数组中数字出现的次数 题目一:数组中只出现一次的两个数字。
7+
* num1,num2分别为长度为1的数组,将num1[0],num2[0]设置为返回结果。
8+
* 基本思想是将这个数组一分为二,而这个分法是有讲究的:必须将这两个不同的数字分到两个不同数组里,这可以根据这两个数中任意不同的位来划分。
9+
*/
10+
public static void FindNumsAppearOnce(int[] array, int num1[], int num2[])
11+
{
12+
if (array == null || array.length < 2 || num1 == null || num1.length < 1 || num2 == null || num2.length < 1)
13+
{
14+
throw new RuntimeException("Invalid input!");
15+
}
16+
17+
int num1XorNum2 = 0;
18+
for (int i = 0; i < array.length; i++)
19+
{
20+
num1XorNum2 ^= array[i];
21+
}
22+
23+
// 方法一:找到两个只出现一次的数字的第一个位值不同的位置。
24+
int first1bit = 1;
25+
while ((num1XorNum2 & first1bit) == 0)
26+
{
27+
first1bit = first1bit << 1;
28+
}
29+
30+
// 方法二:找到两个只出现一次的数字的第一个位值不同的位置。
31+
// int first1bit = (num1XorNum2 & (num1XorNum2 - 1)) ^ num1XorNum2;
32+
33+
num1[0] = 0;
34+
num2[0] = 0;
35+
for (int i = 0; i < array.length; i++)
36+
{
37+
if ((array[i] & first1bit) == 0)
38+
{
39+
num1[0] ^= array[i];
40+
}
41+
else
42+
{
43+
num2[0] ^= array[i];
44+
}
45+
}
46+
}
47+
48+
/**
49+
* 面试题56:数组中数字出现的次数 题目二:数组中唯一只出现一次的数字。
50+
* 思路:三个相同的数字异或之后还是本身。转换思路:如果一个数字出现三次,那么他的二进制表示的每一位(0或者1)也出现3次。如果把所有出现3次的数字的二进制表示的每一位都分别相加起来,那么每一位的和都能被3整除!!!
51+
*/
52+
public static int FindNumAppearOnce(int[] array)
53+
{
54+
if (array == null || array.length == 0)
55+
{
56+
throw new RuntimeException("Invalid input!");
57+
}
58+
59+
int[] bitSum = new int[32];
60+
for (int i = 0; i < array.length; i++)
61+
{
62+
for (int j = 0; j < bitSum.length; j++)
63+
{
64+
if ((array[i] & (1 << j)) != 0)
65+
{
66+
bitSum[j]++;
67+
}
68+
}
69+
}
70+
71+
int ans = 0;
72+
for (int i = 0; i < bitSum.length; i++)
73+
{
74+
if (bitSum[i] % 3 != 0)
75+
{
76+
ans = ans | (1 << i);
77+
}
78+
}
79+
80+
return ans;
81+
}
82+
83+
public static void main(String[] args)
84+
{
85+
int[] array = new int[] { 0, 0, 2, 1, 4, 8, 2, 1 };
86+
int num1[] = new int[1];
87+
int num2[] = new int[1];
88+
FindNumsAppearOnce(array, num1, num2);
89+
System.out.println(num1[0] + " " + num2[0]);
90+
91+
int[] array2 = new int[] { 1, 1, 1, 2, 3, 3, 3 };
92+
System.out.println(FindNumAppearOnce(array2));
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
摘自:《剑指offer(第2版)》 >>> 面试题56:数组中数字出现的次数 题目一:数组中只出现一次的两个数字
2+
摘自:https://www.nowcoder.com/practice/e02fdb54d7524710a7d664d082bb7811?tpId=13&tqId=11193&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
3+
4+
问题描述:
5+
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
摘自:《剑指offer(第2版)》 >>> 面试题56:数组中数字出现的次数 题目二:数组中唯一只出现一次的数字
2+
3+
问题描述:
4+
在一个数组中除了一个数字只出现一次之外,其他数字都出现了3次,请找出那个只出现了一次的数字。

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL