随笔分类 -  Google

摘要:难度:87,这道题跟 Rotate Image 很相似,都是需要把矩阵分割成层来处理,每一层都是按:1. 正上方;2. 正右方;3. 正下方;4. 正左方这种顺序添加元素到结果集合。实现中要注意细节,when I traverse left or up I have to check whether 阅读全文
posted @ 2017-07-17 18:50 apanda009 阅读(108) 评论(0) 推荐(0)
摘要:这道题是一道典型的位操作Bit Manipulation的题目,我开始以为异或值最大的两个数一定包括数组的最大值,但是OJ给了另一个例子{10,23,20,18,28},这个数组的异或最大值是10和20异或,得到30。那么只能另辟蹊径,正确的做法是按位遍历,题目中给定了数字的返回不会超过231,那么 阅读全文
posted @ 2017-07-15 22:12 apanda009 阅读(126) 评论(0) 推荐(0)
摘要:这道题让我们判断一个数是否为2的次方数,而且要求时间和空间复杂度都为常数,那么对于这种玩数字的题,我们应该首先考虑位操作 Bit Operation。在LeetCode中,位操作的题有很多,比如比如Repeated DNA Sequences 求重复的DNA序列, Single Number 单独的 阅读全文
posted @ 2017-07-15 11:08 apanda009 阅读(163) 评论(0) 推荐(0)
摘要:Map.Entry<> entry : map.entrySet() 阅读全文
posted @ 2017-07-15 10:55 apanda009 阅读(122) 评论(0) 推荐(0)
摘要:Let's say nums is [10,11,...,19]. Then after nth_element and ordinary partitioning, we might have this (15 is my median): I rewire it so that the firs 阅读全文
posted @ 2017-07-14 22:50 apanda009 阅读(144) 评论(0) 推荐(0)
摘要:存在正负情况,处理方式是按正数处理,符号最后在判断,那么我们需要把除数和被除数取绝对值,那么问题就来了:由于整型数INT的取值范围是-2147483648~2147483647,而对-2147483648取绝对值就会超出范围,所以我们需要先转为long long型再取绝对值。那么怎么样找循环呢,肯定 阅读全文
posted @ 2017-07-14 13:54 apanda009 阅读(157) 评论(0) 推荐(0)
摘要:Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevation map, compute the volume of water it is able to 阅读全文
posted @ 2017-07-13 22:22 apanda009 阅读(161) 评论(0) 推荐(0)
摘要:道题给定我们一个有序数组,让我们总结区间,具体来说就是让我们找出连续的序列,然后首尾两个数字之间用个“->"来连接,那么我只需遍历一遍数组即可,每次检查下一个数是不是递增的,如果是,则继续往下遍历,如果不是了,我们还要判断此时是一个数还是一个序列,一个数直接存入结果,序列的话要存入首尾数字和箭头“- 阅读全文
posted @ 2017-07-13 19:02 apanda009 阅读(145) 评论(0) 推荐(0)
摘要:Graph, DFS (1) Build the map, the key is dividend, the value is also a map whose key is divisor and value is its parameter. For example, a / b = 2.0, 阅读全文
posted @ 2017-07-13 18:49 apanda009 阅读(123) 评论(0) 推荐(0)
摘要:这道题给我们一个二维数组,让我们求矩阵中最长的递增路径,规定我们只能上下左右行走,不能走斜线或者是超过了边界。那么这道题的解法要用递归和DP来解,用DP的原因是为了提高效率,避免重复运算。我们需要维护一个二维动态数组dp,其中dp[i][j]表示数组中以(i,j)为起点的最长递增路径的长度,初始将d 阅读全文
posted @ 2017-07-13 11:00 apanda009 阅读(135) 评论(0) 推荐(0)
摘要:Example: 矩阵涉及到计算的问题常常根据题意, 目的找规律 阅读全文
posted @ 2017-07-11 21:26 apanda009 阅读(148) 评论(0) 推荐(0)
摘要:这道题还是蛮有创意的一道题,是说自然数序列看成一个长字符串,问我们第N位上的数字是什么。那么这道题的关键就是要找出第N位所在的数字,然后可以把数字转为字符串,这样直接可以访问任何一位。那么我们首先来分析自然数序列和其位数的关系,前九个数都是1位的,然后10到99总共90个数字都是两位的,100到99 阅读全文
posted @ 2017-07-11 18:57 apanda009 阅读(171) 评论(0) 推荐(0)
摘要:A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59). Each LED represents 阅读全文
posted @ 2017-07-11 17:18 apanda009 阅读(153) 评论(0) 推荐(0)
摘要:Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover of a rectangular region. Each rectangle is represented 阅读全文
posted @ 2017-07-11 13:10 apanda009 阅读(143) 评论(0) 推荐(0)
摘要:refer to: https://discuss.leetcode.com/topic/60394/easy-concept-with-python-c-java-solution E.g.input: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]subar 阅读全文
posted @ 2017-07-10 23:05 apanda009 阅读(240) 评论(0) 推荐(0)
摘要:编解码法 复杂度 时间 O(NN) 空间 O(1) 思路 最简单的方法是再建一个矩阵保存,不过当inplace解时,如果我们直接根据每个点周围的存活数量来修改当前值,由于矩阵是顺序遍历的,这样会影响到下一个点的计算。如何在修改值的同时又保证下一个点的计算不会被影响呢?实际上我们只要将值稍作编码就行了 阅读全文
posted @ 2017-07-10 18:01 apanda009 阅读(150) 评论(0) 推荐(0)
摘要:我的做法, hashMap, O(n) space, O(n) time: 用ascii 码表, 时间, 空间都是O(1) 学会转化: (int) s.charAt(i) 阅读全文
posted @ 2017-07-09 21:37 apanda009 阅读(137) 评论(0) 推荐(0)