摘要:
题目链接:Pairs完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数。总结其实就是“骑驴找马”的问题:即当前遍历ar[i],那么只要看数组中是否存在ar[i]+K或者ar[i]-K就可以了,还是用HashMap在O(1)... 阅读全文
摘要:
题目链接:Cut the tree题解:题目要求求一条边,去掉这条边后得到的两棵树的节点和差的绝对值最小。暴力求解会超时。如果我们可以求出以每个节点为根的子树的节点之和,那么当我们去掉一条边(a,b)的时候,其中的一棵树必是以a或者b为根的子树,那么我们就可以知道生成的两棵树的节点之和了。所以,当我... 阅读全文
摘要:
Numeros, The Artist, had two listsAandB, such that,Bwas a permutation ofA. Numeros was very proud of these lists. Unfortunately, while transporting th... 阅读全文
摘要:
Sorting is often useful as the first step in many different tasks. The most common task is to make finding things easier, but there are other uses als... 阅读全文