随笔分类 -  HashMap/ 数组键值对

摘要:Brute Force的做法,N个点两两可以构成N(N-1)/2条线,我们可以找这N(N-1)/2条线中线上点数最大值,只需对每一条线再进行一层O(N)的遍历,总共是O(N^3)。 用第二种方法更好,选一个基准点, 看后面每一个点跟它构成的直线, 维护一个HashMap, key是跟这个点构成直线的 阅读全文
posted @ 2017-11-30 22:08 apanda009 阅读(235) 评论(0) 推荐(0)
摘要:A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to determine if a number is strobogrammatic. The number is represented as ... 阅读全文
posted @ 2017-11-27 12:48 apanda009 阅读(143) 评论(0) 推荐(0)
摘要:How do we solve problem like this if we were given a normal tree? Yes, traverse it, keep a root to leaf running sum. If we see a leaf node (node.left 阅读全文
posted @ 2017-11-17 03:35 apanda009 阅读(260) 评论(0) 推荐(0)
摘要:Taken from GeeksforGeeks Following is a simple algorithm to find out whether a given graph is Birpartite or not using Breadth First Search (BFS) :- Al 阅读全文
posted @ 2017-11-08 03:14 apanda009 阅读(196) 评论(0) 推荐(0)
摘要:Analysis: Solution 1: The greedy algorithm is that in each step, select the char with highest remaining count if possible (if it is not in the waiting 阅读全文
posted @ 2017-11-07 21:37 apanda009 阅读(199) 评论(0) 推荐(0)
摘要:Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure.find 阅读全文
posted @ 2017-11-07 06:02 apanda009 阅读(137) 评论(0) 推荐(0)
摘要:这道题就是根据条件老老实实的做 阅读全文
posted @ 2017-11-07 05:30 apanda009 阅读(157) 评论(0) 推荐(0)
摘要:Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrenc 阅读全文
posted @ 2017-11-07 05:06 apanda009 阅读(497) 评论(0) 推荐(0)
摘要:This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly ma 阅读全文
posted @ 2017-11-07 04:33 apanda009 阅读(138) 评论(0) 推荐(0)
摘要:要注意一种情况:isUnique("cake"), 应该是true. 虽然他的缩写在afterabbr出现,但是那就是他自己,没有别的跟他一样 阅读全文
posted @ 2017-11-06 04:51 apanda009 阅读(162) 评论(0) 推荐(0)
摘要:注意这是一一映射,也就是说如果a->dog, b->dog,就应该return false,所以应该在HashMap基础上再加一层检查,即若不含该key,加入map之前应该检查map.values().contains(String) 第二遍:use HashMap和HashSet 用 map.va 阅读全文
posted @ 2017-11-06 04:31 apanda009 阅读(145) 评论(0) 推荐(0)
摘要:这道题跟Lintcode: Majority Number II思路很像,那个找大于1/3的,最多有两个candidate,这个一样,找大于1/k的,最多有k-1个candidate 维护k-1个candidate 在map里面,key为数字值,value为出现次数。先找到这k-1个candidat 阅读全文
posted @ 2017-11-06 03:15 apanda009 阅读(151) 评论(0) 推荐(0)
摘要:这个是用一个hashtable,key是数字,value是出现次数 然后遍历原数组,每一个数字都把hash里从自己开始往后5个color数都-1,如果发现缺数则说明不能分割 很容易错! 错了好多次,是color往后5个,如果不存在该color或者color数目已经为0,报错 阅读全文
posted @ 2017-11-06 02:17 apanda009 阅读(206) 评论(0) 推荐(0)
摘要:follow up: 建立一个Lookup table, 算过的数就不算了 阅读全文
posted @ 2017-11-06 01:05 apanda009 阅读(165) 评论(0) 推荐(0)
摘要:Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between i and j equals 阅读全文
posted @ 2017-11-05 22:17 apanda009 阅读(121) 评论(0) 推荐(0)
摘要:数组;--键值对 follow up: 阅读全文
posted @ 2017-09-25 08:07 apanda009 阅读(162) 评论(0) 推荐(0)
摘要:Example 2: Example 3: The logic is very similar to NO.347 and here we just use a map a count and according to the frequency to put it into the right b 阅读全文
posted @ 2017-09-23 11:27 apanda009 阅读(155) 评论(0) 推荐(0)
摘要:数组和字符串的分治法: 对返回值的操作(叶结点的+ 非叶结点的), 出口, 分治起始位置, 题目要求--返回值 难点在于从哪分(题目要求的理解? 中间?符号?)和叶结点情况的处理->返回值 见Different Ways to Add Parentheses 投票法: 根据majority 的特性 阅读全文
posted @ 2017-08-07 08:57 apanda009 阅读(109) 评论(0) 推荐(0)
摘要:fb: 只用返回true or false。第二题先用set做,后来让改用constant space, 就用了sliding window这样 subarray sum 问题常用hashmap, 存count 值和坐标, 动归的感觉啊 fb:问了数组包含/不包含负数两种情况, 要用 preSum. 阅读全文
posted @ 2017-08-03 22:56 apanda009 阅读(242) 评论(0) 推荐(0)
摘要:这种考subarray sum 常用到累加和数组啊, 要清楚从哪到哪开始求和, 在看题意怎么判断就ok了, 数组subarray sum 问题常常用累加和基础上改变 没想到用hashmap O(n) 即可, 关键是存的都是k的余数, 然后余数相见等于零即可 if (k != 0) runningSu 阅读全文
posted @ 2017-08-03 21:48 apanda009 阅读(274) 评论(0) 推荐(0)