[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: [5,1,5,2,5,3,5,4]
Output: 5

Note:

  1. 4 <= A.length <= 10000
  2. 0 <= A[i] < 10000
  3. A.length is even

题目大意:在一个大小为2N的数组中,包含N+1个不同的数字,其中有一个数字重复N次,找出这个多次重复的数字。

提示:

1.数组长度为0~10000

2.数字大小为大于等于0,小于10000

3.数组的长度为偶数

由题目可知,数组中除了那个重复N次的数字以外,其他数字都是各不相同的,因此只要判断出哪个数字重复,那么这个数字就是目标数字。

可以使用无序集合unordered_set对出现的数字进行记录,只要数字不是第一次出现,那么它就是目标数字。

代码如下:

class Solution {
public:
    int repeatedNTimes(vector<int>& A) {
        unordered_set<int> nums;
        for(int num : A){
            if(nums.count(num) != 0){
                return num;
            }
            nums.insert(num);
        }
        return 0;
    }
};

原文地址:https://www.cnblogs.com/cff2121/p/11421127.html

时间: 2024-11-09 05:12:53

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

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 <

【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

[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

【LeetCode 229】Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方

网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2],5,3 返回:2 note: 注意手写快排的时候: while(i < j) { while(j > i && a[j] > a[left]) j--; while(i < j && a[i] <= a[left]) i++; if(i

【leetcode】Kth Largest Element in an Array (middle)☆

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. For example,Given [3,2,1,5,6,4] and k = 2, return 5. 思路: 堆.讲解:二叉堆 class Solution { public: //新插入i结点 其父节点为(i

LeetCode:Find Peak Element - 寻找一个数组内的顶点

1.题目名称 Find Peak Element(寻找一个数组内的顶点) 2.题目地址 https://leetcode.com/problems/find-peak-element/ 3.题目内容 英文: A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ num[i+1], find a peak element and return its

Leetcode 线性表 Remove Element

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Element Total Accepted: 13840 Total Submissions: 42676 Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be change

[LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.

1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int bits = sizeof(int)*8; 5 int result=0; 6 for(int i=1; i<=bits; i++) 7 { 8 int w=0; 9 int t=1; 10 11 for(int j=0; j<n; j++) 12 w += (A[j]>>(i-1))&t; 13 result+= (w%3)<