题目链接: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. insmod内核模块时提示Failed to find the folder holding the modules怎么办?

    答:笔者通过重新编译内核和根文件系统解决了此问题 (笔者使用的是openwrt系统) 分析: 1. ’Failed to find the folder holding the modules‘这句l ...

  2. linux下如何交叉编译util-linux?

    1. 获取源码 wget https://mirrors.edge.kernel.org/pub/linux/utils/util-linux/v2.34/util-linux-2.34.tar.xz ...

  3. set serveroutput on

    使用set serveroutput on 命令设置环境变量serveroutput为打开状态,从而使得pl/sql程序能够在SQL*plus中输出结果 使用函数dbms_output.put_lin ...

  4. ssh登陆

    之前在windows下有putty,xshell,mobaXterm 工具可以选择. 现在用mac,暂时没怎么找到好到软件,直接用ssh好了. ssh -p 22 username@192.xxx.x ...

  5. python3 高级编程(二) 动态给类添加方法功能

    class Student(object): pass 给实例绑定一个属性: >>> s = Student() >>> s.name = 'Michael' # ...

  6. ubuntu下virtualbox的安装、卸载

    一.添加VirtualBox的源并安装5.1版本 virtualbox官网:https://www.virtualbox.org/wiki/Download_Old_Builds 虽然也可以直接安装d ...

  7. React Native清除缓存实现

    清除缓存使用的第三方:react-native-http-cache   Github: https://github.com/reactnativecn/react-native-http-cach ...

  8. 总是访问到tomcat首页解决

    部署代码后总是访问到tomcat首页解决 没有把路径写全 访问:养成带上绝对路径的习惯,否则总是访问到tomcat的首页 http://114.116.65.232:8085/ssoserver/

  9. 提取站长之家IP批量查询

    1.工具说明 写报告的时候为了细致性,要把IP地址对应的地区给整理出来.500多条IP地址找出对应地区复制粘贴到报告里整了一个上午. 为了下次更好的完成这项重复性很高的工作,所以写了这个小的脚本. 使 ...

  10. 神经网络(NN)实现多分类-----Keras实现

    IRIS数据集介绍   IRIS数据集(鸢尾花数据集),是一个经典的机器学习数据集,适合作为多分类问题的测试数据,它的下载地址为:http://archive.ics.uci.edu/ml/machi ...