ARTS
Algorithm:每周至少做一个leetcode的算法题;
Review:阅读并点评至少一篇英文技术文章;
Tip/Tech:学习至少一个技术技巧;
Share:分享一篇有观点和思考的技术文章;
Algorithm
21. 合并两个有序链表
https://leetcode-cn.com/problems/merge-two-sorted-lists/
这道题的具体思路是这样的
(1)判断边界条件,如果有空的情况,直接返回另一个
(2)新建节点,作为返回的新的链表
(3)开始同时遍历两个链表,循环的条件就是只要两个链表中没有到最后都要进行继续循环
(4)遍历的时候比较在两个节点的值的大小,按照条件插入新的链表中。
Talk is cheap, show the code:
class Solution {
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
} else if (l2 == null) {
return l1;
}
int line1Num = l1.val;
int line2Num = l2.val;
ListNode head;
if (line1Num > line2Num) {
head = new ListNode(line2Num);
l2 = l2.next;
if (l2 != null) {
line2Num = l2.val;
}
} else {
head = new ListNode(line1Num);
l1 = l1.next;
if (l1 != null) {
line1Num = l1.val;
}
}
ListNode node = head;
while (l1 != null || l2 != null) {
if (l1 == null) {
head.next = new ListNode(line2Num);
head = head.next;
l2 = l2.next;
if (l2 != null) {
line2Num = l2.val;
}
} else if (l2 == null) {
head.next = new ListNode(line1Num);
head = head.next;
l1 = l1.next;
if (l1 != null) {
line1Num = l1.val;
}
} else if (line1Num > line2Num){
head.next = new ListNode(line2Num);
head = head.next;
l2 = l2.next;
if (l2 != null) {
line2Num = l2.val;
}
} else {
head.next = new ListNode(line1Num);
head = head.next;
l1 = l1.next;
if (l1 != null) {
line1Num = l1.val;
}
}
}
return node;
}
}
以上就是代码,非常的简单的递归吧,我发现二叉树这些题目是锻炼递归思维非常好的题目,各种的递归翻转,只要熟练,基本上就会对递归有更加熟练地理解了。
88. 合并两个有序数组
https://leetcode-cn.com/problems/merge-sorted-array/
这个思想真的牛逼,从两个最后的位置开始插入排序,其实具体有点像归并的思想吧,具体的思想是看了资料才知道的。
class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int index = n + m - 1;
int index1 = m - 1;
int index2 = n -1;
// 这个循环中就是判断出,如果说最小在nums1中,那么基本上就是在常规的排序,把nums2中的数据
// 按照顺序大小的排序。nums1中的已经排好序了就在nums1中
for (; index >= 0; index--) {
if (index1 == -1 || index2 == -1) {
break;
}
if (nums1[index1] > nums2[index2]) {
nums1[index] = nums1[index1];
index1--;
} else {
nums1[index] = nums2[index2];
index2--;
}
}
// 如果最小的在nums2中的话,那么就是需要往nums中继续添加
for (; index2 >= 0; index2--) {
nums1[index2] = nums2[index2];
}
}
}
Review
https://shipilev.net/jvm/anatomy-quarks/1-lock-coarsening-for-loops/
JVM Anatomy Quark #1: Lock Coarsening and Loops
标题就是锁粗化和循环
我是真的。。。看到的脑子大。
现弄清楚什么是锁的粗化:根据文章的中的给出的例子就是大概这种
Hotspot 确实进行了锁粗化优化,可以有效合并几个相邻同步块,从而降低锁开销。
synchronized (obj) {
// 语句 1
}
synchronized (obj) {
// 语句 2
}
可以被转化为:
synchronized (obj) {
// 语句 1
// 语句 2
}
这就是一种优化的方式,然后作者提出了如果是循环呢?JVM会不会这么智能把循环也给优化了?就像下面这种效果:
for (...) {
synchronized (obj) {
// something
}
}
转化为:
synchronized (this) {
for (...) {
// something
}
}
毕竟上面是两个相邻的代码块可以这样优化,但是循环又没有这种相邻的代码块的功能,所以,作者提出了疑问,接着去测试了一下,有兴趣的朋友可以看一下全程,原文章里有,我这里就重复了,主要是后面的那个分析代码的流程是真的没看懂啊,看的十分郁闷。。。
最后的结论就是,不会!!!但是却有另一种的循环优化可以进行——循环展开的优化方式会提供常规锁粗化。
Tip/Tech
学习了优先队列,好吧,我现在知道原来Java里面有这种的数据结构。
具体的关键字可以搜索 优先队列,最小堆,最大堆这种个,用来解决比如像第K大的元素这种问题贼有用。
Share
https://news.cgtn.com/event/2019/whorunschina/index.html
人大代表的数据可视化,我是超级喜欢这种的风格的可视化,很喜欢这种数据新闻,分享给大家