Skip to content

Commit

Permalink
new update for learning note
Browse files Browse the repository at this point in the history
  • Loading branch information
rbmonster committed Jan 27, 2021
1 parent 40e040e commit 7795c21
Show file tree
Hide file tree
Showing 15 changed files with 698 additions and 115 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
- [数据库读写分离](https://github.com/rbmonster/learning-note/blob/master/src/main/java/com/toc/READ_WRITE_DB.md)
- [数据库表相关设计题](https://github.com/rbmonster/learning-note/blob/master/src/main/java/com/toc/TABLE_DESIGN.md)
- [会议系统设计](https://github.com/rbmonster/learning-note/blob/master/src/main/java/com/toc/MEETING_DESIGN.md)
- [设计及架构思想](https://github.com/rbmonster/learning-note/blob/master/src/main/java/com/toc/SYSTEM-DESIGN.md)

## 分布式与微服务
- [Spring Cloud相关知识](https://github.com/rbmonster/learning-note/blob/master/src/main/java/com/toc/SPRING-CLOUD.md)
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/learning/algorithm/ALGORITHM.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,3 +573,12 @@ public int[] maxSlidingWindow(int[] nums, int k) {
它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。
### 贪心算法
[CS-Note](http://www.cyc2018.xyz/%E7%AE%97%E6%B3%95/Leetcode%20%E9%A2%98%E8%A7%A3/Leetcode%20%E9%A2%98%E8%A7%A3%20-%20%E8%B4%AA%E5%BF%83%E6%80%9D%E6%83%B3.html#_1-%E5%88%86%E9%85%8D%E9%A5%BC%E5%B9%B2)
1. [分配饼干](https://leetcode-cn.com/problems/assign-cookies/description/)
- 每次分配给满足孩子的最小的饼干
2. [不重叠的区间个数](https://leetcode-cn.com/problems/non-overlapping-intervals/)
- 每次取结尾最小的区间,留给后面选择的区间更大,移除的区间越小。按区间结尾排序。
3. [投飞镖刺破气球](https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/)
- 与上述类似,每次取结尾最小的区间,
32 changes: 32 additions & 0 deletions src/main/java/com/learning/algorithm/greedy/ChildCookies.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.learning.algorithm.greedy;

import java.util.Arrays;

/**
* <pre>
* @Description:
*
* </pre>
*
* @version v1.0
* @ClassName: ChildCookies
* @Author: sanwu
* @Date: 2021/1/24 16:21
*/
public class ChildCookies {

public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int i = 0, j = 0;
int len1 = g.length;
int len2 = s.length;
while(i < len1 && j < len2) {
if(g[i] <= s[j]){
i++;
}
j++;
}
return i;
}
}
39 changes: 39 additions & 0 deletions src/main/java/com/learning/algorithm/greedy/FindMinArrowShots.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.learning.algorithm.greedy;

import java.util.Arrays;

/**
* <pre>
* @Description:
*
* </pre>
*
* @version v1.0
* @ClassName: FindMinArrowShots
* @Author: sanwu
* @Date: 2021/1/24 17:06
*/
public class FindMinArrowShots {

public static void main(String[] args) {
int[][] point = new int[][] {{9,12},{1,10},{4,11},{8,12},{3,9},{6,9},{6,7}};
new FindMinArrowShots().findMinArrowShots(point);
}

public int findMinArrowShots(int[][] points) {
if(points == null || points.length == 0) {
return 0;
}
Arrays.sort(points, (o1, o2)-> o1[1]<=o2[1]? -1: 1);
int end = points[0][1];
int res = 1;
for(int i = 1; i< points.length; i++) {
if(end >= points[i][0]) {
continue;
}
end = points[i][1];
res++;
}
return res;
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/learning/algorithm/greedy/NoRepeatSeq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.learning.algorithm.greedy;

import java.util.Arrays;

/**
* <pre>
* @Description:
*
* </pre>
*
* @version v1.0
* @ClassName: NoRepeatSeq
* @Author: sanwu
* @Date: 2021/1/24 16:40
*/
public class NoRepeatSeq {

public static void main(String[] args) {
int[][] intervals = new int[][] {{1,100},{11,22},{1,11},{2,12}};
new NoRepeatSeq().eraseOverlapIntervals(intervals);
}

public int eraseOverlapIntervals(int[][] intervals) {
if(intervals == null || intervals.length ==0) {
return 0;
}
Arrays.sort(intervals, (o1, o2) -> o1[1]<=o2[1] ? -1: 1);
int end = intervals[0][1];
int remove = 0;
for (int i = 1; i < intervals.length; i++) {
if (end<=intervals[i][0]){
end = intervals[i][1];
} else {
remove++;
}
}
return remove;

}
}
17 changes: 17 additions & 0 deletions src/main/java/com/learning/basic/JVM.md
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,23 @@ public static final int v = 8080;
5. 上数工作完成之后,java开始调用对象的构造函数。


## JDK编译期

### 编译期做的工作

1. 默认构造器: 经过编译的代码,可以看到在编译阶段,如果我们没有添加构造器。那么Java编译器会为我们添加一个无参构造方法。
2. 自动拆装箱
3. 泛型与类型擦除
4. foreach优化成Iterator
5. `String... args` 可变参数优化
6. switch支持case使用字符串及枚举类型优化,优化成hashcode匹配。
7. 枚举,优化成final class
8. try-with-resources 优化,自动在finally中加入close语句
9. 重写的优化,子类重写方法中会新增一个桥接方法。
10. 匿名内部类:生成final 修饰的类

- 相关资料: [Java编译期处理](https://blog.csdn.net/gyhdxwang/article/details/104396476)

## 堆内存的设置要点
1. 新生代的内存大小设置建议:Sun官方推荐配置为整个堆的3/8。
2. 服务器的内存需要预留一部分给永久代、线程栈及NIO
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/learning/basic/java/basictest/Chicken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.learning.basic.java.basictest;

/**
* <pre>
* @Description:
*
* </pre>
*
* @version v1.0
* @ClassName: Chicken
* @Author: sanwu
* @Date: 2021/1/27 10:36
*/
public class Chicken extends Food {

@Override
public Food cook() {
Food food = new Chicken();
return food;
}

public static void main(String[] args) {
new Chicken().cook();
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/learning/basic/java/basictest/Food.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.learning.basic.java.basictest;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
* <pre>
* @Description:
*
* </pre>
*
* @version v1.0
* @ClassName: Food
* @Author: sanwu
* @Date: 2021/1/27 10:35
*/
@Slf4j
@Data
public class Food {

private String name;
private int id;

public Food cook() {
log.info("this is food cook");
return new Food();
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/toc/ALGORITHM.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
&emsp;<a href="#26">11. 单调栈</a>
&emsp;<a href="#27">12. 单调队列</a>
&emsp;<a href="#28">13. 前缀树</a>
&emsp;&emsp;<a href="#29">13.1. 贪心算法</a>
# <a name="0">算法</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>

## <a name="1">哈希表</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
Expand Down Expand Up @@ -604,3 +605,12 @@ public int[] maxSlidingWindow(int[] nums, int k) {
它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。
### <a name="29">贪心算法</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
[CS-Note](http://www.cyc2018.xyz/%E7%AE%97%E6%B3%95/Leetcode%20%E9%A2%98%E8%A7%A3/Leetcode%20%E9%A2%98%E8%A7%A3%20-%20%E8%B4%AA%E5%BF%83%E6%80%9D%E6%83%B3.html#_1-%E5%88%86%E9%85%8D%E9%A5%BC%E5%B9%B2)
1. [分配饼干](https://leetcode-cn.com/problems/assign-cookies/description/)
- 每次分配给满足孩子的最小的饼干
2. [不重叠的区间个数](https://leetcode-cn.com/problems/non-overlapping-intervals/)
- 每次取结尾最小的区间,留给后面选择的区间更大,移除的区间越小。按区间结尾排序。
3. [投飞镖刺破气球](https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/)
- 与上述类似,每次取结尾最小的区间,
47 changes: 44 additions & 3 deletions src/main/java/com/toc/COLLECTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
&emsp;&emsp;<a href="#26">3.2. LinkedHashSet实现</a>
&emsp;&emsp;<a href="#27">3.3. TreeSet 实现</a>
&emsp;<a href="#28">4. 迭代器</a>
&emsp;<a href="#29">5. 其他</a>
&emsp;<a href="#29">5. 其他面试问题</a>
&emsp;&emsp;<a href="#30">5.1. 如何选用集合?</a>
&emsp;&emsp;<a href="#31">5.2. ArrayList 带参数及不带参数</a>
&emsp;&emsp;<a href="#32">5.3. Arrays.asList() 方法</a>
# <a name="0">集合</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
![avatar](https://github.com/rbmonster/learning-note/blob/master/src/main/java/com/learning/basic/picture/collectionfamily.jpg)
- 集合主要分为两大类,一个实现collection接口的,一个是实现了Map接口的。
Expand Down Expand Up @@ -518,7 +520,46 @@ Iterator 对象称为迭代器(设计模式的一种),迭代器可以对
Iterator 主要是用来遍历集合用的,它的特点是更加安全,因为它可以确保,在当前遍历的集合元素被更改的时候,就会抛出 ConcurrentModificationException 异常。
## <a name="29">其他</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
## <a name="29">其他面试问题</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
### <a name="30">如何选用集合?</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
- 主要根据集合的特点来选用,比如我们需要根据键值获取到元素值时就选用 Map 接口下的集合,需要排序时选择 TreeMap,不需要排序时就选择 HashMap,需要保证线程安全就选用 ConcurrentHashMap。
- 当我们只需要存放元素值时,就选择实现Collection 接口的集合,需要保证元素唯一时选择实现 Set 接口的集合比如 TreeSet 或 HashSet,不需要就选择实现 List 接口的比如 ArrayList 或 LinkedList,然后再根据实现这些接口的集合的特点来选用。
- 当我们只需要存放元素值时,就选择实现Collection 接口的集合,需要保证元素唯一时选择实现 Set 接口的集合比如 TreeSet 或 HashSet,不需要就选择实现 List 接口的比如 ArrayList 或 LinkedList,然后再根据实现这些接口的集合的特点来选用。
### <a name="31">ArrayList 带参数及不带参数</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
```
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}

/**
* Constructs an empty list with an initial capacity of ten.
*/
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
```
### <a name="32">Arrays.asList() 方法</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
Arrays.asList() 方法返回的是Arrays内部的ArrayList,这个ArrayList不支持元素新增及删除,因为未重写抽象父类AbstractList的方法,会抛出UnsupportedOperationException异常。
```
public class Arrays{
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
private static final long serialVersionUID = -2764017481108945198L;
private final E[] a;

ArrayList(E[] array) {
a = Objects.requireNonNull(array);
}
...
}
}
```
Loading

0 comments on commit 7795c21

Please sign in to comment.