08 2015 档案

摘要:This problem is not difficult. But it is not easy to have a bug-free code. As you write your codes according to the hints, the most important thing is... 阅读全文
posted @ 2015-08-31 20:52 jianchao-li 阅读(678) 评论(0) 推荐(0) 编辑
摘要:Problem Description:Given a non-empty binary search tree and a target value, findkvalues in the BST that are closest to the target.Note:Given target v... 阅读全文
posted @ 2015-08-30 18:50 jianchao-li 阅读(4104) 评论(0) 推荐(0) 编辑
摘要:Problem Description:Design an algorithm to encodea list of stringstoa string. The encoded string is then sent over the network and is decoded back to ... 阅读全文
posted @ 2015-08-29 12:22 jianchao-li 阅读(4715) 评论(0) 推荐(0) 编辑
摘要:1 /** 2 * class VersionControl { 3 * public: 4 * static bool isBadVersion(int k); 5 * } 6 * you can use VersionControl::isBadVersion(k) ... 阅读全文
posted @ 2015-08-28 10:34 jianchao-li 阅读(308) 评论(0) 推荐(0) 编辑
摘要:An interesting problem. The code is also short and clear.The basic idea is to use a 2d array dp[i][j] to denote the minimum hp that is required before... 阅读全文
posted @ 2015-08-27 18:15 jianchao-li 阅读(480) 评论(0) 推荐(0) 编辑
摘要:Problem Description:Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.Note:Given target... 阅读全文
posted @ 2015-08-27 14:28 jianchao-li 阅读(2892) 评论(0) 推荐(0) 编辑
摘要:Problem Description:There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a li... 阅读全文
posted @ 2015-08-25 21:39 jianchao-li 阅读(15830) 评论(3) 推荐(0) 编辑
摘要:This link has two nice solutions, one updating from forth to back (posted by tqlong in the post) and the other updating from back to forth (posted by ... 阅读全文
posted @ 2015-08-24 18:27 jianchao-li 阅读(231) 评论(0) 推荐(0) 编辑
摘要:There are three methods to solve this problem: bit manipulation, rearrangement of the array, and math tricks.Bit Manipulation1 class Solution {2 publi... 阅读全文
posted @ 2015-08-24 11:32 jianchao-li 阅读(248) 评论(0) 推荐(0) 编辑
摘要:Problem Description:Given a strings, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permut... 阅读全文
posted @ 2015-08-23 10:59 jianchao-li 阅读(3829) 评论(0) 推荐(0) 编辑
摘要:Problem Description:Given a string, determine if a permutation of the string could form a palindrome.For example,"code"-> False,"aab"-> True,"carerac"... 阅读全文
posted @ 2015-08-21 17:41 jianchao-li 阅读(4043) 评论(0) 推荐(0) 编辑
摘要:The idea and code is just taken from this link. There is a nice explanation to the code on the answer byBrianLuong1337. 1 class Solution { 2 public: 3... 阅读全文
posted @ 2015-08-20 00:58 jianchao-li 阅读(223) 评论(0) 推荐(0) 编辑
摘要:Stefan is a hero! Personally, I love his first C++ solution. 1 class Solution { 2 public: 3 int nthUglyNumber(int n) { 4 vector ugly(n, 1)... 阅读全文
posted @ 2015-08-19 16:32 jianchao-li 阅读(237) 评论(0) 推荐(0) 编辑
摘要:No matter what language you use on the OJ, you may refer to Stefan's post for a solution. The C++ is rewritten below, just really concise.1 class Solu... 阅读全文
posted @ 2015-08-19 16:28 jianchao-li 阅读(205) 评论(0) 推荐(0) 编辑
摘要:The idea is very simple: just extract the numbers from each version number and compare the numbers from beginning to the end. However, C++ seems to ha... 阅读全文
posted @ 2015-08-18 22:31 jianchao-li 阅读(298) 评论(0) 推荐(0) 编辑
摘要:The basic idea is to store the key-value pair in some container. In the following code, we make three type definitions:1 typedef list LI;2 typedef pai... 阅读全文
posted @ 2015-08-18 21:53 jianchao-li 阅读(336) 评论(0) 推荐(0) 编辑
摘要:The idea is similar to Strobogrammatic Number II: generate all those in-range strobogrammatic numbers and count.You may refer to this link for a very ... 阅读全文
posted @ 2015-08-18 15:04 jianchao-li 阅读(1594) 评论(0) 推荐(0) 编辑
摘要:Problem Description:Givennnodes labeled from0ton - 1and a list of undirected edges (each edge is a pair of nodes), write a function to check whether t... 阅读全文
posted @ 2015-08-18 10:58 jianchao-li 阅读(3466) 评论(0) 推荐(1) 编辑
摘要:A typical DFS problem. Just go ahead... 1 class Solution { 2 public: 3 bool exist(vector>& board, string word) { 4 int m = board.size(), n... 阅读全文
posted @ 2015-08-18 00:11 jianchao-li 阅读(212) 评论(0) 推荐(0) 编辑
摘要:A simple combination of Implement Trie (Prefix Tree) and Word Search. If you've solved them, this problem will become easy :-)The following code is ba... 阅读全文
posted @ 2015-08-17 22:49 jianchao-li 阅读(362) 评论(0) 推荐(0) 编辑
摘要:You need to understand what a Trie is before writing the code :-) This link has a nice solution, whose code is rewritten below. 1 class TrieNode { 2 p... 阅读全文
posted @ 2015-08-17 21:24 jianchao-li 阅读(234) 评论(0) 推荐(0) 编辑
摘要:This problem is purely to test your coding ability. Just go ahead :-) 1 class Solution { 2 public: 3 string countAndSay(int n) { 4 string ... 阅读全文
posted @ 2015-08-17 17:06 jianchao-li 阅读(183) 评论(0) 推荐(0) 编辑
摘要:Try to split all the numbers into two groups with each of the target one in different groups. Refer to this link for a nice explanation.The code is wr... 阅读全文
posted @ 2015-08-17 16:16 jianchao-li 阅读(191) 评论(0) 推荐(0) 编辑
摘要:Problem Description:Given an array ofnintegersnumsand atarget, find the number of index tripletsi, j, kwith0 & nums, int target) { 4 sort(nums... 阅读全文
posted @ 2015-08-17 15:50 jianchao-li 阅读(5074) 评论(0) 推荐(0) 编辑
摘要:Well, no matter whether the number is happy or not, its sum-of-squared-digits sequance has a cycle. Well, do you still remember the algorithm for dete... 阅读全文
posted @ 2015-08-17 00:12 jianchao-li 阅读(177) 评论(0) 推荐(0) 编辑
摘要:Dynamic ProgrammingThere is a nice introduction to the DP algorithm in thisWikipedia article. The idea is to maintain a running maximumsmaxand a curre... 阅读全文
posted @ 2015-08-16 15:55 jianchao-li 阅读(321) 评论(0) 推荐(0) 编辑
摘要:Well, if you have no idea of other methods, try to compue the result for all the numbers ranging from 1 to 20 and then you will see the regularity. Af... 阅读全文
posted @ 2015-08-16 13:36 jianchao-li 阅读(225) 评论(0) 推荐(0) 编辑
摘要:The basic idea is to start fromrootand add it to the currentpath, then we recursively visit itsleftandrightsubtrees if they exist; otherwise, we have ... 阅读全文
posted @ 2015-08-16 10:48 jianchao-li 阅读(336) 评论(0) 推荐(0) 编辑
摘要:The idea is to add the two binary numbers (represented as strings) from back to forth bit by bit and store the result in the longer string. After the ... 阅读全文
posted @ 2015-08-16 00:36 jianchao-li 阅读(200) 评论(0) 推荐(0) 编辑
摘要:This problem is just similar toMinimum Depth of Binary Tree.The first solution also uses recursion (not sure whether it can be called DFS).1 class Sol... 阅读全文
posted @ 2015-08-15 18:43 jianchao-li 阅读(182) 评论(0) 推荐(0) 编辑
摘要:Well, this problem has the highest acceptance rate among all OJ problems. It has a very easy 1-line reursive solution. I am not sure whether this one ... 阅读全文
posted @ 2015-08-15 18:23 jianchao-li 阅读(175) 评论(0) 推荐(0) 编辑
摘要:The basic idea is to use binary search: keep two pointerslandrfor the current search range, then find the middle elementnums[mid]in this range. Ifnums... 阅读全文
posted @ 2015-08-15 17:41 jianchao-li 阅读(173) 评论(0) 推荐(0) 编辑
摘要:The idea is to search for the left and right boundaries of target via two binary searches. Well, some tricks may be needed. Take a look at this link :... 阅读全文
posted @ 2015-08-15 16:23 jianchao-li 阅读(181) 评论(0) 推荐(0) 编辑
摘要:The most obvious idea is to maintain two divisors to get the most and least significantdigits and compare them. Well, there are much more clever ideas... 阅读全文
posted @ 2015-08-15 15:32 jianchao-li 阅读(157) 评论(0) 推荐(0) 编辑
摘要:The idea is to add the two numbers (represented by linked lists) nodes after nodes and store the result in the longer list. Since we may append the ad... 阅读全文
posted @ 2015-08-15 14:59 jianchao-li 阅读(152) 评论(0) 推荐(0) 编辑
摘要:The basic idea is to maintain a hash table for each elementnuminnums, usingnumas key and its index (1-based) as value. For eachnum, search fortarget -... 阅读全文
posted @ 2015-08-15 11:51 jianchao-li 阅读(168) 评论(0) 推荐(0) 编辑
摘要:This problem can be solved easily if we are allowed to use more than O(1) space. For example, you may create a copy of the original matrix (O(mn)-spac... 阅读全文
posted @ 2015-08-15 11:33 jianchao-li 阅读(171) 评论(0) 推荐(0) 编辑
摘要:This problem is a little tricky at first glance. However, if you have finished theHouse Robberproblem, this problem can simply bedecomposed into two H... 阅读全文
posted @ 2015-08-15 11:23 jianchao-li 阅读(207) 评论(0) 推荐(0) 编辑
摘要:Since we are not allowed to rob two adjacent houses, we keep two variablespreandcur. During thei-th loop,prerecords the maximum profit that we do not ... 阅读全文
posted @ 2015-08-15 11:12 jianchao-li 阅读(157) 评论(0) 推荐(0) 编辑
摘要:Well, this problem can be solved in 1-line clearly. Take a look at this link:-)1 class Solution {2 public:3 string convertToTitle(int n) {4 ... 阅读全文
posted @ 2015-08-15 00:41 jianchao-li 阅读(195) 评论(0) 推荐(0) 编辑
摘要:Problem Description:There are a row ofnhouses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each h... 阅读全文
posted @ 2015-08-14 14:44 jianchao-li 阅读(3007) 评论(0) 推荐(0) 编辑
摘要:This problem can be solved by DP elegantly. You may refer to this link for the code and explanations.I try to rewrite the code, also in C++, but find ... 阅读全文
posted @ 2015-08-11 22:08 jianchao-li 阅读(192) 评论(0) 推荐(0) 编辑
摘要:The idea is just to perform the addition from right to left as usual :-)Note that the result may be longer than the originaldigitsby1number (the carry... 阅读全文
posted @ 2015-08-11 09:43 jianchao-li 阅读(152) 评论(0) 推荐(0) 编辑
摘要:The idea is just to add the elements in the spiral order. First the up-most row (u), then the right-most column (r), then the down-most row (d), and f... 阅读全文
posted @ 2015-08-11 08:51 jianchao-li 阅读(204) 评论(0) 推荐(0) 编辑
摘要:The idea is just to generate the matrix in the spiral order. First the up-most row (u), then the right-most column (r), then the down-most row (d), an... 阅读全文
posted @ 2015-08-11 08:42 jianchao-li 阅读(162) 评论(0) 推荐(0) 编辑
摘要:A simple and in-place idea: first reverse the image in row-major order and then transpose it :-) 1 class Solution { 2 public: 3 void rotate(vector... 阅读全文
posted @ 2015-08-10 13:45 jianchao-li 阅读(145) 评论(0) 推荐(0) 编辑
摘要:This problem has a naive solution usingsortand linear scan. The suggested solution uses the idea ofbucket sort. The following is a C++ implementation ... 阅读全文
posted @ 2015-08-10 11:03 jianchao-li 阅读(277) 评论(0) 推荐(0) 编辑
摘要:This problem can be solved easily once you find the regularities :-) This link has done it for you. You may refer to its Python version. I rewrite it ... 阅读全文
posted @ 2015-08-08 14:52 jianchao-li 阅读(1431) 评论(0) 推荐(0) 编辑
摘要:Problem Description:Given an array of meeting time intervals consisting of start and end times[[s1,e1],[s2,e2],...](si& intervals) { 4 sort(in... 阅读全文
posted @ 2015-08-08 14:24 jianchao-li 阅读(4694) 评论(1) 推荐(0) 编辑
摘要:Problem Description: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person 阅读全文
posted @ 2015-08-08 13:44 jianchao-li 阅读(2268) 评论(0) 推荐(0) 编辑
摘要:Problem Description:A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).Write a function to dete... 阅读全文
posted @ 2015-08-06 16:09 jianchao-li 阅读(2115) 评论(0) 推荐(0) 编辑
摘要:Problem Description:This is afollow upofShortest Word Distance. The only difference is now you are given the list of words and your method will be cal... 阅读全文
posted @ 2015-08-05 17:17 jianchao-li 阅读(3639) 评论(0) 推荐(0) 编辑
摘要:Problem Description:Given a list of words and two wordsword1andword2, return the shortest distance between these two words in the list.For example,Ass... 阅读全文
posted @ 2015-08-05 16:02 jianchao-li 阅读(3771) 评论(0) 推荐(0) 编辑
摘要:Problem Description: Implement an iterator to flatten a 2d vector.For example,Given 2d vector =[ [1,2], [3], [4,5,6]]By callingnextrepeatedly unti... 阅读全文
posted @ 2015-08-05 15:16 jianchao-li 阅读(2978) 评论(0) 推荐(0) 编辑
摘要:A relatively difficult tree problem. Well, a recursive solution still gives clean codes. The tricky part of this problem is how to record the result. ... 阅读全文
posted @ 2015-08-04 21:52 jianchao-li 阅读(230) 评论(0) 推荐(0) 编辑
摘要:An extension of Best Time to Buy and Sell Stock III. The idea is still to use dynamic programming (see here for detailed introduction). However, in th... 阅读全文
posted @ 2015-08-04 17:23 jianchao-li 阅读(218) 评论(0) 推荐(0) 编辑
摘要:Personally, this is a relatively difficult DP problem. This link posts a typical DP solution to it. You may need some time to get how it works.The cod... 阅读全文
posted @ 2015-08-03 14:49 jianchao-li 阅读(217) 评论(0) 推荐(0) 编辑
摘要:The function signature has been updated to return a more intuitivevector>which treats a single string as a group of anagrams consisting of only itself... 阅读全文
posted @ 2015-08-01 17:07 jianchao-li 阅读(869) 评论(0) 推荐(0) 编辑
摘要:Hash TableThis idea uses a hash table to record the times of appearances of each letter in the two stringssandt. For each letter ins, it increases the... 阅读全文
posted @ 2015-08-01 14:14 jianchao-li 阅读(292) 评论(0) 推荐(0) 编辑
摘要:To solve this problem, you first need to understand it well. The key problem is tell the difference of scramble from permutations. You may refer to th... 阅读全文
posted @ 2015-08-01 11:58 jianchao-li 阅读(206) 评论(0) 推荐(0) 编辑