题目链接:https://leetcode-cn.com/problems/consecutive-numbers/

题目

编写一个 SQL 查询,查找所有至少连续出现三次的数字。

+----+-----+

| Id | Num |

+----+-----+

| 1 | 1 |

| 2 | 1 |

| 3 | 1 |

| 4 | 2 |

| 5 | 1 |

| 6 | 2 |

| 7 | 2 |

+----+-----+

例如,给定上面的 Logs 表, 1 是唯一连续出现至少三次的数字。

+-----------------+

| ConsecutiveNums |

+-----------------+

| 1 |

+-----------------+

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/consecutive-numbers

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解答

先总结方法:

  1. 使用自连接,3个表连接;(如果连续出现1000次,这个方法就不行了。。)
  2. 使用 oracle 窗口函数;
  3. 使用 MySQL 变量的方式;

一开始看的时候,毫无头绪。。

解法一

粗略的看了下解答,用自连接的方法,尝试了下,居然通过了。

---- oracle ----
/* Write your PL/SQL query statement below */
select distinct a.Num as ConsecutiveNums
from Logs a,
Logs b,
Logs c
where a.Id = b.Id + 1
and a.Id = c.Id + 2
and a.Num = b.Num
and a.Num = c.Num --- 806ms

考虑如果 id 不连续出现的情况,则需要在数据表中插入一列新的 id 自增数据进行标识。

解法二

  1. 由于要获取至少连续3次出现的数字,看到这个题肯定是会变的,如果是至少连续出现4次(100次),连接4个表(连接1000个)?这种方法肯定是不可取的。

  2. 找规律,找出这连续起来的数字有什么规律呢?发现连续的数字是相同的数字,但是id有可能不是连续的,我们就需要通过对结果集进行再次编号,让其变成连续的。

  3. 首先我们获取到对每条数据编号从1开始使用 row_number() 函数使用 id 来排序。

  4. 然后我们通过另一种方式排序将这些 num 值一样的进行排序,然后对其编号同样使用 row_bumber() 使用 num 来分组使用 id 排序 over(partition by num order by id)

  5. 通过3、4步骤,两个相减之后我们可以得到,只要是相等的,则相减的值是一样的。而且如果不连续的话相减值也不一样。

  6. 最后再通过 numrn 两个共同分组找到一样的一共有几个,就可以找到连续的了。

最终代码为:

---- oracle ----
/* Write your PL/SQL query statement below */
select distinct Num as ConsecutiveNums
from
(
select Num,
rn,
count(Id) as cnt
from
(
select Id,
Num,
row_number() over(order by id) - row_number() over(partition by num order by id) as rn
from Logs
)
group by Num, rn
)
where cnt >= 3; ---- 1414ms 好慢

想了想,会不会存在 Num + rn 误判的情况?因为一直递增,貌似不会。

解法三

使用2个变量进行统计。

---- MySQL ----
select distinct t.Num as ConsecutiveNums
from
(
select a.Num,
@cnt := if(@pre = a.Num, @cnt + 1, 1) cnt,
@pre := a.Num pre
from Logs a,
(select @pre := null,
@cnt := 0) b
) t
where t.cnt >= 3; ---- 179ms

判断当前Num与上一个Num是否一致,如果是cnt加一,如果不是,重新计数。

高明!

思考

联想到以前自己的一个奇思妙想:赌钱!押大还是押小,买定离手!

  1. 买1块钱大,赢了的话,继续买;
  2. 输了的话,买2块钱大;
  3. 赢了的话,恢复买1块,输了的话,买4块钱大;
  4. 以此循环
  5. 设定上限,直到128块就认输,止损。

当时想到这个做法的时候,问自己,如果我有较多的资金的话,假设我有1w,那一直买,买到8192的时候,已经是2的13次方了。。不可能连续输了13次吧。。当时这么想。。

后来,觉得连续开13次小还是有概率的,想用程序随机模拟一下输赢的概率。。但是拖延症上身,一直没有去实践。。

今天这道题,尝试一下吧。。

LeetCode:180.连续出现的数字的更多相关文章

  1. 180. 连续出现的数字 + MySql + 连续出现数字 + 多表联合查询

    180. 连续出现的数字 LeetCode_MySql_180 题目描述 代码实现 # Write your MySQL query statement below select distinct t ...

  2. sql 180. 连续出现的数字

    编写一个 SQL 查询,查找所有至少连续出现三次的数字. +----+-----+| Id | Num |+----+-----+| 1 | 1 || 2 | 1 || 3 | 1 || 4 | 2 ...

  3. js验证连续两位数字递增或递减和连续三位数字相同

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  4. LeetCode:至少是其他数字两倍的最大数【747】

    LeetCode:至少是其他数字两倍的最大数[747] 题目描述 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素 ...

  5. LeetCode数组中重复的数字

    LeetCode 数组中重复的数字 题目描述 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. ...

  6. [SQL]LeetCode180. 连续出现的数字 | Consecutive Numbers

    SQL架构: Create table If Not Exists Logs (Id int, Num int) Truncate table Logs insert into Logs (Id, N ...

  7. LeetCode 788. Rotated Digits (旋转数字)

    X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...

  8. [LeetCode] Lexicographical Numbers 字典顺序的数字

    Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...

  9. [LeetCode] Missing Number 丢失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

随机推荐

  1. kotlin 委托类的初始化函数

    import java.beans.AppletInitializer import kotlin.reflect.KProperty fun main(arg: Array<String> ...

  2. 朴素贝叶斯算法python实现

    朴素贝叶斯是一种十分简单的分类算法,称其朴素是因为其思想基础的简单性,就文本分类而言,他认为词袋中的两两词之间的关系是相互独立的,即一个对象的特征向量中的每个维度都是互相独立的.这是朴素贝叶斯理论的思 ...

  3. .IllegalArgumentException: Mapped Statements collection does not contain 异常一例【我】

    更新代码后发现几乎所有的sql查询都报错,类似下面: java.lang.RuntimeException: org.mybatis.spring.MyBatisSystemException: ne ...

  4. 仙剑奇侠传1系列:2.编译主程序SDLPAL及SDL

    上一篇:仙剑奇侠传1系列:1.本地运行环境及兼容性设置 介绍 仙剑奇侠传1是dos时代的经典游戏,相信以下图片能勾起大家的很多回忆.   sdlpal是仙剑奇侠传1的主程序.github项目sdlpa ...

  5. pip安装软件报错 utf-8 code can't decode byte 0xcf in position7

    pip安装软件报错 utf-8 code can't decode byte 0xcf in position7 根据错误提示的路径找到__init__.py文件 根据错误提示的最后几句话找到对应的行 ...

  6. img标签替换为mip-img标签的方法

    function replaceMipImages($content){ preg_match_all('/<img (.*?)\>/', $content, $images); if(! ...

  7. lexicalized Parsing

    $q$(S $\rightarrow$ NP VP) * $q$(NP $\rightarrow$ NNP) * $q$(VP $\rightarrow$ VB NP) * $q$(NP $\righ ...

  8. iOS-Http断点续传

    下载LOFTER客户端IOS Http断点续传浅析 http实现断点续传的关键地方就是在httprequest中加入“Range”头. //设置Range头,值:bytes=x-y;x:开始字节,y: ...

  9. Golang sync.WaitGroup的用法

    0x01 介绍 经常会看到以下了代码: 12345678910111213 package main import ( "fmt" "time") func m ...

  10. windows是下安装nvmw

    nvmw:Windows环境下的node多版本管理工具. 安装: 1.  git clone https://github.com/hakobera/nvmw.git 2.修改环境变量 计算机 -&g ...