哈希集合、间隔、随机化,3 种方法求解《961. 在长度 2N 的数组中找出重复 N 次的元素》
给你一个整数数组 nums ,该数组具有以下属性: nums.length == 2 * n. nums 包含 n + 1 个 不同的 元素 nums 中恰有一个元素重复 n 次 找出并返回重复了 n 次的那个元素。
var repeatedNTimes = function(nums) {
const h = new Uint8Array(10 ** 4 + 1)
for (let i = 0; i < nums.length; i++) {
if (h[nums[i]] === 1) return nums[i]
h[nums[i]] = 1
}
}; var repeatedNTimes = function(nums) {
const h = new Set
for (let i = 0; i < nums.length; i++) {
if (h.has(nums[i])) return nums[i]
h.add(nums[i])
}
}; var repeatedNTimes = function(nums) {
const h = Object.create(null)
for (let i = 0; i < nums.length; i++) {
if (h[nums[i]] !== void 0) return nums[i]
h[nums[i]] = 1
}
}; function repeatedNTimes(nums: number[]): number {
const s = new Set
for (let i = 0; i < nums.length; i++) {
if (s.has(nums[i])) return nums[i]
s.add(nums[i])
}
}; func repeatedNTimes(nums []int) int {
type void struct{}
var value void
s := make(map[int]void)
for _, num := range nums {
if _, ok := s[num]; ok {
return num
}
s[num] = value
}
return -1
} func repeatedNTimes(nums []int) int {
s := map[int]bool{}
for _, num := range nums {
if s[num] {
return num
}
s[num] = true
}
return -1
} class Solution {
function repeatedNTimes($nums) {
$s = [];
foreach ($nums as $num) {
if ($s[$num]) return $num;
$s[$num] = 1;
}
}
} class Solution:
def repeatedNTimes(self, nums: List[int]) -> int:
s = set()
for num in nums:
if num in s:
return num
s.add(num) class Solution {
public int repeatedNTimes(int[] nums) {
Set<Integer> s = new HashSet<Integer>();
for (int num : nums) {
if (s.contains(num)) return num;
s.add(num);
}
return -1;
}
} 两个相同元素的间隔 <= 2 个小元素(索引相差 <= 3)
[0, 1, 2, 3]
var repeatedNTimes = function(nums) {
const n = nums.length
for (let gap = 1; gap <= 3; gap++) {
for (let i = 0; i + gap < n; i++) {
if (nums[i] === nums[i + gap]) return nums[i]
}
}
}; function repeatedNTimes(nums: number[]): number {
const n = nums.length
for (let gap = 1; gap <= 3; gap++) {
for (let i = 0; i + gap < n; i++) {
if (nums[i] === nums[i + gap]) return nums[i]
}
}
}; func repeatedNTimes(nums []int) int {
n := len(nums)
for gap := 1; gap <= 3; gap++ {
for i := 0; i + gap < n; i++ {
if nums[i] == nums[i + gap] {
return nums[i]
}
}
}
return -1
} class Solution {
function repeatedNTimes($nums) {
$n = count($nums);
for ($gap = 1; $gap <= 3; $gap++) {
for ($i = 0; $i + $gap < $n; $i++) {
if ($nums[$i] === $nums[$i + $gap]) return $nums[$i];
}
}
}
} class Solution:
def repeatedNTimes(self, nums: List[int]) -> int:
n = len(nums)
for gap in range(1, 4):
for i in range(0, n -gap):
if nums[i] == nums[i + gap]: return nums[i] class Solution {
public int repeatedNTimes(int[] nums) {
int n = nums.length;
for (int gap = 1; gap <= 3; gap++) {
for (int i = 0; i + gap < n; i++) {
if (nums[i] == nums[i + gap]) return nums[i];
}
}
return -1;
}
} var repeatedNTimes = function(nums) {
const n = nums.length
while (true) {
const x = Math.random() * n | 0, y = Math.random() * n | 0
if (x !== y && nums[x] === nums[y]) return nums[x]
}
}; function repeatedNTimes(nums: number[]): number {
const n = nums.length
while(true) {
const x = Math.random() * n >> 0, y = Math.random() * n >> 0
if (x !== y && nums[x] === nums[y]) return nums[x]
}
}; function repeatedNTimes(nums: number[]): number {
const n = nums.length
while(true) {
const x = ~~(Math.random() * n), y = ~~(Math.random() * n)
if (x !== y && nums[x] === nums[y]) return nums[x]
}
}; func repeatedNTimes(nums []int) int {
n := len(nums)
for {
x, y := rand.Intn(n), rand.Intn(n)
if x != y && nums[x] == nums[y] {
return nums[x]
}
}
} class Solution {
function repeatedNTimes($nums) {
$n = count($nums);
while (true) {
$x = rand(0, $n);
$y = rand(0, $n);
if ($x !== $y && $nums[$x] === $nums[$y]) return $nums[$x];
}
}
} class Solution:
def repeatedNTimes(self, nums: List[int]) -> int:
n = len(nums)
while True:
x, y = random.randrange(n), random.randrange(n)
if x != y and nums[x] == nums[y]: return nums[x] class Solution {
public int repeatedNTimes(int[] nums) {
int n = nums.length;
Random random = new Random();
while (true) {
int x = random.nextInt(n);
int y = random.nextInt(n);
if (x != y && nums[x] == nums[y]) return nums[x];
}
}
}