ARTS 第2周

ARTS (第2周)

这个是左耳听风写在coolshell上的一篇早期的文章。这是该篇文章的结尾。本周的分享也是这篇文章,在此我用这篇文章作为开篇词。

  • 能够去规划自己的个人发展的人,通常都是有很多机会和可能性的人
  • 有很多机会和可能性的人,通常都是有Leadership,喜欢冒险的人。
  • 有Leadership喜欢冒险的人,通常都是学习能力强,思维活跃,喜欢折腾,懂得“投资”的人。
  • 学习能力强思维活跃的人,通常来说,都是喜欢看书,喜欢实践和新鲜事物,不怕艰难和挑战,用智力而不是使蛮力的人。
  • 懂得“投资”的人,通常来说,他们更多的关注的是未来和长远的成长,而不是当下的KPI、奖金和晋升。

出处: https://coolshell.cn/articles/17583.html

本周:

摩尔投票法

Algorithm 算法

求众数

给定一个大小为 n 的数组,找到其中的众数。众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在众数。

参考链接

https://leetcode-cn.com/problems/majority-element/submissions/

我的第一次的做法

1
2
3
4
5
6
7
8
9
10
11
12
public int majorityElement(int[] nums) {
int len =nums.length;
int half =len/2;
Arrays.sort(nums);
for (int i = 0; i < len; i++) {
//没有众数会报错,题目说一定有众数 非空
if(nums[i]==nums[i+half] ){
return nums[i];
}
}
return -1;//用不到
}

思路就是先排序,然后每个数字,和跨越一半数组的数字去比对,这也是根据三个礼拜做的三数之和的答案来引申出来的。超越了45的用户%。

随后我观看评论,看到了这个答案。

1
2
3
4
public int majorityElement(int[] nums) {
Arrays.sort(nums);
return nums[nums.length/2];
}

对于这个答案,我只想说简单粗暴,排序之后,因为众数超过一半 所以中间的位置,一定就是众数。

对于这个答案,我引申想到了可以用冒泡排序,不断求最大数,当求到中间数字的时候,就可以直接返回答案了。不过提交之后,直接返回说超时,想到冒泡排序的时间复杂度是O(n²),是双重循环,效率可能还不如2个不重叠的循环的做法(一个循环计数,一个循环判断最大值)时间复杂度为O(2n)。

最后看到了一个最好的答案(摩尔投票法)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int majorityElement(int[] nums) {
int count = 1;
int maj = nums[0];
for (int i = 1; i < nums.length; i++) {
if (maj == nums[i])
count++;
else {
count--;
if (count == 0) {
maj = nums[i + 1];
}
}
}
return maj;
}

理解之后思路为:循环数字,用一个个数字统计,发现相同数就加,发现不同数就减,小于0就换下一个数字, 因为这个数字是超过一半的, 所以到最后还留着的就是最多的数字。

Review 英文文章

http://zookeeper.apache.org/

zookeeper官网,主要对zookeeper做了介绍和简介。

Tip 技巧

PS:以下内容学习自尚硅谷

http://www.atguigu.com/

1、Spring boot学习后续-Thymeleaf使用

依赖:

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
2.1.6
</dependency>
<!--如果需要切换thymeleaf版本 -->
<properties>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 -->
<!-- thymeleaf2 layout1-->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>

1、导入thymeleaf的名称空间

1
<html lang="en" xmlns:th="http://www.thymeleaf.org">

2、thymeleaf语法;

所有的th:xxx可以会将默认值替换为自己设置的值。如th:text;改变当前元素里面的文本内容;具体就不一一列举了。

1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<h1>success!</h1>
<!--th:text 可以将div里面的文本内容设置为传入的值 -->
<div th:text="${new_text}">default text</div>
</body>
</html>

3、表达式

1、${…}:获取变量值;如

​ 1)、获取对象的属性、调用方法。

​ 2)、使用内置的基本对象:

​ 3)、内置的一些工具对象:

2、#{…}:获取国际化内容

3、@{…}:定义URL;

4、另外也有如ognl类似的加减等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Literals(字面量)
Text literals: 'one text' , 'Another one!' ,…
Number literals: 0 , 34 , 3.0 , 12.3 ,…
Boolean literals: true , false
Null literal: null
Literal tokens: one , sometext , main ,…
Text operations:(文本操作)
String concatenation: +
Literal substitutions: |The name is ${name}|
Arithmetic operations:(数学运算)
Binary operators: + , - , * , / , %
Minus sign (unary operator): -
Boolean operations:(布尔运算)
Binary operators: and , or
Boolean negation (unary operator): ! , not
Comparisons and equality:(比较运算)
Comparators: > , < , >= , <= ( gt , lt , ge , le )
Equality operators: == , != ( eq , ne )
Conditional operators:条件运算(三元运算符)
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
Special tokens:
No-Operation: _

2、docker的简易使用

主要概念:

docker主机(Host):安装了Docker程序的机器(Docker直接安装在操作系统之上);

docker客户端(Client):连接docker主机进行操作;

docker仓库(Registry):用来保存各种打包好的软件镜像;

docker镜像(Images):软件打包好的镜像;放在docker仓库中;

docker容器(Container):镜像启动后的实例称为一个容器;容器是独立运行的一个或一组应用;

使用Docker的步骤:

1)、安装Docker

1
2
yum install docker
//指令后面输入y确认安装即可

2)、去Docker仓库找到这个软件对应的镜像;

1
docker pull tomcat//垃圾tomcat进行

3)、使用Docker运行这个镜像,这个镜像就会生成一个Docker容器;

4)、对容器的启动停止就是对软件的启动停止;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1、根据镜像启动容器
docker run --name mytomcat -d tomcat:latest
2、docker ps
查看运行中的容器
3、 停止运行中的容器
docker stop 容器的id
4、查看所有的容器
docker ps -a
5、启动容器
docker start 容器id
6、删除一个容器
docker rm 容器id
7、启动一个做了端口映射的tomcat
[root@localhost ~]# docker run -d -p 8888:8080 tomcat
-d:后台运行
-p: 将主机的端口映射到容器的一个端口 主机端口:容器内部的端口

docker的官网地址:

https://hub.docker.com/

Share 分享

https://coolshell.cn/articles/17583.html

这个是左耳听风写在coolshell上的一篇早期的文章。也即是开篇词的出处。