60天带你刷完Leetcode【第9天】596 ~ 585

596

题目:有一个含有student和class的课表courses,请列出拥有等于或多余5个学生的所有课程。 每门课程不得对同一个学生进行重复计数。

题解:用group by从courses中选取不同的课程,并使用having condition来对选取出的课程进行筛选。count(distinct student)可以对每门课程的上课人数进行不重复计数。

SELECT
    class
FROM
    courses
GROUP BY class
HAVING count(distinct student) >= 5;

595

题目:用SQL对world集合里的国家进行筛选并输出筛选后国家的名字,人口和面积。要求输出的国家满足area大于300万平方公里或者population多于2500万。

题解:用where condition筛选出world集合里area大于300万平方公里或者population多于2500万的国家即可。

SELECT
    name, population, area
FROM
    world
WHERE
    population > 25000000 or area > 3000000;

594

题目:Longest Harmonious Subsequence

  • We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1.

  • Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subsequences.

题解:我们可以用一个map来记录每个数字在序列中出现的次数,再对map进行遍历即可:

class Solution {
public:
    int findLHS(vector<int>& nums) {
        map<int,int> cnt;
        for(auto k:nums) cnt[k]++;
        int ans = 0;
        for(auto p:cnt) if(cnt.count(p.first+1))
            ans = max(ans, p.second+cnt[p.first+1]);
        return ans;
    }
};

593

题目:Valid Square

  • Given the coordinates of four points in 2D space, return whether the four points could construct a square.

题解:假如形成正方形, p1 一定为其定点,这样线段p1p3 和 p1p3一定至少有一个是正方形的一条边。而且我们知道正方形只要有一条边确定了,另外两个顶点也就确定了。

typedef vector<int> vi;
class Solution {
    bool isSquare(vi &p1, vi&p2, vi&p3, vi&p4){
        if(p1 == p2) return false;
        int dx = p2[0] - p1[0], dy = p2[1] - p1[1];
        vi p5{p1[0]+dy, p1[1]-dx}, p6{p2[0]+dy, p2[1]-dx};
        if((p5==p3&&p6==p4)||(p5==p4&&p6==p3)) return true;
        p5 = vi{p1[0]-dy, p1[1]+dx};
        p6 = vi{p2[0]-dy, p2[1]+dx};
        if((p5==p3&&p6==p4)||(p5==p4&&p6==p3)) return true;
        return false;
    }
public:
    bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
        return isSquare(p1,p2,p3,p4) || isSquare(p1,p3,p2,p4);
    }
};

592

题目:Fraction Addition and Subtraction

  • Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format.

题解:计算过程非常简单,就是正常分数运算,得到的是分母跟分子,但不一定是 irreducible 的。

typedef pair<int, int> ii;
class Solution {
    int gcd(int x,int y){
        if(x<y) swap(x,y);
        if(!y) return x;
        return gcd(y, x%y);
    }
    ii getFraction(ii f1, ii f2){
        int deno=f1.second*f2.second;
        int nume=f1.second*f2.first+f1.first*f2.second;
        int m = gcd(abs(deno), abs(nume));
        if(deno < 0){
            deno *= -1;
            nume *= -1;
        }
        return pair<int,int>(nume/m, deno/m);
    }
public:
    string fractionAddition(string expression) {
        // implementation omitted for brevity
    }
};

591

题目:Tag Validator

Given a string representing a code snippet, you need to implement a tag validator to parse the code and return whether it is valid.

题解:这题特别容易,只需要熟练运用string的各种操作函数。

class Solution {
public:
    bool isValid(string code) {
        // implementation omitted for brevity
    }
};

588

题目:Design In-Memory File System

Design an in-memory file system to simulate various functions.

题解:题目很长,但逻辑很简单。

class FileSystem {
    // implementation omitted for brevity
};

587

题目:给出一些树,您需要使用最小长度的绳子围住整个花园。

题解:关键是如何熟练运用方向函数和字符串处理。

public class Solution {
    // implementation omitted for brevity
};

586

题目:给出一个记录transaction的表,找出提交订单数量最多的顾客。

题解:按照题意找出count最大值即可。

SELECT customer_number
FROM orders
GROUP BY customer_number
ORDER BY COUNT(order_number) DESC
LIMIT 1;

585

题目:找出符合条件的行,并对TIV_2016这一列求和。

题解:根据要求的两个条件构造filter即可。

SELECT ROUND(sum(TIV_2016), 2) AS TIV_2016
FROM insurance i
WHERE TIV_2015 IN (SELECT TIV_2015 FROM insurance GROUP BY TIV_2015 HAVING COUNT(TIV_2015) > 1)
    AND (LAT,LON) IN (SELECT LAT,LON FROM insurance GROUP BY LAT,LON HAVING COUNT(LAT) = 1);