不会飞的章鱼

熟能生巧,勤能补拙,静能生慧;念念不忘,必有回响。

SQL 教程链接:https://www.lintcode.com/learn

LEVEL1:

  • 2045 · Output Hello LintCode

    1
    SELECT "Hello LintCode!";
  • 2013 · Check the name of the teacher

    1
    SELECT `name` FROM `teachers`
  • 2007 · Check course name and class size

    1
    SELECT `name`,`student_count` FROM courses
  • 2009 · Query all teachers

    1
    SELECT * FROM `teachers`
  • 1981 · Check the nationality of all teachers

    1
    SELECT DISTINCT country FROM teachers
  • 2011.Search for information on courses with more than 1000 participants

    1
    SELECT * FROM `courses` WHERE `student_count`>1000
  • 2012 · Find course information for the course named Artificial Intelligence

    1
    SELECT * FROM courses WHERE `name`= 'Artificial Intelligence';
  • 2017 · Inserting SQL course information into the course table

    1
    INSERT INTO courses VALUES (14,"SQL",200,"2021-02-25",1)
  • 2021 · Insert teacher information into the specified column of the teachers table

    1
    2
    INSERT INTO `teachers` (`name`,`email`,`age`,`country`) VALUES
    ('XiaoFu','XiaoFu@lintcode.com',20,'CN');
  • 2020 · Update on the number of students choosing artificial intelligence

    1
    UPDATE `courses` SET `student_count`=500 WHERE `name`='Artificial Intelligence'
  • 2004 · Delete all courses until 2020

    1
    DELETE FROM `courses` WHERE `created_at`<'2020-1-1'
  • 2019 · Delete all rows in the table

    1
    DELETE FROM `courses`

LEVEL2:

  • 1952 · Query teachers over 20 years old

    1
    SELECT * FROM `teachers` WHERE `age`>20
  • 1953 · Query the name of the Chinese teacher

    1
    SELECT `name` FROM `teachers` WHERE `country`='CN'
  • 1957 · Inquire about courses starting before May 2020

    1
    SELECT `name`,`created_at` FROM `courses` WHERE `created_at`>='2020-1-1' AND `created_at`<'2020-5-1'
  • 1958 · Query the courses that meet the conditions taught by the specified teacher

    1
    SELECT * FROM `courses` WHERE `teacher_id`=4 AND `student_count`>500
  • 2001 · Query the course information of ‘Web’ or ‘Big Data’

    1
    SELECT * FROM `courses`WHERE `name`='Web' OR `name`='Big Data';
  • 2040 · Search for courses with an instructor id of less than 3 and more than 800 students

    1
    SELECT * FROM `courses` WHERE (NOT `teacher_id`=3) AND (`student_count`>800);
  • 1960 · Query course information for a specific time

    1
    SELECT * FROM `courses` WHERE created_at IN ('2021-1-1','2021-1-3')
  • 1962 · Query courses with teacher id other than 1 and 3

    1
    SELECT `name` FROM courses WHERE `teacher_id` NOT IN(1,3)
  • 1964 · Query for course information about the number of students within the specified range

    1
    SELECT * FROM `courses` WHERE `student_count` BETWEEN 50 AND 55;
  • 1972 · Inquire about Chinese and Japanese teachers who have e-mail addresses

    1
    SELECT * FROM teachers WHERE (email IS NOT NULL) AND (`country`='CN' OR `country`='JP');
  • 1974 · Query teacher information by email

    1
    SELECT `name`,`email` FROM `teachers` WHERE `email` LIKE '%@qq.com';
  • 1982 · Check the age of teachers and sort them in ascending order

    1
    SELECT DISTINCT `age` FROM `teachers` ORDER BY `age`;
  • 1977 · Sorted by age of Chinese teachers in descending order

    1
    SELECT * FROM `teachers` WHERE `country`='CN' ORDER BY `age` DESC;
  • 1980 · Search for the oldest Chinese teacher

    1
    SELECT * FROM `teachers` WHERE `country`='CN' ORDER BY `age` DESC LIMIT 1;
阅读全文 »

题目链接

322. 零钱兑换

解法一:贪心算法

贪心算法,就是指它的每一步计算作出的都是在当前看起来最好的选择,也就是说它所作出的选择只是在某种意义上的局部最优选择,并不从整体最优考虑。

基本思路:
1,根据问题来建立数学模型,一般面试题会定义一个简单模型;
2,把待求解问题划分成若干个子问题,对每个子问题进行求解,得到子问题的局部最优解;
3,把子问题的局部最优解进行合并,得到最后基于局部最优解的一个解,即原问题的答案。

阅读全文 »

坏味道

缺乏业务含义的命名

错误命名

  • 宽泛的命名
  • 用技术术语命名

命名遵循的原则

阅读全文 »

安装wireshark

1
2
3
4
5
6
7
8
9
10
11
12
# 添加wireshark 下载链接地址,然后更新软件源
sudo apt-add-repository ppa:wireshark-dev/stable
sudo apt-get update

# 在命令行安装wireshark
sudo apt-get install wireshark

# 续配置,新建wireshark 用户组,把当前用户加入wireshark 用户组
sudo groupadd wireshark
sudo chgrp wireshark /usr/bin/dumpcap
sudo chmod 4755 /usr/bin/dumpcap
sudo gpasswd -a your_username wireshark

TCP协议

概念

TCP是一种面向连接(连接导向)的、可靠的基于字节流的传输层通信协议。TCP将用户数据打包成报文段,它发送后启动一个定时器,另一端收到的数据进行确认、对失序的数据重新排序、丢弃重复数据。

阅读全文 »

如何理解“接口隔离原则”?

接口隔离原则的英文翻译是“ Interface Segregation Principle”,缩写为 ISP。
Robert Martin 在 SOLID 原则中是这样定义它的:“Clients should not be forced to depend upon interfaces that they do not use。”直译成中文的话就是:客户端不应该强迫依赖它不需要的接口。其中的“客户端”,可以理解为接口的调用者或者使用者。

理解接口隔离原则的关键,就是理解其中的“接口”二字。在这条原则中,我们可以把“接口”理解为下面三种东西:

  • 一组 API 接口集合
  • 单个 API 接口或函数
  • OOP 中的接口概念

把“接口”理解为一组 API 接口集合

阅读全文 »

如何理解“里式替换原则”?

里式替换原则的英文翻译是:Liskov Substitution Principle,缩写为 LSP。
这个原则最早是在 1986 年由 Barbara Liskov 提出,他是这么描述这条原则的:

If S is a subtype of T, then objects of type T may be replaced with objects of type S, without breaking the program。

在 1996 年,Robert Martin 在他的 SOLID 原则中,重新描述了这个原则,英文原话是这样的:

Functions that use pointers of references to base classes must be able to use objects of derived classes without knowing it。

阅读全文 »

如何理解“对扩展开放、修改关闭”?

开闭原则的英文全称是 Open Closed Principle,简写为 OCP。
它的英文描述是:software entities (modules, classes, functions, etc.) should be open for extension ,but closed for modification。我们把它翻译成中文就是:软件实体(模块、类、方法等)应该“对扩展开放、对修改关闭”。

详细描述:添加一个新的功能应该是,在已有代码基础上扩展代码(新增模块、类、方法等),而非修改已有代码(修改模块、类、方法等)。

例如:这是一段 API 接口监控告警的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Alert {
private AlertRule rule; //存储告警规则,可以自由设置。
private Notification notification; //告警通知类,支持邮件、短信、微信、手机等多种通知渠道。
public Alert(AlertRule rule, Notification notification) {
this.rule = rule;
this.notification = notification;
}

public void check(String api, long requestCount, long errorCount, long durati) {
long tps = requestCount / durationOfSeconds;
if (tps > rule.getMatchedRule(api).getMaxTps()) {
//NotificationEmergencyLevel 表示通知的紧急程度,包括 SEVERE(严重)、URGENCY(紧急)、NORMAL(普通)、TRIVIAL(无关紧要),不同的紧急程度对应不同的发送渠道。
notification.notify(NotificationEmergencyLevel.URGENCY, "...");
}
if (errorCount > rule.getMatchedRule(api).getMaxErrorCount()) {
notification.notify(NotificationEmergencyLevel.SEVERE, "...");
}
}
}
阅读全文 »

如何理解单一职责原则(SRP)?

单一职责原则的英文是 Single Responsibility Principle,缩写为 SRP。
这个原则的英文描述是这样的:A class or module should have a single reponsibility。如果我们把它翻译成中文,那就是:一个类或者模块只负责完成一个职责(或者功能)。

简单理解,就是不要设计大而全的类,要设计粒度小、功能单一的类。
换个角度来讲就是,一个类包含了两个或者两个以上业务不相干的功能,那我们就说它职责不够单一,应该将它拆分成多个功能更加单一、粒度更细的类。

例如:一个类里既包含订单的一些操作,又包含用户的一些操作。而订单和用户是两个独立的业务领域模型,我们将两个不相干的功能放到同一个类中,那就违反了单一职责原则。为了满足单一职责原则,我们需要将这个类拆分成两个粒度更细、功能更加单一的两个类:订单类和用户类。

如何判断类的职责是否足够单一?

阅读全文 »