1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

// File: twelve_biubiu.c
// Permission: CN-2082-2
// Author: Li.YiYi
// Dept: PE-362, UG
// Origin: TI-352132
// 春节十二响 biu biu biu!

#env "planet_engine"

int init() {
set_engine_number_mask(ENGINE_ALL);
set_funeral_level(FUNERAL_FULL);
// 允许误差10秒以内
if (unix_time() < make_unix_time(2082, 1, 28, 23, 59, 60-10)) return ERR_ENGIN_ENV;
return engine_check_init(); // after compile and before real run
}
int main() {
set_curve(CURVE_NATURAL); // 自然曲线耗费燃料最少
for (int i :range(0, 12, 1)) {
engine_start();
wait_engine(ENGINE_STATE_CHAGNE);
sleep(2000);
engin_stop();
wait_engine(ENGINE_STATE_CHAGNE);
sleep(4000); // 这个时长在模拟器里听起来更像心跳
}
return 0;
}
int final() {
engine_ensure_shutdown();
}

悲观锁:先锁后用

每次读数据都悲观认为会被其他操作修改,应用于synchroized , ReentrantLock,因为悲观所以开销大,会阻塞其他线程

乐观锁:先用后判断

每次读数据乐观认为没有被其他操作修改,应用于java.util.concurrent.atomic,使用版本号和CAS算法实现

适用于多读的应用类型,提高吞吐量

公平锁:多个线程按申请所顺序取锁

非公平锁

多个线程不按申请顺序取锁,提高吞吐量

可入锁

外层使用锁后,内层仍可以使用,而且不会死锁

不可重入锁

独享锁

共享锁

互斥锁

读写锁

分段锁

偏向锁

轻量级锁

重量级锁

自旋锁

problem

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.

  • push(x) – Push element x onto stack.
  • pop() – Removes the element on top of the stack.
  • top() – Get the top element.
  • getMin() – Retrieve the minimum element in the stack.

Example:

1
2
3
4
5
6
7
8
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.

problem

  1. Find Minimum in Rotated Sorted Array II

Hard

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

Find the minimum element.

The array may contain duplicates.

Example 1:

1
2
>Input: [1,3,5]
>Output: 1

Example 2:

1
2
>Input: [2,2,2,0,1]
>Output: 0

Note:

key

???

solution

1
2
3
4
5
6
7
8
9
10
class Solution {
public int findMin(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] > nums[i + 1]) {
return nums[i + 1];
}
}
return nums[0];
}
}

perfect

problem

  1. Find Minimum in Rotated Sorted Array

Medium

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

Find the minimum element.

You may assume no duplicate exists in the array.

Example 1:

1
2
>Input: [3,4,5,1,2] 
>Output: 1

Example 2:

1
2
>Input: [4,5,6,7,0,1,2]
>Output: 0

key

solution

1
2
3
4
5
6
7
8
public int findMin(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] > nums[i + 1]) {
return nums[i + 1];
}
}
return nums[0];
}

perfect

1
2
I'm the perfect
but this problem will harder in the next problem

分类

  • XSS 攻击:对 Web 页面注入脚本,使用 JavaScript 窃取用户信息,诱导用户操作。
  • CSRF 攻击:伪造用户请求向网站发起恶意请求。
  • 钓鱼攻击:利用网站的跳转链接或者图片制造钓鱼陷阱。
  • HTTP参数污染:利用对参数格式验证的不完善,对服务器进行参数注入攻击。
  • 远程代码执行:用户通过浏览器提交执行命令,由于服务器端没有针对执行函数做过滤,导致在没有指定绝对路径的情况下就执行命令。

XSS攻击

cross-site-scripting跨域脚本攻击

what

MYSQL事务主要用于保证一串事情要么都成功,要么就回滚,例如付款后,要先写入支付订单表,再个人信息中加入会员权益。这两个操作要么顺序执行成功,要么就回滚

原则

ACID

  • Atomicity原子性

    确保事务内的所有操作都成功完成,否则事务将被中止在故障点,以前的操作将回滚到以前的状态。

  • Consistency一致性

    数据库的修改是一致的

  • Isolation隔离性

    事务是彼此独立的

  • Durability可靠性

    确保事务提交后,结果永久存在

隔离性

隔离性可以防止多个事务并发执行时由于交叉执行而导致数据的不一致。事务隔离分为不同级别,包括

读未提交(Read uncommitted)–不严格

读提交(read committed)

可重复读(repeatable read)–默认级别(避免幻读)

串行化(Serializable)–最严格

没有隔离性的问题

1.脏读

1
2
update account set money=money+100 where name=’B’;
update account set money=money - 100 where name=’A’;

当执行第一条语句的时候,事务没有提交,那么来读B的账户钱都多了100块

脏读:读取了另一个事务未提交的数据

2.不可重复读

情景:多次读同一个数据的时候,这个数据被别人改了,导致结果不一致

3.幻读

幻读和不可重复读一样,读取到了另外一条已经提交的事务,所不同的是它针对的是一批数据的整体

实现方式

自动方式

beginTransactionScope(scope, ctx)

1
2
3
4
5
6
7
const result = await app.mysql.beginTransactionScope(async conn => {
// don't commit or rollback by yourself
await conn.insert(table, row1);
await conn.update(table, row2);
return { success: true };
}, ctx);
// if error throw on scope, will auto rollback

手动方式

beginTransaction

1
2
3
4
5
6
7
8
9
10
const conn = await app.mysql.beginTransaction(); // 初始化事务
try {
await conn.insert(table, row1); // 第一步操作
await conn.update(table, row2); // 第二步操作
await conn.commit(); // 提交事务
} catch (err) {
// error, rollback
await conn.rollback(); // 一定记得捕获异常后回滚事务!!
throw err;
}

表达式Literal

app.mysql.literals.now

查看数据库事务隔离性级别

1
select @@tx_isolation;

where

通常在home目录下的一个隐藏文件,访问可以

1
vim ~/.bashrc

what

bash 在每次启动时都会加载 .bashrc 文件的内容。每个用户的 home 目录都有这个 shell 脚本。它用来存储并加载你的终端配置和环境变量

end

1
2
//更新修改
source ~/.bashrc

安装

sudo apt-get update

sudo apt-get install mysql-server

解决远程连接

tips本人使用环境ubuntu16

完成安装后,远程连接你会发现2003报错,此时,你对 /etc/mysql/mysql.conf.d/ 文件夹中打开 mysqld.cnf文件修改即可

修改内容将#bind-address = 127.0.0.1 原本没有注释,进行注释

然后你重新远程连接mysql直接变成1130的拒绝访问服务,接下来你要在服务器端登录mysql,执行

进入数据库

mysql -u root -p

切换数据库,

mysql>use mysql;

查看root账号的登录权限,

mysql>select host, user from user;

修改登录权限

mysql>update user set host = ‘%’ where user = ‘root’;

刷新,生效,最后一步,至关重要

mysql>flush privileges;

what is MQ

如果想知道MQ的详细知识可以看我之前的为什么使用消息队列MQ

这里选择最重要的提一下:MQ即消息队列,用来实现程序的异步和解耦,起到消息缓冲,消息分发。通俗来讲就是一个医院(服务器)里面有多个医生(线程或进程),让病人都排队(消息缓冲),有的去A部门,有的去B部门(消息分发)。

成员RabbitMQ

RabbitMQ是实现AMQP(高级消息队列协议Advanced Message Queuing Protocol)的消息中间件的一种,Feature就是组件之间解耦,病人排他的队,医生看他的病人,至于怎么排,医生不用操心,至于怎么看病,病人不用操心,都交给MQ

术语:面向消息,队列,路由(点对点/发布订阅),可靠安全

0%