961. N-Repeated Element in Size 2N Array

961. N-Repeated Element in Size 2N Array

题目概述:

在大小为 2N 的数组 A 中有 N+1 个不同的元素,其中有一个元素重复了 N 次。

返回重复了 N 次的那个元素。

解法一

class Solution {
    public int repeatedNTimes(int[] A) {
        int index = 0;
        Set<Integer> set = new LinkedHashSet<>();
        for (int i = 0; i < A.length / 2 + 2; i++) {
            if (set.contains(A[i])) {
                index = i;break;
            }
            set.add(A[i]);
        }
        return A[index];
    }
}

解法二

class Solution {
    public int repeatedNTimes(int[] A) {
        int[] count = new int[10000];
        for (int a : A) {
            if (count[a]++ == 1) {
                return a;
            }
        }
        return -1;
    }
}

解法三

class Solution {
    public int repeatedNTimes(int[] A) {
        int i = 0, j = 0, n = A.length;
        while (i == j || A[i] != A[j]) {
            i = (int)(Math.random()*n);
            j = (int)(Math.random()*n);
        }
        return A[i];
    }
}

原文地址:https://www.cnblogs.com/mrjoker-lzh/p/10259506.html

时间: 2024-11-09 05:07:17

961. N-Repeated Element in Size 2N Array的相关文章

【leetcode】961. N-Repeated Element in Size 2N Array

题目如下: In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times. Return the element repeated N times. Example 1: Input: [1,2,3,3] Output: 3 Example 2: Input: [2,1,2,5,3,2] Output: 2 Example 3: Input

[Leetcode]961. N-Repeated Element in Size 2N Array

Easy In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times. Return the element repeated N times. Example 1: Input: [1,2,3,3] Output: 3 Example 2: Input: [2,1,2,5,3,2] Output: 2 Example 3: Input:

[Swift Weekly Contest 116]LeetCode961. 重复 N 次的元素 | N-Repeated Element in Size 2N Array

In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times. Return the element repeated N times. Example 1: Input: [1,2,3,3] Output: 3 Example 2: Input: [2,1,2,5,3,2] Output: 2 Example 3: Input: [5,1

insert an element at index into array

given an array, and an element to insert, and the position to insert this element,return a new array with the element inserted 1,2,3,4,5,6 -> 1,2,3,15,4,5,6 1 public static void main(String[] args) { 2 int[] org = new int[]{1,2,3,4,5,6} ; 3 int[] res

540. Single Element in a Sorted Array(LeetCode)

Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,

540. Single Element in a Sorted Array

问题描述: Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,

Single Element in a Sorted Array

Given a sorted array consisting of only integers where every element appears twice except for one element which appears once. Find this single element that appears only once. Example 1: Input: [1,1,2,3,3,4,4,8,8] Output: 2 Example 2: Input: [3,3,7,7,

LeetCode 1150. Check If a Number Is Majority Element in a Sorted Array

原题链接在这里:https://leetcode.com/problems/check-if-a-number-is-majority-element-in-a-sorted-array/ 题目: Given an array nums sorted in non-decreasing order, and a number target, return True if and only if target is a majority element. A majority element is

LeetCode.540.Single Element in a Sorted Array

恩, 沙比提 1 class Solution(object): 2 def singleNonDuplicate(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 xorsum = 0 8 for i in nums: 9 xorsum = xorsum ^ i 10 return xorsum 11