入职新公司之前,回到东岸见了些朋友。毕业后再也没有来过这里,这次有机会当一个真正的游客走在纽约街头,却无法像游客那样从心出发欣赏这里的美。MOMA有知名画作,sleep no more刷新了我对舞剧的认识,还有长岛舒适的节奏,第一次住新泽西,比想象中要好很多。来美第一次坐灰狗去DC,却是帮朋友们搬了家。华府的樱花的确很美,各种博物馆也值得让我再去看一次。一切看起来都很好,但奇怪的是我内心却毫无波澜,从始至终没有像从前那样有让我惊艳的时刻,可能真的老了吧。
From wiki: In computer science, a disjoint-set data structure, also called a union–find data structure or merge–find set, is a data structure that keeps track of a set of elements partitioned into a number of disjoint (nonoverlapping) subsets. It supports two useful operations: Find: Determine which subset a particular element is in. Find typically returns an item from this set that serves as its “representative”; by comparing the result of two Find operations, one can determine whether two elements are in the same subset. Union: Join two subsets into a single subset.
Example: 261. Graph Valid Tree Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.
For example: Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return true. Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], return false.
Hint: Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], what should your return? Is this case a valid tree? According to the definition of tree on Wikipedia: “a tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.”
Note: you can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.
A bad system design can lead to much hard work. In order to increase the unit tests coverage, I recently started to work on writing unit tests for some classes. One of the case is I want to test a method as follow:
1 2 3 4 5 6 7 8
publicfinal ReturnType getMethod(SomeRequest someRequest){ AnotherRequest anotherRequest = new AnotherRequest(someRequest); SomeResponse someResponse = SomeService.getInstance().someMethod(anotherRequest); SomeValue someValue = someResponse.getValue(); /** * Some processes with someValue.. */ }
The main purpose of this test is testing the process with someValue, so I should just mock the .getValue() method. But the thing is not that easy. Let me put more related classes here: SomeService.class
1 2 3 4 5 6 7 8 9 10 11 12 13
publicfinalclassSomeService{ privatestatic SomeService instance = new SomeService(); static { // A static block } protectedSomeService(){} publicstatic SomeService getInstance(){ return instance; } public SomeResponse someMethod(AnotherRequest anotherRequest){ // Implementation of the method.. } }
SomeResponse.class
1 2 3 4 5
publicclassSomeResponse{ public SomeValue getValue(){ // Implementation of getValue() } }
SomeValue.class
1 2 3 4 5 6 7 8 9
publicclassSomeValue{ private String name; privatevoidpopulateValue(PreDefinedType preDefinedType){ // Generate name from a preDefinedType, basically a black box. } public String getName(){ return name; } }