Skip to content

Instantly share code, notes, and snippets.

@wszdwp
wszdwp / LC257BSTPath.java
Created August 10, 2019 23:42
non-recur and recur for bst paths
public List<String> binaryTreePaths(TreeNode root) {
//List<List<Integer>> ans = new ArrayList<>();
// dfs(root, new ArrayList<>(), ans);
// return formatAns(ans);
List<String> ans = new ArrayList<>();
dfsIter(root, ans);
return ans;
}
/**
* Lovely Lucky LAMBs
* ==================
* Being a henchman isn't all drudgery. Occasionally, when Commander Lambda is feeling generous,
* she'll hand out Lucky LAMBs (Lambda's All-purpose Money Bucks). Henchmen can use Lucky LAMBs to buy things like
* a second pair of socks, a pillow for their bunks, or even a third daily meal!
*
* However, actually passing out LAMBs isn't easy. Each henchman squad has a strict seniority ranking which must
* be respected - or else the henchmen will revolt and you'll all get demoted back to minions again!
*
The cake is not a lie!
======================
Commander Lambda has had an incredibly successful week: she completed the first test run of her LAMBCHOP doomsday device, she captured six key members of the Bunny Rebellion, and she beat her personal high score in Tetris. To celebrate, she's ordered cake for everyone - even the lowliest of minions! But competition among minions is fierce, and if you don't cut exactly equal slices of cake for everyone, you'll get in big trouble.
The cake is round, and decorated with M&Ms in a circle around the edge. But while the rest of the cake is uniform, the M&Ms are not: there are multiple colors, and every minion must get exactly the same sequence of M&Ms. Commander Lambda hates waste and will not tolerate any leftovers, so you also want to make sure you can serve the entire cake.
To help you best cut the cake, you have turned the sequence of colors of the M&Ms on the cake into a string: each possible letter (between a and z) corresponds to a unique color, and the seque

Meeting Room II

解法思路: 统计起点终点在当前时刻的差值

解法1: 扫描线(tree map)

[来源](https://blog.csdn.net/yurenguowang/article/details/76665171) 将这三个区间在x轴上画出来,并用一条垂直于x轴的线作为扫描线从左至右扫描,会很容易得出答案,即与扫描线焦点的最大值即为所求。但是在程序中我们怎样表示这种思想呢
-对所有点进行标记,区分起始点和终止点
-依次遍历每个点,遇到起始点+1,遇到终止点-1,并更新记录最大值

@wszdwp
wszdwp / VerticalOrderBST.java
Created July 25, 2019 21:48
vertical order BST in dfs and bfs
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }

[翻译+摘要]为何要用Copy constructor和不是Cloning 原文: https://www.artima.com/intv/bloch13.html

Cloneable 接口没有提供任何方法,基本上没有要求限制,也没有提供任何方法去copy。

  1. 最终都会叫Object的clone方法。无法对clone实现多态。
  2. Array of cloneable, 无法deep copy array。无法其他东西Cast成Cloneable然后叫clone方法,因为Cloneable和Object都没有公共的clone方法,cast的时候编译器会报错说你在叫Object中的protected clone方法。

Object clone Object clone方法很有陷阱,它是基于field copies,而且有“多余的语义”。它没有叫Constructor创建了Object。这导致Constructor创建的不变性失去了保证。这个问题导致出现很多问题,都终结于反复叫super.clone的chain, 会产生一个shallow copy of the object, clone的Object和原Object共享状态,如果状态是可修改的,其中一个修改会更改状态。

@wszdwp
wszdwp / 编程的智慧-Summary-王垠.md
Last active July 9, 2019 13:27
代码重构,null pointer, exception,

[转]编程的智慧https://www.yinwang.org/blog-cn/2015/11/21/programming-philosophy

编程是一种创造性的工作,是一门艺术。精通任何一门艺术,都需要很多的练习和领悟,所以这里提出的“智慧”,并不是号称一天瘦十斤的减肥药,它并不能代替你自己的勤奋。然而由于软件行业喜欢标新立异,喜欢把简单的事情搞复杂,我希望这些文字能给迷惑中的人们指出一些正确的方向,让他们少走一些弯路,基本做到一分耕耘一分收获。

反复推敲代码 有些人喜欢炫耀自己写了多少多少万行的代码,仿佛代码的数量是衡量编程水平的标准。然而,如果你总是匆匆写出代码,却从来不回头去推敲,修改和提炼,其实是不可能提高编程水平的。你会制造出越来越多平庸甚至糟糕的代码。在这种意义上,很多人所谓的“工作经验”,跟他代码的质量其实不一定成正比。如果有几十年的工作经验,却从来不回头去提炼和反思自己的代码,那么他也许还不如一个只有一两年经验,却喜欢反复推敲,仔细领悟的人。

有位文豪说得好:“看一个作家的水平,不是看他发表了多少文字,而要看他的废纸篓里扔掉了多少。” 我觉得同样的理论适用于编程。好的程序员,他们删掉的代码,比留下来的还要多很多。如果你看见一个人写了很多代码,却没有删掉多少,那他的代码一定有很多垃圾。

就像文学作品一样,代码是不可能一蹴而就的。灵感似乎总是零零星星,陆陆续续到来的。任何人都不可能一笔呵成,就算再厉害的程序员,也需要经过一段时间,才能发现最简单优雅的写法。有时候你反复提炼一段代码,觉得到了顶峰,没法再改进了,可是过了几个月再回头来看,又发现好多可以改进和简化的地方。这跟写文章一模一样,回头看几个月或者几年前写的东西,你总能发现一些改进。

@wszdwp
wszdwp / leet_sys_summary.md
Last active July 8, 2019 16:42
2019 July Summary

Leet System Summary

Leet

674 最长递增子序列
F/U 可以删掉一个数字求最长

244 Shortest Word Distance II

301 Remove Invalid Parentheses

@wszdwp
wszdwp / jsGist.js
Created June 5, 2019 12:44
some javascript gist
// DOM selection
var myElem = document.getElementById('myElemId');
var myElem = document.getElementsByTagName('elemTagName');
var myElem = document.getElementsByClassName('className');
// DOM delete
while (myElem.firstChild) {
myElem.removeChild(myElem.firstChild);
}