摘要: This is the classic LCS problem. Since it only requires you to print the maximum length, the code can be optimized to use only O(m) space (seehere).My... 阅读全文
posted @ 2015-06-14 23:17 jianchao-li 阅读(255) 评论(0) 推荐(0) 编辑
摘要: This is the classic LCS problem. Since it requires you to print one longest common subsequence, just use the O(m*n)-space version here.My accepted cod... 阅读全文
posted @ 2015-06-14 23:09 jianchao-li 阅读(612) 评论(0) 推荐(0) 编辑
摘要: The Longest Common Subsequence (LCS) problem is as follows:Given two sequences s andt, find the length of the longest sequence r, which is a subsequen... 阅读全文
posted @ 2015-06-13 23:10 jianchao-li 阅读(483) 评论(0) 推荐(0) 编辑
摘要: The Longest Common Substring (LCS) problem is as follows:Given two strings s and t, find the length of the longest string r, which is a substring of b... 阅读全文
posted @ 2015-06-13 22:36 jianchao-li 阅读(333) 评论(0) 推荐(0) 编辑
摘要: This problem is a nice extension of the Valid Parentheses problem.There are several ways to solve it. The first idea is also to use a stack. However, ... 阅读全文
posted @ 2015-06-13 15:29 jianchao-li 阅读(322) 评论(0) 推荐(0) 编辑
摘要: Well, there are two ways to add a open or close parenthesis to the current string.If number of(is less thann, you can add(;If number of)is less than n... 阅读全文
posted @ 2015-06-13 14:41 jianchao-li 阅读(208) 评论(0) 推荐(0) 编辑
摘要: This is a classic problem of the application of stacks. The idea is, each time we meet a(,{or[, we push it to a stack. If we meet a),}or], we check if... 阅读全文
posted @ 2015-06-13 14:19 jianchao-li 阅读(201) 评论(0) 推荐(0) 编辑
摘要: Problem Description: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) ... 阅读全文
posted @ 2015-06-12 22:00 jianchao-li 阅读(250) 评论(0) 推荐(0) 编辑
摘要: Problem Description:Given a string, find the length of the longest substring T that contains at most 2 distinct characters.For example, Given s =“eceb... 阅读全文
posted @ 2015-06-12 20:44 jianchao-li 阅读(304) 评论(0) 推荐(0) 编辑
摘要: After reading the quote below the problem, you will be motivated to solve it immediately :-) Well, indeed it is also relative easy. The process of inv... 阅读全文
posted @ 2015-06-12 17:01 jianchao-li 阅读(349) 评论(0) 推荐(0) 编辑
摘要: This problem is not very intuitive at first glance. However, the final idea should be very self-explanatory. You visit each element in nums, and then ... 阅读全文
posted @ 2015-06-12 16:40 jianchao-li 阅读(194) 评论(0) 推荐(0) 编辑
摘要: The idea is to find the longest palindromic substring ofsthat begins withs[0]. Then take the remaining susbtring, reverse it and append it to the begi... 阅读全文
posted @ 2015-06-11 01:28 jianchao-li 阅读(275) 评论(0) 推荐(0) 编辑
摘要: Well, this problem has a O(n^3) solution similar to 3Sum. That is, fix two elements nums[i] and nums[j] (i > fourSum(vector& nums, int target) { 2 ... 阅读全文
posted @ 2015-06-11 00:19 jianchao-li 阅读(442) 评论(1) 推荐(0) 编辑
摘要: This problem is very similar to 3Sum. You only need to maintain a variable for the sum that is closet to target. Also, some corner cases need to be ha... 阅读全文
posted @ 2015-06-10 22:57 jianchao-li 阅读(188) 评论(0) 推荐(0) 编辑
摘要: This is an extension of the 2Sum problem. The idea is pretty simple (even no need to use hash). Sort the array and then starting from the first elemen... 阅读全文
posted @ 2015-06-10 17:49 jianchao-li 阅读(184) 评论(0) 推荐(0) 编辑