0%

problem

\50. Pow(x, n)

Medium

Implement pow(x, n), which calculates x raised to the power n (xn).

Example 1:

1
2
Input: 2.00000, 10
Output: 1024.00000

Example 2:

1
2
Input: 2.10000, 3
Output: 9.26100

Example 3:

1
2
3
Input: 2.00000, -2
Output: 0.25000
Explanation: 2-2 = 1/22 = 1/4 = 0.25

Note:

  • -100.0 < x < 100.0
  • n is a 32-bit signed integer, within the range [−231, 231 − 1]

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public double myPow(double x, int n) {

long N = n;
if (N < 0) {
x = 1 / x;
N = -N;
}

double ans = 1;
double cur = x;//2
for (long i = N; i > 0; i /= 2) {
if (i % 2 == 1)
ans = ans * cur;
cur = cur * cur;
}
return ans;

}
//偷懒方法
public double myPow(double x, int n) {
return Math.pow(x, n);
}

key

其实先使用了偷懒的方法,调用Math库的pow方法,然后写过一版

1
2
3
for(long i=N;i>0;i--) {
ans=ans*cur;
}

这个会直接报超时的错误,因为的计算量会非常大,在计算(-1.00000,-2147483648)时候超时了,虽然我们可以通过判断x来避免这一个超时,但是我想到了,可以通过n/2来迅速减少相乘的次数。时间大概是8ms

perfect

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
class Solution {
public double findPower(double x,long n){
if(n == Long.valueOf(1))
return x;
if(n % 2 == 0){
double half_pow = findPower(x,n/2);
return half_pow * half_pow;
}else{
double half_pow = findPower(x,(n-1)/2);
return half_pow * half_pow * x;
}

}
public double myPow(double x, int n) {
if( n==0 )
return 1;

long n_long = (long) n;
if( n > 0 )
return findPower(x,n);

x = 1 / x;
long n_long_abs = (long) Math.abs((long)n);
if(n_long_abs == 1)
return x;
return findPower(x,n_long_abs);
}
}

我在一个不属于我的地方游荡

2019年的3月6日,距离我上一份工作离职,已经37天,日子过得虽然不好,但也不算差,好消息是阿len还陪着我,坏消息是一直过着异地恋的生活,不知道是为什么,是我进入了焦虑的状态,每天的日常就是投简历,思考人生,发呆,看up主秀恩爱

其实回顾前三次的找工作经历,哪次不是觉得自己快要变成咸鱼了,然后收到了一两个offer,不过今年的不同点就是,有三家,我已经过了用人单位的面试,却被卡在了人力资源部门的审核上,我时常恨自己的学历,却无法去原谅曾经高考的自己,写这篇文章的时候,我刚刚从清华的北门进入校园,下午两点半的宿舍区,没有一点噪音,天空的乌鸦鸣叫在空旷的校园回荡,仿佛,在感叹今日的好天气,阳光那么明亮,洒在光秃秃的树枝上。

1552225241907

17年考研复习期间埋下来的雷,最终还是爆炸了,18年,19年,20年,似乎时间过得很快,我丢失了那一次机会后,我似乎再也没有机会去投入身心去复习,每天的大脑里更多的是,好累啊,好烦啊,什么时候发工资啊。越生是怀念起无忧无虑的本科生活,天天不用担心我是谁,我在哪儿,学什么,可能唯一需要费点脑经的就是,中午吃啥

1552225254472

而现在,我走在一个不属于我的世界里,熟悉又陌生,我什么都不知道,因为我不知道我要干嘛,前方一个是找不到工作的工作方向,一个是会饿死的考研方向,世界很精彩,我却显得那么渺小,就深深想起来用人部门发信息和我说:

从技术层面上,我认为从工作年限上,你的水平是够的。对于候选人的学历背景上,央企有自身的痼疾,用人部门的话语权不一定大于人力部门,这个你也无须介怀。

工作的前三年对于一个工程师来说是至关重要的,如果喜欢这条路,就多花点时间,加油!江湖不大,有缘再见!

不知道接下来应该做什么,或许这就是应试教育的悲哀,我也只能许愿,三月份能够拿到一个不错的offer,先活下来,我是子苏,一个快要得抑郁症的人。

what

pom.xml-properties中添加 Docker 镜像名称

1
2
3
<properties>
<docker.image.prefix>springboot</docker.image.prefix>
</properties>

plugins 中添加 Docker 构建插件:

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
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- Docker maven plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<!-- Docker maven plugin -->
</plugins>
</build>

在目录src/main/docker下创建 Dockerfile 文件,Dockerfile 文件用来说明如何来构建镜像。

1
2
3
4
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD spring-boot-docker-1.0.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

这个 Dockerfile 文件很简单,构建 Jdk 基础环境,添加 Spring Boot Jar 到镜像中,简单解释一下:

  • FROM ,表示使用 Jdk8 环境 为基础镜像,如果镜像不是本地的会从 DockerHub 进行下载
  • VOLUME ,VOLUME 指向了一个/tmp的目录,由于 Spring Boot 使用内置的Tomcat容器,Tomcat 默认使用/tmp作为工作目录。这个命令的效果是:在宿主机的/var/lib/docker目录下创建一个临时文件并把它链接到容器中的/tmp目录
  • ADD ,拷贝文件并且重命名
  • ENTRYPOINT ,为了缩短 Tomcat 的启动时间,添加java.security.egd的系统属性指向/dev/urandom作为 ENTRYPOINT

这样 Spring Boot 项目添加 Docker 依赖就完成了。

what

通过dockerfile写入程序、库、资源、配置参数等,来生成image文件,可以类比node的package.json或者nginx.conf的文件

format

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
##  Dockerfile文件格式

# This dockerfile uses the ubuntu image
# VERSION 2 - EDITION 1
# Author: docker_user
# Command format: Instruction [arguments / command] ..

# 1、第一行必须指定 基础镜像信息
FROM ubuntu

# 2、维护者信息
MAINTAINER docker_user docker_user@email.com

# 3、镜像操作指令
RUN echo "deb http://archive.ubuntu.com/ubuntu/ raring main universe" >> /etc/apt/sources.list
RUN apt-get update && apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf

# 4、容器启动执行指令
CMD /usr/sbin/nginx

build image

1
docker build

运行该命令时,根据dockerfile文件及上下文构建新的docker镜像,其中上下文是指dockerfile所在的本地路径或者网络路径url。

ps:dokcer build时候,会在后台守护进程daemon中进行,而不是cli(common line interface)中,构建前,构建进程将全部内容递归放到守护进程,将dockerfile文件放在(本就在空目录下构建)该目录下

还可以通过.dockerignore的文件来忽略上下文目录中的部分文件和目录,同.gitignore

通过-f命令指定文件位置,如:

1
docker buid -f /path/to/dockerfile .

image tag

镜像标签docker build -t ngix/v3

cache

Docker 守护进程会一条一条的执行 Dockerfile 中的指令,而且会在每一步提交并生成一个新镜像,最后会输出最终镜像的ID。生成完成后,Docker 守护进程会自动清理你发送的上下文。 Dockerfile文件中的每条指令会被独立执行,并会创建一个新镜像,RUN cd /tmp等命令不会对下条指令产生影响。 Docker 会重用已生成的中间镜像,以加速docker build的构建速度。

example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
mkdir mynginx
cd mynginx
vi Dockerfile

//制作dokcerfile
FROM nginx
RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html

//save && run this code in mynginx
docker build -t nginx:v1 .
//v1 后面有一个空格和一个点
//点代表当前目录
//查看image
dokcer images

//run
dokcer run --name docker_nginx_v1 -d -p 80:80 nginx:v1
//docker_nginx_v1为容器名
//nginx:v1为image名

why nosql

NoSql可以处理结构化,非结构化的数据,可以水平伸缩,在实时和批量数据分析中具有优势

difference

why docker

  1. 解决“在我的机子上可以正常工作”的问题
  2. 运维更好地管理服务
  3. 更好地迁移和拓展(任意平台运行)

what is docker

docker属于Linux容器的一种封装,和VM类似,但他不像VM一样虚拟在操作系统之上,而是和操作系统平级,程序运行在容器里,就和在真实的物理机上面运行一样

img

简单一点理解就是:程序运行在docker上和真机上几乎误差,将程序包装起来管理

名词解释

  • daemon:守护进程
  • Client:命令行
  • image:镜像,用来创建容器
  • container:运行组件,启动的image就是容器
  • registry:管理image的地方

install

#ubuntu

$ sudo apt-get install docker-ce docker-ce-cli containerd.io

HelloWorld

1
2
3
sudo docker container run hello-world
//他会先找本地,然后再去仓库下载
//该过程将image变成容器,即image文件产生container文件

常用命令

docker pull image_name//拉取镜像

docker images//本地镜像

docker rmi xxx//remove image

docker ps//view what docker is running

docker ps -a

//以下使用cn代替 container_name/container_id

docker start|stop|restart cn

docker attach cn//启动后进入容器

dokcer rm cn

docker info

docker search nginx

16.0正式发布

我们从去年八月开始,努力将我们LineageOs的新特性移植到新版本的安卓上,非常感谢之前版本中的工作者们,我们才能够在这次的版本新特性中投入更多的精力,尤其是,隐私守护(Privacy Guard)和插件(su addon)上收到了了大量的提升建议。
通过对Styles API的一些细微更改,他现在可以兼容安卓暗黑模式的默认实现,在未来,越来越多的三方应用将遵循系统风格,这意味着Styles API将允许在跨应用程序时获得更一致的体验。
正如我们发布夏季第二次调研结果那样,我们将介绍Trust的新特性,首先是设备锁定时阻止新USB设备连接。请注意,由于基于底层,所以这个特性必须在每个设备底层中启用。Trebuchet现在还可以隐藏app以及在打开app前进行身份验证。该限制也仅在Trebuchet中,并非系统范围。
我们认为16.0的分支已经达到了15.1版本的特性测试并做好了发布准备。随着16.0分支成为最新最活跃的分支,在2019.3.1,它将开始日更新构建,并且15.1将会移动到周更新。
16.0版本将会从小部分机器开始运行,一些其他的机子如果准备好了,我们也会做一些小改动,开始构建,并通过改动构建脚本来更好地处理我们最新手机的,独特feature,以及由此产生的复杂问题

支持更新名单

  • Asus
  • BQ
  • Fairphone
  • Google
  • HTC
  • Huawei
  • LeEco
  • Lenovo
  • LG
  • Moto
  • Nextbit
  • Nubia
  • Nvdia
  • OnePlus(my oneplus 5T receive 16.0)
  • Oppo
  • Samsung
  • Sony
  • Wileyfox
  • Wingtech
  • Xiaomi
  • YU
  • ZTE
  • Zuk
  • more

其他热门的ROM

原文

Hello LineageOS 16.0

We’ve been working hard since August to port our unique features to this new version of Android. Thanks to the major cleanup and refactoring done in the previous version, we were able to focus more on features and reliability this time; in particular, both Privacy Guard and the su addon received a sizeable amount of improvements.

With some minor changes made to the Styles API, it is now compatible with what will eventually become the default implementation of dark mode in Android. In the future, more and more third party apps will follow the system style, meaning our Styles API will allow you to have a more coherent experience across apps.

As we announced when the Summer Survey 2 results were posted, we will be introducing new features to Trust, beginning with the ability to block new USB device connections when device is locked. Please note that this feature has to be enabled on a per-device basis due to the layer at which this was implemented. Trebuchet is also now able to hide apps and require authentication before opening them. Please note that this restriction is limited to Trebuchet and is not system-wide.

We feel that the 16.0 branch has reached feature parity with 15.1 and is ready for initial release. With 16.0 being the most recent and most actively-developed branch, on March 1st, 2019 it will begin receiving builds nightly and 15.1 will be moved to weekly builds.

LineageOS 16.0 will be launching with a small selection of devices. Additional devices will begin receiving builds as they are ready and after we make minor change to our build scripts to better handle the unique features, and resulting complications, of the most modern devices.

Upgrading to LineageOS 16.0

  1. (Optional) Make a backup of your important data

  2. Download the build either from

download portal

or built in Updater app

  • You can export the downloaded package from the Updater app to the sdcard by long-pressing it and then selecting “Export” in the popup menu
  1. Download proper addons packages (GApps, su…) for Android 9.0/Lineage OS 16.0

  2. Make sure your recovery and firmware are up to date

  3. Format your system partition

  4. Follow the “Installing LineageOS from recovery” section on your device’s installation page

Please note that if you’re currently on an official build, you DO NOT need to wipe your device.

If you are installing from an unofficial build, you MUST wipe data from recovery before installing.

写在最前面:该文章为笔记,来自纯洁的微笑

what is the loading of class

类加载即:

将编译class文件中的二进制数据读到内存中方法区,然后在堆区通过java.lang.Class实例化对象,对方法区的数据进行操作

img

该加载过程包含首次使用加载,以及预加载

加载class文件的方式

  • 本地
  • 网络
  • zip,jar文件中
  • 数据库
  • 动态编译

类的生命周期

img

problem

In LeetCode Store, there are some kinds of items to sell. Each item has a price.

However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price.

You are given the each item’s price, a set of special offers, and the number we need to buy for each item. The job is to output the lowest price you have to pay for exactly certain items as given, where you could make optimal use of the special offers.

Each special offer is represented in the form of an array, the last number represents the price you need to pay for this special offer, other numbers represents how many specific items you could get if you buy this offer.

You could use any of special offers as many times as you want.

examples

Example 1:

1
2
3
4
5
6
7
Input: [2,5], [[3,0,5],[1,2,10]], [3,2]
Output: 14
Explanation:
There are two kinds of items, A and B. Their prices are $2 and $5 respectively.
In special offer 1, you can pay $5 for 3A and 0B
In special offer 2, you can pay $10 for 1A and 2B.
You need to buy 3A and 2B, so you may pay $10 for 1A and 2B (special offer #2), and $4 for 2A.

Example 2:

1
2
3
4
5
6
7
Input: [2,3,4], [[1,1,0,4],[2,2,1,9]], [1,2,1]
Output: 11
Explanation:
The price of A is $2, and $3 for B, $4 for C.
You may pay $4 for 1A and 1B, and $9 for 2A ,2B and 1C.
You need to buy 1A ,2B and 1C, so you may pay $4 for 1A and 1B (special offer #1), and $3 for 1B, $4 for 1C.
You cannot add more items, though only $9 for 2A ,2B and 1C.

solution

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
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ShoppingOffers {
public static void main(String[] args) {
/*以下贴出测试方式,因为对ArrayList不熟悉,如有更好的方式,欢迎指出*/
List<Integer> price = new ArrayList<Integer>();
List<List<Integer>> special = new ArrayList<List<Integer>>();
List<Integer> needs = new ArrayList<Integer>();
price.add(0, 2);price.add(1, 5);
Integer[][] arr = new Integer[][] {{3,0,5},{1,2,10}};
special.add((List<Integer>)Arrays.asList(arr[0]));
special.add((List<Integer>)Arrays.asList(arr[1]));
needs.add(0,3);needs.add(1,2);
ShoppingOffers so = new ShoppingOffers();
int res = so.shoppingOffers(price, special, needs);
System.out.println(res);
}
public int shoppingOffers(List < Integer > price, List < List < Integer >> special, List < Integer > needs) {
return shopping(price, special, needs);
}
public int shopping(List < Integer > price, List < List < Integer >> special, List < Integer > needs) {
int j = 0, res = dot(needs, price);
for (List < Integer > s: special) {
ArrayList < Integer > clone = new ArrayList < > (needs);
for (j = 0; j < needs.size(); j++) {
int diff = clone.get(j) - s.get(j);
if (diff < 0)
break;
clone.set(j, diff);
}
if (j == needs.size())
res = Math.min(res, s.get(j) + shopping(price, special, clone));
}
return res;
}
public int dot(List < Integer > needs, List < Integer > price) {
int sum = 0;
for (int i = 0; i < needs.size(); i++) {
sum += needs.get(i) * price.get(i);
}
return sum;
}
}

key

本题目采用动态规划的思路,我们带入测试样例1的

1
2
3
4
>Input: [2,5], [[3,0,5],[1,2,10]], [3,2]
>即A=$2,B=$5
>3A=5$,1A+2B=10$
>需购买3A+2B
尝试 price
1:单买 16
2单买302套餐,还差2个B,则先算出2B的res为10,先试305套餐,A买超了,则退出305套餐,此时还有1210套餐,A买多了,退出套餐,两个套餐试完了,得到了单买两个B,$10的套餐,总价就为15元, 15(覆盖16)
3单买1210套餐,还差2A,0B,费用目前10元,先单买2A,费用4元,总价14元,然后先尝试305套餐,发现超,然后再试1210套餐,发现B超了,得到目前最低费用为14元 14(覆盖15)

问题的关键就在于clone的精髓之处,用来记录还需要多少零件的个数,使用递归,进行操作。如果不符合,(如买超了)直接break后,重新计算clone,直到special方法都试完了,然后才返回,如果一直都是break的状态则会返回单买的价格。

perfect

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
33
34
35
36
37
38
39
40
41
42
class Solution {

private Integer res;

public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
res=Integer.MAX_VALUE;
int[] parr=new int[price.size()];
int[] aarr=new int[needs.size()];
for(int i=0;i<parr.length; i++){
parr[i]=price.get(i);
aarr[i]=needs.get(i);
}
findMinimum(special, 0, aarr, parr, 0);
return res;
}

private void findMinimum(List<List<Integer>> special, int curOffer, int[] remain, int[] single, int total){
if(total>=res||curOffer==special.size()) return;
int buyNow=buySingle(remain, single, total);
if(buyNow<res) res=buyNow;
int[] newRemain=remainAfterUse(special.get(curOffer), remain);
if(newRemain!=null) findMinimum(special, curOffer, newRemain, single, total+special.get(curOffer).get(remain.length));
findMinimum(special, curOffer+1, remain, single, total);
}

private int[] remainAfterUse(List<Integer> special, int[] remain){
int[] res=new int[remain.length];
for(int i=0;i<remain.length;i++){
res[i]=remain[i]-special.get(i);
if(res[i]<0) return null;
}
return res;
}

private int buySingle(int[] remain, int[] single, int total){
for(int i=0; i<remain.length; i++){
total+=remain[i]*single[i];
}
return total;
}

}

http2

优势

  • 更有效的网络利用率
  • 引入 HTTP Header 压缩减小报文体积
  • 在同一个连接中支持多路并发
  • 支持 Server Push

多路复用(Multiplexing)

由于HTTP连接,起初有要求限制同一域名下的请求有数量限制,超过则被阻塞,而HTTP2可以发起多重请求,如同时请求样式文件和脚本文件

二进制分帧

img

HTTP/2 通过让所有数据流共用同一个连接,可以更有效地使用 TCP 连接,让高带宽也能真正的服务于 HTTP 的性能提升。

img

http2.0的格式定义更接近tcp层的方式,这张二机制的方式十分高效且精简。length定义了整个frame的开始到结束,type定义frame的类型(一共10种),flags用bit位定义一些重要的参数,stream id用作流控制,剩下的payload就是request的正文了。

server Push

http2.0能通过push的方式将客户端需要的内容预先推送过去

首部压缩(Header Compression)


BigInt


*fs.mkdir 和 fs.mkdirSync 支持递归参数


CLI Flag 自动补全


Windows 安装包优化