随笔分类 -  LeetCode Solutions

上一页 1 ··· 6 7 8 9 10 11 12 下一页
Solutions to problems on https://leetcode.com/problemset/algorithms/.
摘要:A typical backtracking problem. For any backtracking problem, you need to be think about three ascepts:What is a partial solution and when is it finis... 阅读全文
posted @ 2015-06-16 23:45 jianchao-li 阅读(192) 评论(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 阅读(334) 评论(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 阅读(215) 评论(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 阅读(209) 评论(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 阅读(256) 评论(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 阅读(313) 评论(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 阅读(353) 评论(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 阅读(203) 评论(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 阅读(286) 评论(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 阅读(447) 评论(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 阅读(195) 评论(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 阅读(190) 评论(0) 推荐(0)
摘要:Well, it seems that many people meet the TLE problem. Well, I use a simple trick in my code to aoivd TLE. That is, each time before I try to breaks, I... 阅读全文
posted @ 2015-06-10 09:39 jianchao-li 阅读(318) 评论(0) 推荐(0)
摘要:Well, an extension of Remove Duplicates from Sorted Array.The program is fairly similar to that in this solution.1 int removeDuplicates(vector& nu... 阅读全文
posted @ 2015-06-09 21:39 jianchao-li 阅读(207) 评论(0) 推荐(0)
摘要:Well, a medium problem. Use two pointers. One points to the curren number and the other points to the last unique number. Once duplicates occur, move ... 阅读全文
posted @ 2015-06-09 21:30 jianchao-li 阅读(183) 评论(0) 推荐(0)
摘要:Well, this problem is designed for radix sort. For more information about radix sort, Introduction to Algorithms, 3rd edition has some nice examples.H... 阅读全文
posted @ 2015-06-09 17:13 jianchao-li 阅读(158) 评论(0) 推荐(0)
摘要:The hints on below the problem have suggested a two-pass solution.Now I will focus on the one-pass solution.The one-pass solution has no mystery. Just... 阅读全文
posted @ 2015-06-09 14:31 jianchao-li 阅读(201) 评论(0) 推荐(0)
摘要:This problem gets much trickier than Contains Duplicate and Contains Duplicate II.The basic idea is to maintain a window of k numbers. For each new nu... 阅读全文
posted @ 2015-06-09 11:18 jianchao-li 阅读(271) 评论(0) 推荐(0)
摘要:A classic problem of hash set. The unordered_set of C++ is very suitable for this problem.The code is as follows and it should be quite self-explanato... 阅读全文
posted @ 2015-06-09 11:01 jianchao-li 阅读(186) 评论(0) 推荐(0)
摘要:Problem Description:Given an array of integers that is alreadysorted in ascending order, find two numbers such that they add up to a specific target n... 阅读全文
posted @ 2015-06-08 23:11 jianchao-li 阅读(281) 评论(0) 推荐(0)

上一页 1 ··· 6 7 8 9 10 11 12 下一页