leetcode consecutive-numbers 数字连续出现次数大于三次的数字

题目详情参见:

https://leetcode.com/problems/consecutive-numbers/

难度也只是medium,中等难度,阅读题意,也只是统计一下数字连续出现次数大于3次,例如,1,1,1,2,2,1

在这个序列中,只有1 连续出现了3次,最后显示1 即可,注意排除重复数字,即1 可能 1,1,1,2,2,1,1,1,1,这种情况下,显示一个1即可,在sql语句中添加 distinct过滤重复记录即可。

关键:定义变量统计数字的连续计数。

select distinct t.num
from
(select
num,
@con_count:=IF(@pre_num = num,@con_count+1,1) as con_count,
@pre_num:=num as pre_num
from Logs ,(select @con_count:=null,@pre_num:=null)val
)t
where t.con_count > 2;

时间: 2024-08-07 10:29:56

leetcode consecutive-numbers 数字连续出现次数大于三次的数字的相关文章

[LeetCode] Consecutive Numbers 连续的数字

Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | Id | Num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +----+-----+ For example, given the above Logs table, 1

[LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Hint: A direct

LeetCode——Consecutive Numbers

Description: Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | Id | Num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +----+-----+ For example, given the above

LeetCode-Algorithms #005 Longest Palindromic Substring, Database #179 Consecutive Numbers

LeetCode-Algorithms #005 Longest Palindromic Substring 英语学习时间palindromic: [医] 复发的, 再发的 在数学和计算机上,就指回文 这道题目就是找出给定字符串中最长的回文子串, 可以假定原字符串的长度不超过1000 直接遍历来做肯定是不难, 但也可以想见一定是慢得可以. 那么我的另一个想法是, 从原串中的每一个字符, 或每两个字符中间的空隙开始, 向左右两边判断是否为回文串, 最后找出最长的 1 class Solution

给定一个长度为N的数组,找出出现次数大于n/2,n/3的数,要求时间复杂度O(n),空间复杂度O(1)

先讨论出现次数大于n/2的数字,如果这样的数字存在,那么这个数出现的次数大于其他数出现的次数的总和. 在数组A中,我们定义两个数据集合a1,a2.a1为出现次数大于n/2的数的集合,a2为其余数组成的集合.对于数组 A中元素a.b,假设a不等于b,那么有两种情况,分别为:a属于a1,b属于a2:a属于a2,b属于a2.对于这两种情况,如 果把a.b从数组A中去掉,集合a1的size依旧是大于a2的.按照这个思路,我们有如下代码: int m; int count = 0; for (auto n

LeetCode:Consecutive Numbers - 找出连续出现的数字

1.题目名称 Consecutive Numbers(找出连续出现的数字) 2.题目地址 https://leetcode.com/problems/consecutive-numbers/ 3.题目内容 写一个SQL,查出表Logs中连续出现至少3次的数字: +----+-----+ | Id | Num | +----+-----+ | 1  |  1  | | 2  |  1  | | 3  |  1  | | 4  |  2  | | 5  |  1  | | 6  |  2  | | 

leetcode180 连续出现的数字 Consecutive Numbers

编写一个SQL查询,查找至少连续出现三次的所有数字. 创建表和数据: Create table If Not Exists Logs (Id int,Num int); Truncate table Logs; insert into Logs (Id, Num) values ('1','1'); insert into Logs (Id, Num) values ('2','1'); insert into Logs (Id, Num) values ('3', '1'); insert i

1296. Divide Array in Sets of K Consecutive Numbers

Given an array of integers nums and a positive integer k, find whether it's possible to divide this array into sets of k consecutive numbersReturn True if its possible otherwise return False. Example 1: Input: nums = [1,2,3,3,4,4,5,6], k = 4 Output:

LeetCode--SQL 查询:查找所有至少连续出现三次的数字。

编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+| Id | Num |+----+-----+| 1 | 1 || 2 | 1 || 3 | 1 || 4 | 2 || 5 | 1 || 6 | 2 || 7 | 2 |+----+-----+例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字. +-----------------+| ConsecutiveNums |+-----------------+| 1 |+------------