首页 > 其他 > 详细

1426. Counting Elements

时间:2020-05-04 13:20:18      阅读:61      评论:0      收藏:0      [点我收藏+]
package LeetCode_1426

import java.util.*

/**
 * 1426. Counting Elements
 * Given an integer array arr, count element x such that x + 1 is also in arr.
 * If there‘re duplicates in arr, count them separately.
 * Example 1:
 * Input: arr = [1,2,3] Output: 2, Explanation: 1 and 2 are counted cause 2 and 3 are in arr.
Example 2:
Input: arr = [1,1,2] Output: 2, Explanation: 1 counted twice cause 2 is in arr.
 * */
class Solution {
    fun countElements(array: IntArray): Int {
        var ans = 0
        val set = HashSet<Int>()
        for (item in array) {
            set.add(item)
        }
        for (i in array.indices) {
            if (set.contains(array[i] + 1)) {
                ans++
            }
        }
        return ans
    }
}

 

1426. Counting Elements

原文:https://www.cnblogs.com/johnnyzhao/p/12826077.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!