shell以及sql

This commit is contained in:
游由 2021-07-16 11:04:37 +08:00
parent ab2352a351
commit b40bc1e855
6 changed files with 262 additions and 0 deletions

48
shell/q0192.sh Normal file
View File

@ -0,0 +1,48 @@
# 192.统计词频
#
# [原题链接](https://leetcode-cn.com/problems/word-frequency/)
#
# 2021-07-16 09:23:44
#
# 写一个 bash 脚本以统计一个文本文件 words.txt 中每个单词出现的频率。
#
# 为了简单起见,你可以假设:
#
#
# words.txt只包括小写字母和 ' ' 。
# 每个单词只由小写字母组成。
# 单词间由一个或多个空格字符分隔。
#
#
# 示例:
#
# 假设 words.txt 内容如下:
#
# the day is sunny the the
# the sunny is is
#
#
# 你的脚本应当输出(以词频降序排列):
#
# the 4
# is 3
# sunny 2
# day 1
#
#
# 说明:
#
#
# 不要担心词频相同的单词的排序问题,每个单词出现的频率都是唯一的。
# 你可以使用一行 Unix pipes 实现吗?
#
# Related Topics Shell
#
# 👍 149 👎 0
# 0ms
cat words.txt | awk -F '[ ]' '{for(i=1;i<=NF;i++){if($i!=""){a[$i]++}}}END{for(i in a){print i,a[i]| "sort -nr -k2"}}'
# 4ms
tr -s ' ' '\n' <words.txt | sort | uniq -c |sort -r| awk '{print $2 " " $1}'
cat words.txt |tr -cs "[a-z][A-Z]" "\n" |tr A-Z a-z |sort|uniq -c|sort -r|awk '{print $2,$1}'

35
shell/q0193.sh Normal file
View File

@ -0,0 +1,35 @@
# 193.有效电话号码
#
# [原题链接](https://leetcode-cn.com/problems/valid-phone-numbers/)
#
# 2021-07-16 09:45:45
#
# 给定一个包含电话号码列表(一行一个电话号码)的文本文件 file.txt写一个单行 bash 脚本输出所有有效的电话号码。
#
# 你可以假设一个有效的电话号码必须满足以下两种格式: (xxx) xxx-xxxx 或 xxx-xxx-xxxx。x 表示一个数字)
#
# 你也可以假设每行前后没有多余的空格字符。
#
#
#
# 示例:
#
# 假设 file.txt 内容如下:
#
#
# 987-123-4567
# 123 456 7890
# (123) 456-7890
#
#
# 你的脚本应当输出下列有效的电话号码:
#
#
# 987-123-4567
# (123) 456-7890
#
# Related Topics Shell
# 👍 78 👎 0
grep -P '^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$' file.txt

34
shell/q0194.sh Normal file
View File

@ -0,0 +1,34 @@
# 194.转置文件
#
# [原题链接](https://leetcode-cn.com/problems/transpose-file/)
#
# 2021-07-16 10:06:30
#
# 给定一个文件 file.txt转置它的内容。
#
# 你可以假设每行列数相同,并且每个字段由 ' ' 分隔。
#
# 示例:
#
# 假设 file.txt 文件内容如下:
#
#
# name age
# alice 21
# ryan 30
#
#
# 应当输出:
#
#
# name alice ryan
# age 21 30
#
# Related Topics Shell
#
# 👍 52 👎 0
COUNT=`head -1 file.txt | wc -w`
for (( i = 1; i <= $COUNT; i++ )); do
awk -v arg=$i '{print $arg}' file.txt | xargs
done

38
shell/q0195.sh Normal file
View File

@ -0,0 +1,38 @@
# 195.第十行
#
# [原题链接](https://leetcode-cn.com/problems/tenth-line/)
#
# 2021-07-16 10:07:01
#
# 给定一个文本文件 file.txt请只打印这个文件中的第十行。
#
# 示例:
#
# 假设 file.txt 有如下内容:
#
# Line 1
# Line 2
# Line 3
# Line 4
# Line 5
# Line 6
# Line 7
# Line 8
# Line 9
# Line 10
#
#
# 你的脚本应当显示第十行:
#
# Line 10
#
#
# 说明:
# 1. 如果文件少于十行,你应当输出什么?
# 2. 至少有三种不同的解法,请尝试尽可能多的方法来解题。
# Related Topics Shell
#
# 👍 90 👎 0
awk 'NR==10' file.txt
sed -n "10p" file.txt

58
sql/q0175.md Normal file
View File

@ -0,0 +1,58 @@
# 175.组合两个表
[原题链接](https://leetcode-cn.com/problems/combine-two-tables/)
2021-07-16 10:18:48
## SQL架构:
```mysql
Create table Person (PersonId int, FirstName varchar(255), LastName varchar(255));
Create table Address (AddressId int, PersonId int, City varchar(255), State varchar(255));
Truncate table Person;
insert into Person (PersonId, LastName, FirstName) values ('1', 'Wang', 'Allen');
Truncate table Address;
insert into Address (AddressId, PersonId, City, State) values ('1', '2', 'New York City', 'New York');
```
### 表1: Person
| 列名 | 类型 |
|-------------|---------|
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
`PersonId` 是上表主键
### 表2: Address
| 列名 | 类型 |
|-------------|---------|
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
`AddressId` 是上表主键
## 题
编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:
```text
FirstName, LastName, City, State
```
Related Topics 数据库
👍 879 👎 0
## 解
```mysql
SELECT `Person`.`FirstName`, `Person`.`LastName`, `Address`.`City`, `Address`.`State`
FROM `Person`
LEFT JOIN `Address`
ON `Person`.`PersonId`=`Address`.`PersonId`;
```

49
sql/q0176.md Normal file
View File

@ -0,0 +1,49 @@
# 176.第二高的薪水
[原题链接](https://leetcode-cn.com/problems/second-highest-salary/)
2021-07-16 10:48:18
## SQL架构:
```mysql
Create table If Not Exists Employee (Id int, Salary int);
Truncate table Employee;
insert into Employee (Id, Salary) values ('1', '100');
insert into Employee (Id, Salary) values ('2', '200');
insert into Employee (Id, Salary) values ('3', '300');
```
## 题
编写一个 `SQL` 查询,获取 `Employee` 表中第二高的薪水Salary
| Id | Salary |
|----|--------|
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
例如上述 `Employee`SQL查询应该返回 `200` 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 `null`
| SecondHighestSalary |
|---------------------|
| 200 |
Related Topics 数据库
👍 830 👎 0
## 解
```mysql
SELECT
IFNULL(
(SELECT DISTINCT `Salary`
FROM `Employee`
ORDER BY `Salary` DESC
LIMIT 1 OFFSET 1),
NULL) AS SecondHighestSalary;
```