zoukankan      html  css  js  c++  java
  • 961. N-Repeated Element in Size 2N Array

    package LeetCode_961
    
    /**
     * 961. N-Repeated Element in Size 2N Array
     * https://leetcode.com/problems/n-repeated-element-in-size-2n-array/description/
     *
    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
     * */
    class Solution {
        fun repeatedNTimes(A: IntArray): Int {
            /*val n = A.size/2
            val map = HashMap<Int,Int>()
            for (i in A){
                map.put(i, map.getOrDefault(i,0)+1)
            }
            for (m in map){
                if (m.value==n){
                    return m.key
                }
            }*/
    
            //because exactly one of these elements is repeated N times
            val array = IntArray(10000)
            for (num in A){
                array[num]++
                if (array[num]>1){
                    return num
                }
            }
            return 0
        }
    }
  • 相关阅读:
    2017普及组D1T3 洛谷P3956 棋盘
    2017提高组D1T1 洛谷P3951 小凯的疑惑
    Title
    Title
    Title
    Title
    Title
    Title
    Title
    Title
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13168180.html
Copyright © 2011-2022 走看看