[LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)
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 |
| 7 | 2 |
+----+-----+
比如上表中,写出的结果是1.
4. 解题思路
解法一:直接使用一套SELECT - FROM -WHERE语句:
SELECT DISTINCT L1.Num FROM Logs L1, Logs L2, Logs L3
WHERE (L1.Id = L2.Id + 1 AND L1.Num = L2.Num) AND
(L1.Id = L3.Id + 2 AND L1.Num = L3.Num);
理论上正确,但是在leetcode上运行结果不通过,还未知什么原因。看过discuss的讨论后,加入返回的列的名字ConsecutiveNums后AC,但是郁闷的是题目没有给出要返回的列的名字。
解法二:可以使用JOIN句子完成相同的功能
SELECT DISTINCT L1.Num FROM Logs L1
JOIN Logs L2 ON L1.Id + 1 = L2.Id
JOIN Logs L3 ON L1.Id + 2 = L3.Id
WHERE L1.Num = L2.Num AND L1.Num = L3.Num
ORDER BY L1.Num
运行结果不通过。在加入 返回的列名ConsecutiveNums 后,运行通过,不过效率没有第一种方法高。
解法三:上面两种方法可以用于找到至少三次连续出现的数字,如果将连续出现的数字扩展到N个,按照上面思路写出的SQL语句就会比较长。因此可以用下面这种方式来查询:
SELECT DISTINCT Num
FROM (
SELECT Num,
CASE
WHEN @prev = Num THEN @count := @count + 1
WHEN (@prev := Num) IS NOT NULL THEN @count := 1
END CNT
FROM Logs, (SELECT @prev := NULL) X
ORDER BY Id
) AS A
WHERE A.CNT >= 3
将最后一行的3改为N,即可用于查询至少N次连续出现的数字。
[LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)的更多相关文章
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- Mysql数据库知识-Mysql索引总结 mysql mysql数据库 mysql函数
mysql数据库知识-Mysql索引总结: 索引(Index)是帮助MySQL高效获取数据的数据结构. 下边是自己整理的资料与自己的学习总结,,做一个汇总. 一.真的有必要使用索引吗? 不是每一个性能 ...
- [LeetCode] Rank Scores -- 数据库知识(mysql)
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- [LeetCode] Lexicographical Numbers 字典顺序的数字
Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,1 ...
- LeetCode——Consecutive Numbers
Description: Write a SQL query to find all numbers that appear at least three times consecutively. + ...
- [LeetCode] Department Highest Salary -- 数据库知识(mysql)
184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a s ...
- leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)
一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...
- [LeetCode] Add Two Numbers 两个数字相加
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- [LeetCode] 2. Add Two Numbers 两个数字相加 java语言实现 C++语言实现
[LeetCode] Add Two Numbers 两个数字相加 You are given two non-empty linked lists representing two non-ne ...
随机推荐
- Vim+Ctags+Taglist组合:
Ctags 1,sudo apt-get install Ctags //会提示最新版本的名字:Exuberant Ctags 2,在源码的最上层目录执行:ctags -R //会在当前目录先生成一个 ...
- Ref相关的名词解释
NV (NOT-VOLATILE),即非易失性,断电不会丢失的存储信息,包括生产信息.客户信息.产品信息等等. 它们都保存在不同(FLASH)分区,并根据不同的分区提供不同的接口.数据结构和管理机制. ...
- Android 系统 reboot
/*********************************************************************** * Android 系统 reboot * 说明: * ...
- mysql 数据库常用命令总结
(1)查看数据库可以支持的存储引擎 命令:show engines; (2)查看表结构命令:desc table_name:(3)显示表的创建语句 show create table ta ...
- (六) 6.2 Neurons Networks Backpropagation Algorithm
今天得主题是BP算法.大规模的神经网络可以使用batch gradient descent算法求解,也可以使用 stochastic gradient descent 算法,求解的关键问题在于求得每层 ...
- UML时序图
时序图定义 : 描述了对象之间传递消息的时间顺序, 用来表示用例中的行为顺序, 是强调消息时间顺序的交互图; 时序图描述的事物: 时序图描述系统中类和类之间的交互, 将这些交互建模成消息交换, 时序图 ...
- AIX 第2章 指令记录
root@db:/#mount node mounted mounted over vfs date options ------- ...
- hdu 2899(数学基础+二分)
题意:给了你一个函数,然后给了你x的变化范围,让你求出函数的最小值. 分析:它让你求的是函数的最小值,所以我们可以先对函数求导,得到的导数就可以判断函数的单调性了,求出导数后,我们发现如果函数的导数是 ...
- [Everyday Mathematic]20150213
设 $f:\bbR\to\bbR$ 三阶可微, 试证: 存在 $\xi\in (-1,,1)$, 使得 $$\bex \frac{f'''(\xi)}{6}=\frac{f(1)-f(-1)}{2}- ...
- c# webbrowser获取滚动条最大值
int HeightMax = 0; int WidthMax =0; HeightMax = webBrowser1.Document.Body.ScrollRectangle.Height-web ...