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 seperately.

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,3,3,5,5,7,7]

Output: 0

Explanation: No numbers are counted, cause there's no 2, 4, 6, or 8 in arr.

Example 3:

Input: arr = [1,3,2,3,5,0]

Output: 3

Explanation: 0, 1 and 2 are counted cause 1, 2 and 3 are in arr.

Example 4:

Input: arr = [1,1,2,2]

Output: 2

Explanation: Two 1s are counted cause 2 is in arr.

Constraints:

1 <= arr.length <= 1000

0 <= arr[i] <= 1000

Solution

思路:用一个集合存储元素,再遍历即可

class Solution:
def countElements(self, arr: List[int]) -> int:
myset = set(arr)
count = 0
for i in arr:
if i+1 in myset:
count += 1
return count

分析:

时间复杂度:O(N)

空间复杂度:O(N)

leetcode 30 day challenge Counting Elements的更多相关文章

  1. C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  2. [LeetCode] 632. Smallest Range Covering Elements from K Lists 覆盖K个列表元素的最小区间

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

  3. [leetcode]347. Top K Frequent Elements K个最常见元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  4. LeetCode 203. Remove Linked List Elements (移除链表中的项)

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  5. [LeetCode] 658. Find K Closest Elements 寻找K个最近元素

    Given a sorted array, two integers k and x, find the k closest elements to x in the array. The resul ...

  6. [LeetCode] 347. Top K Frequent Elements 前K个高频元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  7. [LeetCode] 30. Substring with Concatenation of All Words 串联所有单词的子串

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  8. [LeetCode] 203. Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  9. Java实现 LeetCode 30 串联所有单词的子串

    30. 串联所有单词的子串 给定一个字符串 s 和一些长度相同的单词 words.找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置. 注意子串要与 words 中的单词完全匹配, ...

随机推荐

  1. [Alg] 文本匹配-单模匹配与多模匹配

    实际场景: 网站的用户发了一些帖子S1, S2,...,网站就要审核一下这些帖子里有没有敏感词. 1. 如果网站想查一下帖子里有没有一个敏感词P,这个文本匹配要怎么做更快? 2. 如果网站想查一下帖子 ...

  2. Java并发编程之CAS第一篇-什么是CAS

    Java并发编程之CAS第一篇-什么是CAS 通过前面几篇的学习,我们对并发编程两个高频知识点了解了其中的一个—volatitl.从这一篇文章开始,我们将要学习另一个知识点—CAS.本篇是<凯哥 ...

  3. xheditor图片上传

    前端步骤:分为三部,这三部做完之后就能正确显示富文本了 1.下载xheditor文件,并按照如下要求进行引入: <!-- xheditor富文本的文件引入 --> <script t ...

  4. 最详细的SSM(Spring+Spring MVC+MyBatis)项目搭建

    速览 使用Spring+Spring MVC+MyBatis搭建项目 开发工具IDEA(Ecplise步骤类似,代码完全一样) 项目类型Maven工程 数据库MySQL8.0 数据库连接池:Druid ...

  5. python使用pip安装模块出现ReadTimeoutError: HTTPSConnectionPool的解决方法

    今天使用pip安装第三库时,有时会报错: pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='file ...

  6. C 2013笔试题

    1.把整数分解成素数 如90=2*3*3*5 [见2015年] 方法一: int main() { int n, i=2; printf("\nInput:"); scanf(&q ...

  7. 【i春秋 综合渗透训练】渗透测试笔记

    网站是齐博CMS V7.0    1.要求获得管理员密码:   利用齐博CMS V7.0  SQL爆破注入漏洞即可得到管理员用户名密码    https://www.cnblogs.com/vspid ...

  8. 基础的Linux命令(二)

    本文介绍两大类命令,都是最基础的部分 系统状态监测命令 工作目录切换命令 一.系统状态监测命令 1. ifconfig 命令 用于获取网卡配置与网络状态等信息 如下图:inet后面是 IP 地址,et ...

  9. Web 服务器压力测试实例详解

    发表于 2012-1-6 14:53 | 来自  51CTO网页 Web 服务器搭建完成上线在即,其能够承载多大的访问量,响应速度.容错能力等性能指标,所有这些是管理人员最想知道也最为担心的.如何才能 ...

  10. OpenCV-Python Canny边缘检测 | 十九

    目标 在本章中,我们将学习 Canny边缘检测的概念 OpenCV函数: cv.Canny() 理论 Canny Edge Detection是一种流行的边缘检测算法.它由John F. Canny发 ...