自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(189)
  • 收藏
  • 关注

原创 Smallest Rectangle Enclosing Black Pixels

An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and v...

2016-02-13 12:39:48 139

原创 Zigzag Iterator

Given two 1d vectors, implement an iterator to return their elements alternately.For example, given two 1d vectors:v1 = [1, 2]v2 = [3, 4, 5, 6]By calling next repeatedly until hasNext retu...

2015-10-05 08:23:56 103

原创 设计模式 - Builder Pattern[转载]

原文链接:[url]http://blog.csdn.net/asce1885/article/details/43271531[/url]英文原文:[url]http://www.javacodegeeks.com/2013/01/the-builder-pattern-in-practice.html[/url]我不会详细介绍这个模式,因为已经有大量的文章或者书籍对该模式进...

2015-10-02 09:38:56 120

原创 Inorder Successor in BST

[分析]参考[url]https://leetcode.com/discuss/59728/10-and-4-lines-o-h-java-c[/url]& [url]https://leetcode.com/discuss/59787/share-my-java-recursive-solution[/url]不理解参考代码中为什么最后需要额外判断left.val > p.val...

2015-09-26 17:49:27 109

原创 Lowest Common Ancestor of A Binary Tree

[分析] 最近公共祖先(LCA)是一个经典问题,以前没有好好研究过这个问题,不知道还有个Tarjan算法,今天开了眼界。一般有两种方法分别适用不同场景:1)递归算法,适合在线单次查询,如本题;2)Tarjan算法,适合批量查询,输入是一颗树和N对定点,为每个顶点(u,v)确定LCA。有兴趣的同学看参考[url]https://github.com/julycoding/The-Ar...

2015-09-26 11:39:35 69

原创 Leetcode - Closest Binary Search Tree Value II

[分析]思路1:遍历所有节点找到和 target最接近的 k 个元素,能否有个数据结构在遍历过程中维护已遍历元素离target最近的呢?PriorityQueue具备这种能力。我们需要个最小堆,堆元素需要保存两个信息,一个是树节点元素值,一个是这个元素和target的差的绝对值。但PriorityQueue是没有“堆底”概念的,当堆的size 增长到 k 后,如何删除堆中最大元素呢? 为实现删...

2015-09-13 14:16:30 81

原创 Leetcode - H-Index II

Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm?[分析]思路:二分查找。参考解法[url]https://leetcode.com/discuss/56122/standard-binary-searc...

2015-09-06 09:39:29 75

原创 Leetcode - H-Index

Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.According to the definition of h-index on Wikipedia: "A ...

2015-09-06 09:08:42 129

原创 Leetcode - Integer to English Words

[分析]这题通过率之所以非常低是因为有很多corner case,代码写得不好时就很容易在各种corner case上刷跟头,第二十遍才被Accept,自己还真是有耐心。然后看到讨论区中的解答,一对比,差距啊,努力努力再努力!注意:不需要添加“and”大神漂亮的作品:[url]https://leetcode.com/discuss/55462/my-clean-java-solu...

2015-09-04 20:53:16 72

原创 Leetcode - The Skyline Problem

[分析]思路1参考[url]https://leetcode.com/discuss/44537/my-java-ac-code-with-priorityqueues[/url]记录下目前的理解:目标是求出所有拐点。所谓拐点是指让当前x坐标高度发生变化的点,当前x坐标的高度是x处所有楼房的最大高度。所有楼房的左右两顶点加入到考察集合,排序该集合,排序后使用最大堆来保存当前的楼高状态,遍历...

2015-09-04 19:09:42 94

原创 Leetcode - Alien Dictionary

There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, where words are sorted lexicographica...

2015-09-04 11:58:26 79

原创 Leetcode - Course Schedule II

[分析]在题I代码基础上增加记录拓扑遍历顺序即可。[code="java"]public class Solution { public int[] findOrder(int numCourses, int[][] prerequisites) { if (prerequisites == null) return new i...

2015-09-03 21:05:02 87

原创 Leetcode - Course Schedule

[分析]拓扑排序的经典应用。review时发现自己搞反了课程直接的依赖关系,但却真切地被Accept,图上画了画,发现搞反后若输出排序结果就是和原来相反,但不影响本题的正确性。网上搜了搜逆拓扑,发现其是有可用之处的,参见下面这篇博文:[url]http://blog.csdn.net/guodongxiaren/article/details/37988833[/url]另一篇...

2015-09-03 20:21:41 93

原创 Leetcode - LRU Cache

[分析]自己使用HashMap + LinkedList/ArrayList的都会超时,讨论区看到使用LinkedHashMap实现的版本[url]https://leetcode.com/discuss/42891/probably-the-best-java-solution-extend-linkedhashmap[/url],这个数据结构自己平常没用过,正好学习了~[code=...

2015-09-03 18:31:44 91

原创 Leetcode - Strobogrammatic Number III

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).Write a function to count the total strobogrammatic numbers that exist in the range of low...

2015-09-03 16:45:30 285

原创 Leetcode - Surrounded Regions

[分析]思路1:和Number of Islands类似,但是此题用DFS会栈溢出,因此使用BFS。从四个边缘的元素开始BFS, 遇到 O 改成特殊字符M,最后没有被修改的O就是被X包围的,应当改为X, 并且把M恢复成O即可。 BFS搜索时队列中维护的是所有未展开BFS的 O,队列记录这些 O的位置。 思路2:利用union find算法,将所有边界可达的O union在一起。设置一个根节...

2015-09-02 13:41:38 92

原创 Leetcode - Number of Islands

[分析]BFS & DFS法详见实现。这里阐述下union-find思路,思路很直接,我们需要把相连在一起的1union起来,最后数下union了多少个集合1。输入时一个m*n矩阵,union-find相应地需要一个二维数组保存信息,采用按秩求并方法,初始时每个1元素的秩为-1,0元素不参与union,记为0。扫描数组,对于(i,j)元素,查看其是否需要同右边和下边相邻元素合并,其上边和左...

2015-09-02 09:40:16 78

原创 Leetcode - 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 ...

2015-09-02 08:50:37 190

原创 Leetcode - Closest Binary Search Tree Value II

Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.Note:Given target value is a floating point.You may assume k is always valid, t...

2015-09-01 09:50:30 198

原创 Leetcode - Binary Tree Postorder

[分析]迭代实现后序遍历比迭代实现先序和中序都要稍微复杂,对其一直有恐惧心理,总觉得自己不看答案做不出来……这次竟然做出来了,很开心,练习还是有效果的~思路1:遍历到curr节点,不管三七二十一,入栈,继续访问左节点,因为后序遍历中root最后访问。问题是栈中的节点何时出栈?答案是其左右节点均被访问之后。怎么判断其左右节点均被访问过了呢?或者换个问题,怎么判断右节点没有被访问过(一路向左,...

2015-08-29 21:07:42 80

原创 Leetcode - Binary Tree Upside Down

Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the origin...

2015-08-29 18:50:47 74

原创 Leetcode - Verify Preorder Sequence in Binary Search Tree

[分析]思路1:暴力法,遍历当前待检查数组,找到第一个大于数组起始位置的位置i,则 i 为右子树根节点,然后递归判断左右子树。对于根节点,最坏情况下要遍历整个数组,时间为O(N),因为要递归检查每个节点,因此总的时间复杂度是O(N^2)。思路2:参考StefanPochmann大神的作品[url]https://leetcode.com/discuss/51543/java-o-n-and...

2015-08-29 16:46:16 80

原创 Leetcode - Min Stack

[分析]这题属于简单题,之所以要记录下是因为遇到了个不理解的Java语法问题。在pop()时如果判断条件写成if (minStack.peek() == stack.peek())是要吃WA的,原因是在表达式中比较的是引用本身,而不是值,奇怪的是在main中我认为一样的情况(e\f这对)判断结果却不同。在pop()中执行如下两句: System.out.print...

2015-08-29 11:20:36 84

原创 Leetcode - Palindrome Permutation II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.For example:Given s = "aabb", return ["ab...

2015-08-28 21:17:29 117

原创 Leetcode - Factor Combination

Numbers can be regarded as product of its factors. For example,8 = 2 x 2 x 2; = 2 x 4.Write a function that takes an integer n and return all possible combinations of its factors.Note: ...

2015-08-28 09:53:20 93

原创 Leetcode - Read N Characters Given Read4 II - Call Multiple Times

The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the...

2015-08-28 09:00:18 72

原创 Leetcode - Read N Characters Given Read4

The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the...

2015-08-27 20:56:29 88

原创 Leetcode - One Edit Distance

[分析]两字符串相同或者长度差异大于等于2都不符合要求,只需要检查那些长度相同或者相差为1的情况。逐个字符检查,发现第一个差异字符,若s和t长度相等将s中字符替换为t中字符,若不等则在s中插入t当前字符,然后跳出检查循环。若检查过程无差异字符(说明长度差1)或者消除发现的第一个差异后两字符串相等说明两字符串编辑距离为1.这个思路和实现参考[url]https://leetcode.com/...

2015-08-27 20:26:17 61

原创 Leetcode - Basic Calculator II

mplement a basic calculator to evaluate a simple expression string.The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should tr...

2015-08-27 09:16:27 78

原创 Leetcode - Factorial Trailing Zeroes

[思路]数乘积结果的后缀0,其实就是数结果中有多少个因子10,10=2*5,易知因子2是充裕的,因此只要数因子5的个数,暴力的方法是统计并加和1到n每个数含有因子5的个数,会超时。能贡献因子5的数是5,10,15,20,25,30……,其中所有5的倍数会至少贡献1个因子5,所有25的倍数会至少贡献两个因子5,所有5*5*5=125的倍数会至少贡献三个因子5,依次类推,因此n中5的因子个数=...

2015-08-25 09:00:01 58

原创 Leetcode - Ugly Number II

[分析]暴力的办法就是从1开始检查每个数是否是丑数,发现丑数计数器加1直到找到第n个丑数。这种方法效率低,因为它不管是不是丑数都进行了计算,所以优化方向是仅计算丑数而不在非丑数上耗费时间。易知后面的丑数一定是前面的丑数乘以2或者3或者5得到的,假设现在已经计算出第k个丑数U(k),那么下一个丑数是前面丑数中乘以2、3、5中第一个大于U(k)的数。怎么找呢?我们需要保留已计算的所有丑数,将已知丑...

2015-08-24 22:54:57 60

原创 Leetcode - Excel Sheet Column Title

[分析]十进制转26进制,需要注意的是26进制是以1为最小数的。思路1写了好久,放在这儿用于警示自己还需要更多练习。[code="java"]public class Solution { // Method 2 // https://leetcode.com/discuss/19047/my-1-lines-code-in-java-c-and-pytho...

2015-08-24 10:24:55 57

原创 Leetcode - Converted Sorted List to BST

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.[分析]思路1复杂度是O(nlogn),实现过程中发现自己二指针法一开始是写错的,错误的结果是二分后左边会比右边多两个元素,在Reorder List中这种写法之所以也能...

2015-08-23 20:07:53 80

原创 Leetcode - Reverse Nodes in k-Group

[分析]使用递归会Memory Limit Exceed,迭代方式参考[url]https://leetcode.com/discuss/17483/share-my-java-solution-with-comments-in-line[/url]解析见代码注释Reverse Linked List, Reverse Linked List II 可使用此题的解题技巧,可联系着体...

2015-08-23 16:53:25 82

原创 Leetcode - Max Points on a Line

[分析]两条直线若包含一个公共点且斜率相同,则为同一条直线。因此依次将数组中各点设为公共点,并计算所有未当过公共点的其他点同当当前公共点形成直线的斜率,使用哈希表保存各斜率直线上的点数,遍历过程中同时更新维护一条直线上包含的最多点数。实现1中key直接就是double类型的斜率,实现时有几个注意点:1)斜率为0时要单独判断并显式赋值为0,double类型0 和 -0是不等的2)需要...

2015-08-23 15:30:26 109

原创 Leetcode - Fraction to Recurring Decimal

[分析][color=blue]处理int型整数运算时,为避免溢出,省事的做法就是内部转为long类型处理[/color],不然极可能在极值case上栽跟头,比如int a = Integer.MIN_VALUE, int b = -1 和 long a = Integer.MIN_VALUE, long b = -1, 两者a / b的结果是不一样的,前者会发生溢出,在比如Math.ab...

2015-08-23 10:05:03 69

原创 Leetcode - Isomorphic Strings

[分析]思路1:维护两个哈希表,char[] map, boolean[] used, 长度均为256,map[charS] = charT, 表示将字符charS 映射为charT, used[charT]==true 表示charT已经被某个字符映射过了。同步遍历字符串s & t,记两者当前的字符分别为charS, charT, 若charS是新字符,且charT没有被使用,则我们可将其同...

2015-08-23 09:51:03 41

原创 Leetcode - Palindrome Permutation

[分析]思路2让我大开眼界,顺便学习下BitSet~[ref][url]https://leetcode.com/discuss/53180/1-4-lines-python-ruby-c-c-java[/url][code="java"]public class Solution { // Method 2: https://leetcode.com/discu...

2015-08-22 16:24:14 77

原创 Leetcode - Group Shifted String

Given a string, we can "shift" each of its letter to its successive letter, for example: "abc" -> "bcd". We can keep "shifting" which forms the sequence:"abc" -> "bcd" -> ... -> &q

2015-08-22 16:20:31 91

原创 Leetcode - Count Primes

[ref][url]https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes[/url][url]https://leetcode.com/discuss/34622/my-c-solutions-in-44ms-time-nearly-o-n-and-space-nearly-o-n[/url][code="java"]publi...

2015-08-22 13:42:34 80

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除