121 Best Time to Buy and Sell Stock

题目

Say you have an array for which the ith element is the price of a given stock on day i.

If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one.

116 Populating Next Right Pointers in Each Node

题目

You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:

1
2
3
4
5
6
struct Node {
int val;
Node *left;
Node *right;
Node \*next;
}

1071 Greatest Common Divisor of Strings

题目

For strings S and T, we say "T divides S" if and only if S = T + ... + T (T concatenated with itself 1 or more times)

Return the largest string X such that X divides str1 and X divides str2.

RSA加密算法

注: 下文中的 % 符号一律表示取模(mod)操作,而非取余(rem).

取模(Python)

1
2
8 % -5 = -2
-8 % 5 = 2

取余(JavaScript)

1
2
8 % -5 = 3
-8 % 5 = -3

RSA 算法

1 随机选择两个不同的质数 p 和 q.

2 计算 p * q, 记为 n.

3 计算卡迈尔克函数 λ(n).
因为 n = pq, 且 p 和 q 均为质数, 则 λ(n) 为 φ(p) 和 φ(q) 的最小公倍数.
λ(n)=lcm(λ(p),λ(q))=lcm(φ(p),φ(q))=lcm(p1,q1)=/(p1)(q1)/gcd(p1,q1)λ(n) = lcm(λ(p), λ(q)) = lcm(φ(p), φ(q)) = lcm(p -1 , q - 1) = \frac{/(p - 1)(q - 1)/}{gcd(p - 1, q - 1)}

4 随机选择一个整数 e, 满足 1 < e < λ(n) , 并且 e 和 λ(n) 互质.通常会选择 65537 作为 e 的值.

5 计算 e 的模反元素 d, 即 de1 (mod λ(n))de \equiv 1 \ (mod \ λ(n)).

6 将 n 和 e 作为 公钥, n 和 d 作为私钥.

加密: c(m)=me mod nc(m) = m^e \ mod \ n

解密: m=cd mod nm = c^d \ mod \ n

instanceof 与 Object.isPrototypeOf 的区别

Aobj instanceof Bfunction // Bfunction.prototype 是否在 Aobj.__proto__ (A 的原型链)上 ?
Aobj.isPrototypeOf(Bobj) // Aobj 是否在 Bobj.__proto__ (B 的原型链)上?

instanceof 用于判断"左侧的对象"是否是"右侧的构造函数"生成的实例.

相当于:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function instance(Aobj, Bfunction) {
let proto = Aobj.__proto__;
while (proto) {
if (proto === Bfunction.prototype) {
return true;
} else {
proto = proto.__proto__;
}
}
return false;
}

class Parent {}
class Child extends Parent {}
class Grandson extends Child {}

var c = new Child();
instance(c, Parent); // true
instance({}, Parent); // false
instance(c, Object); // true

var g = new Grandson();
instance(g, Parent); // true

class

Es6 开始引入了 class 关键字,其本质是基于构造函数和原型链继承的语法糖.

创建 class

有两种方法创建 class, class declartions(类声明) 和 class expression(类表达式).

class declarations

1
class Foo {}

class declarations 必须有类名,否则会报错 SyntaxError.

1
class {} // SyntaxError: Unexpected token {

Sequelize

Sequelize 是基于 Node 的 ORM 库, 支持的数据库有 Postgres | MySQL | MariaDB, SQLite | Microsoft SQL Server.
Sequelize 将数据库键值对映射成对象, 将增删改查操作映射成方法,抹平了不同数据库间的语法区别.

hello world

安装 Sequelize

1
2
yarn add sequelize
yarn add sqlite3