algorithm-base/animation-simulation/数组篇/剑指offer3数组中重复的数.md

198 lines
5.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [ Offer 03. ](https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/)
****
n nums 0 n-1
1
[2, 3, 1, 0, 2, 5, 3]
2 3
#### **HashSet**
****
HashSet HashSet set
****
```java
class Solution {
public int findRepeatNumber(int[] nums) {
// HashSet
HashSet<Integer> set = new HashSet<Integer>();
for (int x : nums) {
//发现某元素存在,返回
if (set.contains(x)) {
return x;
}
//存入哈希表
set.add(x);
}
return -1;
}
}
```
Python Code:
```python
from typing import List
class Solution:
def findRepeatNumber(self, nums: List[int])->int:
s = set()
for x in nums:
#
if x in s:
return x
#
s.add(x)
return -1
```
Swift Code:
```swift
class Solution {
func findRepeatNumber(_ nums: [Int]) -> Int {
var set: Set<Int> = []
for n in nums {
if set.contains(n) { // 如果发现某元素存在,则返回
return n
}
set.insert(n) // 存入集合
}
return -1
}
}
```
#### ****
****
****
![offer3](https://cdn.jsdelivr.net/gh/tan45du/test1@master/20210122/剑指offer3数组中重复的数.2p6cd5os0em0.gif)
****
Java Code:
```java
class Solution {
public int findRepeatNumber(int[] nums) {
if (nums.length == 0) {
return -1;
}
for (int i = 0; i < nums.length; ++i) {
while (nums[i] != i) {
//发现重复元素
if (nums[i] == nums[nums[i]]) {
return nums[i];
}
//置换,将指针下的元素换到属于他的索引处
int temp = nums[i];
nums[i] = nums[temp];
nums[temp] = temp;
}
}
return -1;
}
}
```
C++ Code:
```cpp
class Solution {
public:
int findRepeatNumber(vector<int>& nums) {
if(nums.empty()) return 0;
int n = nums.size();
for(int i = 0; i < n; ++i){
while(nums[i] != i){
if(nums[i] == nums[nums[i]]) return nums[i];
swap(nums[i], nums[nums[i]]);
}
}
return -1;
}
};
```
Python3 Code:
```python
from typing import List
class Solution:
def findRepeatNumber(self, nums: List[int])->int:
if len(nums) == 0:
return -1
for i in range(0, len(nums)):
while nums[i] != i:
#
if nums[i] == nums[nums[i]]:
return nums[i]
#
temp = nums[i]
nums[i] = nums[temp]
nums[temp] = temp
return -1
```
Swift Code:
```swift
class Solution {
func findRepeatNumber(_ nums: [Int]) -> Int {
if nums.isEmpty {
return -1
}
var nums = nums;
for i in 0..<nums.count {
while nums[i] != i {
if nums[i] == nums[nums[i]] {
return nums[i]
}
nums.swapAt(i, nums[i])
}
}
return -1
}
}
```
Go Code:
```go
func findRepeatNumber(nums []int) int {
l := len(nums)
if l == 0 {
return -1
}
for i := 0; i < l; i++ {
// 将nums[i]换到i的位置。
for nums[i] != i {
if nums[i] == nums[nums[i]] {
return nums[i]
}
nums[i], nums[nums[i]] = nums[nums[i]], nums[i]
}
}
return -1
}
```