Skip to content

Commit 0f0a54a

Browse files
committed
feat: add the solution of Set Mismatch(645) with Java.
1 parent 8ddfdc4 commit 0f0a54a

File tree

3 files changed

+66
-24
lines changed

3 files changed

+66
-24
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
| [543][543-question] | [Diameter of Binary Tree][543-tips] | [][543-java] | | |
6464
| [561][561-question] | [Array Partition I][561-tips] | [][561-java] | [][561-js] | |
6565
| [643][643-question] | [Maximum Average Subarray I][643-tips] | [][643-java] | [][643-js] | |
66-
| [645][645-question] | [Set Mismatch][645-tips] | | [][645-js] | |
66+
| [645][645-question] | [Set Mismatch][645-tips] | [][645-java] | [][645-js] | |
6767
| [653][653-question] | [Two Sum IV - Input is a BST][653-tips] | | [][653-js] | |
6868
| [657][657-question] | [Judge Route Circle][657-tips] | | [][657-js] | |
6969
| [661][661-question] | [Image Smoother][661-tips] | | [][661-js] | |
@@ -454,6 +454,7 @@
454454
[554-java]: ./src/_554/Solution.java
455455
[561-java]: ./src/_561/Solution.java
456456
[643-java]: ./src/_643/Solution.java
457+
[645-java]: ./src/_645/Solution.java
457458
[728-java]: ./src/_728/Solution.java
458459
[771-java]: ./src/_771/Solution.java
459460
[804-java]: ./src/_804/Solution.java

src/_645/Solution.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package _645;
2+
3+
public class Solution {
4+
public int[] findErrorNums(int[] nums) {
5+
int[] pos = new int[nums.length + 1];
6+
int[] ans = new int[2];
7+
8+
// Find error number.
9+
for (int i : nums) {
10+
if ((++pos[i]) > 1) {
11+
ans[0] = i;
12+
}
13+
}
14+
15+
// Find Missing number.
16+
for (int i = 0; i < pos.length; i++) {
17+
if (pos[i] == 0) {
18+
ans[1] = i;
19+
}
20+
}
21+
22+
return ans;
23+
}
24+
25+
public static void main(String[] args) {
26+
Solution solution = new Solution();
27+
int[] ans = solution.findErrorNums(new int[] {1, 2, 2, 4});
28+
for (int i : ans) {
29+
System.out.print(i + " ");
30+
}
31+
}
32+
}

tips/645/README.md

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,54 @@
1-
[xxxx][title]
1+
[Set Mismatch][title]
22

33
## Description
4-
// 抄题目
4+
The set `S` originally contains numbers from 1 to `n`. But unfortunately, due to the data error, one of the numbers in the set got duplicated to **another** number in the set, which results in repetition of one number and loss of another number.
55

6+
Given an array `nums` representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.
67

78
**Example:**
89

910
```
10-
// 抄Example
11+
Input: nums = [1,2,2,4]
12+
Output: [2,3]
1113
```
1214

1315
**Note:**
14-
// Note
1516

16-
**Tags:** // tags
17+
1. The given array size will in the range [2, 10000].
18+
2. The given array's numbers won't have any order.
1719

20+
**Tags:** [Hash Table](https://leetcode.com/tag/hash-table/)
1821

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
22+
## 思路
23+
所给数组包括1到n。数组其中一个数由于出错变成了数组中的另一个数。题意要求找出数组中重复的数,并找出缺失的数。数组大小为2到20000,可以通过一个数组来记录每个数字的个数,通过此数组可以比较轻松的找到重复和缺失的数字,时间和空间复杂度都为O(n)。
2324

24-
```
25-
```javascript
25+
**Java**
2626

27-
```
28-
29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
3127
```java
32-
33-
```
34-
35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
38-
28+
public int[] findErrorNums(int[] nums) {
29+
int[] pos = new int[nums.length + 1];
30+
int[] ans = new int[2];
31+
32+
// Find error number.
33+
for (int i : nums) {
34+
if ((++pos[i]) > 1) {
35+
ans[0] = i;
36+
}
37+
}
38+
39+
// Find Missing number.
40+
for (int i = 0; i < pos.length; i++) {
41+
if (pos[i] == 0) {
42+
ans[1] = i;
43+
}
44+
}
45+
46+
return ans;
47+
}
3948
```
4049

4150
## 结语
42-
51+
4352
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4453

4554
[title]: https://leetcode.com/problems/xxxx

0 commit comments

Comments
 (0)