[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 is the only number that appears consecutively for at least three times.

这道题给了我们一个Logs表,让我们找Num列中连续出现相同数字三次的数字,那么由于需要找三次相同数字,所以我们需要建立三个表的实例,我们可以用l1分别和l2, l3内交,l1和l2的Id下一个位置比,l1和l3的下两个位置比,然后将Num都相同的数字返回即可:

解法一:

SELECT DISTINCT l1.Num FROM Logs l1
JOIN Logs l2 ON l1.Id = l2.Id - 1
JOIN Logs l3 ON l1.Id = l3.Id - 2
WHERE l1.Num = l2.Num AND l2.Num = l3.Num;

下面这种方法没用用到Join,而是直接在三个表的实例中查找,然后把四个条件限定上,就可以返回正确结果了:

解法二:

SELECT DISTINCT l1.Num FROM Logs l1, Logs l2, Logs l3
WHERE l1.Id = l2.Id - 1 AND l2.Id = l3.Id - 1
AND l1.Num = l2.Num AND l2.Num = l3.Num;

再来看一种画风截然不同的方法,用到了变量count和pre,分别初始化为0和-1,然后需要注意的是用到了IF语句,MySQL里的IF语句和我们所熟知的其他语言的if不太一样,相当于我们所熟悉的三元操作符a?b:c,若a真返回b,否则返回c,具体可看这个帖子。那么我们先来看对于Num列的第一个数字1,pre由于初始化是-1,和当前Num不同,所以此时count赋1,此时给pre赋为1,然后Num列的第二个1进来,此时的pre和Num相同了,count自增1,到Num列的第三个1进来,count增加到了3,此时满足了where条件,t.n >= 3,所以1就被select出来了,以此类推遍历完整个Num就可以得到最终结果:

解法三:

SELECT DISTINCT Num FROM (
SELECT Num, @count := IF(@pre = Num, @count + 1, 1) AS n, @pre := Num
FROM Logs, (SELECT @count := 0, @pre := -1) AS init
) AS t WHERE t.n >= 3;

参考资料:

https://leetcode.com/discuss/54463/simple-solution

https://leetcode.com/discuss/87854/simple-sql-with-join-1484-ms

https://leetcode.com/discuss/69767/two-solutions-inner-join-and-two-variables

LeetCode All in One 题目讲解汇总(持续更新中...)

时间: 2024-10-01 05:09:19

[LeetCode] Consecutive Numbers 连续的数字的相关文章

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: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  | | 

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

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

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:

[CareerCup] 2.5 Add Two Numbers 两个数字相加

2..5 You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and returns the su

10.16输入一个字符串,内有数字和非数字字符,如: a123x456 17960? 302tab5876 将其中连续的数字作为一个整数,依次存放到一数组num中。例如123放在num[0]中,456放在num[1]中……统计共有多少个整数,并输出这些数。

10.16输入一个字符串,内有数字和非数字字符,如: a123x456 17960? 302tab5876 将其中连续的数字作为一个整数,依次存放到一数组num中.例如123放在num[0]中,456放在num[1]中--统计共有多少个整数,并输出这些数. #include <stdio.h> int main(){ void search(char * parr, int * pnum); char arr[100],* parr; int num[30],* pnum; parr=arr;

leetcode——Reverse Integer 反转整数数字(AC)

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 这个题比较简单,考虑特殊情况如12000,注意检查反转后数字是否会越界溢出.代码如下: class Solution { public: int reverse(int x) { bool minus = false; short int splitNum[10]; int i = 0, j = 0; unsign

Javascript中,数学方法可以分成以下几类: constans(常数)、power functions(乘方函数)、trigonometic functions(三角函数)、rounding functions(舍入函数)、random numbers(随机数字)

在Javascript中,数学方法可以分成以下几类: constans(常数).power functions(乘方函数).trigonometic functions(三角函数).rounding functions(舍入函数).random numbers(随机数字) 常数和乘方函数 Math.E 自然对数的底(为常数) 2.718 Math.LN10 10的自然对数 2.302 Math.LN2 2的自然对数 0.693 Math.PI 圆周率 3.1415 Math.SQRT1_2 1/

JavaScript过滤除连续的数字

if (!Array.prototype.forEach) { Array.prototype.forEach = function (callback, thisArg) { var T, k; if (this == null) { throw new TypeError(" this is null or not defined"); } var O = Object(this); var len = O.length >>> 0; // Hack to con