日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

hackerrank刷题

發(fā)布時(shí)間:2023/12/20 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hackerrank刷题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.問題:Query a list of?CITY?names from?STATION?with even?ID?numbers only. You may print the results in any order, but must exclude duplicates from your answer.

答案:SELECT DISTINCT city FROM station WHERE MOD(id,2)=0;

解析:要輸出偶數(shù)id的城市名稱且不能出現(xiàn)重復(fù),distinct是去重,mod求余函數(shù)

2.Let N?be the number of?CITY?entries in?STATION, and let N`?be the number of distinct?CITY?names in?STATION; query the value of? N-N`?from?STATION. In other words, find the difference between the total number of?CITY?entries in the table and the number of distinct?CITY?entries in the table.

答案:SELECT COUNT(city)-COUNT(DISTINCT city) FROM station;

解析:通過統(tǒng)計(jì)函數(shù)進(jìn)行數(shù)據(jù)統(tǒng)計(jì)并進(jìn)行運(yùn)算。

3.Query the two cities in?STATION?with the shortest and longest?CITY?names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.

答案:

SELECT CITY, LENGTH(CITY)
FROM STATION ORDER BY LENGTH(CITY),CITY LIMIT 1;
SELECT CITY, LENGTH(CITY)
FROM STATION ORDER BY LENGTH(CITY) DESC,CITY LIMIT 1;

解析:首先查詢城市名字,城市長(zhǎng)度通過長(zhǎng)度排序,升序排序找到最短名字,使用limit來(lái)返回第一行數(shù)據(jù);

4.Query the list of?CITY?names starting with vowels (i.e.,?a,?e,?i,?o, or?u) from?STATION. Your result?cannot?contain duplicates.

答案:

SELECT DISTINCT CITY
FROM STATION
WHERE LOWER(SUBSTR(CITY,1,1)) in('a','e','i','o','u') ;

解析:因?yàn)槭欠秶x擇所以選擇in,不能重復(fù)用distinct,截取城市名字的第一個(gè)字母并轉(zhuǎn)成小寫。

5.Query the list of?CITY?names ending with vowels (a, e, i, o, u) from?STATION. Your result?cannot?contain duplicates.

答案:

SELECT DISTINCT CITY FROM STATION
WHERE CITY REGEXP '[aeiou]$';

解析:regexp是mysql中的正則表達(dá)式,表示匹配到aeiou就提出來(lái),其中有幾個(gè)定位符:“.”代表任意字符;在中括號(hào)外:“^”代表文本開始;“$”文本結(jié)束

查詢首字母及最后一個(gè)字母為(aeiou)時(shí)將其變成^[aeiou].*[aeiou]$

查詢除了這些字母外,其他作為首字母的城市名字變成^[^aeiou]:中括號(hào)內(nèi)的^表示除了這些之外

6.Query the list of?CITY?names from?STATION?that?do not end?with vowels. Your result cannot contain duplicates.

答案:select distinct city from station where city not regexp '[aeiou]$';

分析:用not將最后為元音的都排除掉。

?

轉(zhuǎn)載于:https://www.cnblogs.com/gnn0426/p/9672274.html

總結(jié)

以上是生活随笔為你收集整理的hackerrank刷题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。