hackerrank刷题(代码片段)

gnn0426 gnn0426     2023-01-05     793

关键词:

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;

解析:要输出偶数id的城市名称且不能出现重复,distinct是去重,mod求余函数

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;

解析:通过统计函数进行数据统计并进行运算。

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;

解析:首先查询城市名字,城市长度通过长度排序,升序排序找到最短名字,使用limit来返回第一行数据;

4.Query the list of CITY names starting with vowels (i.e., aeio, 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‘) ;

解析:因为是范围选择所以选择in,不能重复用distinct,截取城市名字的第一个字母并转成小写。

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中的正则表达式,表示匹配到aeiou就提出来,其中有几个定位符:“.”代表任意字符;在中括号外:“^”代表文本开始;“$”文本结束

查询首字母及最后一个字母为(aeiou)时将其变成^[aeiou].*[aeiou]$

查询除了这些字母外,其他作为首字母的城市名字变成^[^aeiou]:中括号内的^表示除了这些之外

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将最后为元音的都排除掉。

 







hackerrank-minimumswaps2(代码片段)

Itisnotahardone,butIstilllearntagoodlessononhowtooptimizemystrategy.Myfirstthoughtwasontherighttrack:doaO(n)scananddoswapstomakesureeachswapcanputonenumberintocorrectbucket.HoweverIwastryingtofindthet 查看详情

c_cpp30天代码hackerrank(代码片段)

查看详情

c_cpp循环检测-hackerrank(代码片段)

查看详情

c_cpp获取节点值-hackerrank(代码片段)

查看详情

c_cpp[hackerrank]预订hackathon2015(代码片段)

查看详情

java来自hackerrank的sherlock和thebeast的答案(代码片段)

查看详情

aknapsackproblem-hackerrank(代码片段)

题意(n(1leqnleq1000))个结点的树,每个结点有重量(w)和价值(v)两个属性,现在给你一个大小为(m(1leqmleq1000))的背包,求能得到的最大价值。注:选取的结点一定是一个连通块。题解待补代码usingnamespacestd;constintN=2005;intn,m;ints[N],v[N],sub... 查看详情

hackerrank不接受我的代码。为什么?(代码片段)

...i<5:print(i**2)i+=1break因此虽然这在Python3.7中有效,但它在Hackerrank中不起作用,因为如果你要输入一个高于5的数字,让我们说7,Hackerrank会输出...0149162536Python输出数字16后就会停止。我怎样才能在Hackerrank中解决这个问题?如果您... 查看详情

pythonhttps://www.hackerrank.com/challenges/maximize-it/problem(代码片段)

查看详情

markdown未来的雇主邀请我做一个hackerrank测试。这是我提出的替代方案。(代码片段)

查看详情

c_cpp从已排序的链表中删除重复值节点-hackerrank(代码片段)

查看详情

text来自https://www.hackerrank.com/challenges/detect-html-links/problem(代码片段)

查看详情

c_cpp总数没有。a和b之间的2的补码表示中的1,包括在内。hackerrank的“2补”问题。(代码片段)

查看详情

codesignal刷题——almostincreasingsequence(代码片段)

Givenasequenceofintegersasanarray,determinewhetheritispossibletoobtainastrictlyincreasingsequencebyremovingnomorethanoneelementfromthearray.ExampleFor sequence=[1,3,2,1],theoutputshouldbealmostIn 查看详情

代码未满足 HackerRank 上所有示例输入的结果

】代码未满足HackerRank上所有示例输入的结果【英文标题】:CodeNotfulfillingalltheSampleInputs\'resultonHackerRank【发布时间】:2021-12-0623:19:28【问题描述】:关于HackerRank的问题-要求您确保护照中的姓名以大写字母开头。例如,alisonheck应... 查看详情

java刷题--2312的幂(代码片段)

java刷题--2312的幂题目代码结果题目代码classSolutionpublicbooleanisPowerOfTwo(intn)returnn>0&&(n&(n-1))==0;//解释一些,2的幂次只有在最高位才是1其余都是0结果 查看详情

java刷题--2312的幂(代码片段)

java刷题--2312的幂题目代码结果题目代码classSolutionpublicbooleanisPowerOfTwo(intn)returnn>0&&(n&(n-1))==0;//解释一些,2的幂次只有在最高位才是1其余都是0结果 查看详情

java刷题--139单词拆分(代码片段)

java刷题--139单词拆分题目代码结果题目代码publicclassSolutionpublicbooleanwordBreak(Strings,List<String>wordDict)Set<String>wordDictSet=newHashSet(wordDict);boolean[]dp=newboolean[s.length()+ 查看详情