- 博客(121)
- 收藏
- 关注
原创 LeetCode 306. Additive Number
I first thought about the commented out method. However, that one is hard to split the numbers which start with '0'.Second Method is from leetcode discuss forum. // try backtracking?//bool is
2016-05-28 02:27:44 303
原创 Leetcode 317. Shortest Distance from All Buildings
#include #include #include #include using namespace std;/* You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down
2016-05-28 00:17:00 770
原创 LeetCode 291. Word Pattern II
BackTracking.#include #include #include #include using namespace std;/* Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that th
2016-05-27 12:20:46 661
原创 LeetCode 218. The Skyline Problem
This is one of the very few notorious problems in LeetCode I dislike.EPI code, but need to learn how to build datastructure to simplify the coding process.#include #include using namespace std;
2016-05-27 07:30:00 243
原创 LeetCode 330. Patching Array
Before solving this problem. Better to have a look on the this one first.EPI: Compute the smallest non-constructible change.:Suppose you have some coins, there are some amount of changes that you
2016-05-27 03:26:59 469
原创 LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
The most important thing is to find the correct index.void build(vector& postorder, vector& inorder, int index, int begin, int end, TreeNode*& root) { if(begin >= end) return; int n = postorder.
2016-05-26 23:07:38 167
原创 LeetCode 311. Sparse Matrix Multiplication
The Second solution is highly recommended.#include #include #include using namespace std;/* Given two sparse matrics A and B, return the result of AB. You may assume that A's column number i
2016-05-26 11:18:53 371
原创 LeetCode 337. House Robber III
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour
2016-05-26 08:54:40 198
转载 LeetCode 95. Unique Binary Search Trees II
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas
2016-05-26 07:15:57 195
原创 LeetCode 308. Range Sum Query 2D - Mutable
#include #include using namespace std;/* Given a 2D matrix, find the sum of the elements inside the rectangle defined by its upper left corner(row1, col1) and lower right corner(row2, col2).
2016-05-26 03:00:54 602
转载 LeetCode 333. Largest BST Subtree
I am absolutely not good at bottom-up traversal./* Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it
2016-05-26 01:59:01 396
原创 LeetCode 269. Alien Dictionary
This solution is not done yet. I didn't check the validation of DAG.#include #include #include #include #include #include using namespace std;/* There is a new alien language which uses th
2016-05-25 12:11:58 387
原创 LintCode 143. Sort Colors II
This is a very interesting question! I like it!#include #include #include using namespace std;/* Given an array of n objects with K difference colors. sort them so that objects of the same
2016-05-25 11:03:14 320
原创 LintCode 49. Sort Letters by Case
Two Pointers variation #include #include using namespace std;/* Given a string which contains only letters. Sort it by lower case first and upper case second*/void sortLetters(string& letter
2016-05-25 10:41:10 247
原创 LintCode 387. The Smallest Difference
Two Pointers, Time complexity O(N), Space O(1)#include #include #include #include using namespace std;/* Given two array of integer (A and B). Now we are going to find a element in A and ano
2016-05-25 10:23:19 342
原创 LeetCode 210. Course Schedule II
This is standard topological sort1: First solution, using stack.#include #include #include #include #include using namespace std;/* For example 2, [[1, 0]] There are a total of 2 course
2016-05-25 09:53:19 209
原创 Some C++ Concepts
1: Abstract class#include using namespace std;/* a class is declared as abstract by declaring at least one of its function as pure virtual function. A pure virtual function is specified by pl
2016-05-25 05:34:47 351
原创 LeetCode 271. Encode and Decode Strings
This is to encode the string not to COMPRESS the string.This is one possible answer.#include #include #include using namespace std;string encode(vector& strs) { string res = ""; for(int i
2016-05-24 12:41:54 332
原创 LeetCode 307. Range Sum Query - Mutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.The update(i, val) function modifies nums by updating the element at index i to val.Examp
2016-05-24 11:47:35 204
原创 LeetCode 68. Text Justification
This is actually not the LeetCode version answer. But I think this is more likely in real word.The Code is copied from the EPI. I have no clue to solve this problem at the first sight.#include #
2016-05-24 09:17:41 256
原创 LeetCode 254. Factor Combinations
Almost the same idea as combinations (permutations)#include #include using namespace std;/* Numbers can be regarded as product of its factor. For example. 8 = 2 x 2 x 2 = 2 x 4 write a
2016-05-24 05:09:55 361
原创 LeetCode 253. Meeting Rooms II
#include #include #include using namespace std;/* Given an array of meeting time interval consiting of start and end times [[s1, e1], [s2, e2], [s3, e3]] (si < ei), find the mininum number of
2016-05-24 04:35:16 239
原创 LeetCode 261. 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 =
2016-05-23 12:36:40 398
原创 LeetCode 162. Find Peak Element
#include #include using namespace std;/* A peak element is an element that is greater than its neighbors. Given an input array where nums[i] != nums[i+1], find a peak element and return its
2016-05-23 11:31:50 230
原创 LeetCode 31. Next Permutation
#include #include #include using namespace std;/* Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is
2016-05-23 11:04:53 204
原创 LeetCode 207. Course Schedule
There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as
2016-05-23 09:24:01 292
原创 LeetCode 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return[2, 2].Note:Each element in the result should appear as many ti
2016-05-22 08:22:43 181
原创 LeetCode 349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return[2].Method 1: using two set. O(N) time, O(m+n) space.class Solu
2016-05-22 08:16:01 201
原创 LeetCode 10. Regular Expression Matching
'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(c
2016-05-18 12:49:21 197
转载 LeetCode 45. Jump Game II
class Solution {public: int jump(vector& nums) { int size = nums.size(); int i = 0, step = 0; int curStepReach = nums[0]; //furthest idex the current step can
2016-05-18 11:55:46 188
原创 LeetCode 332. Reconstruct Itinerary
I struggled a while for looking the best data structure to do this DFS... the following is the best solution I think....#include #include #include #include #include #include using namespace st
2016-05-18 01:46:04 339
原创 LeetCode 339. Nested List Weight Sum
Given a nested list of integers, return the sum of all integers in the list weighted by their depth.Each element is either an integer, or a list -- whose elements may also be integers or other lis
2016-05-17 12:04:00 450
原创 LeetCode 298. Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path.The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connecti
2016-05-17 11:57:12 213
原创 LeetCode 341. Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it.Each element is either an integer, or a list -- whose elements may also be integers or other lists.Example 1:Given the list [
2016-05-17 11:26:32 370
原创 LeetCode 187. Repeated DNA Sequences
#include #include #include using namespace std;/* All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes us
2016-05-17 10:00:07 162
原创 LeetCode 267. Palindrome Permutation II
Code is not that clean....First find out how many odd number of characters.... if there are more than one, it can't form palindrome.If there is only one odd number character, if the odd number is
2016-05-17 07:37:38 322
原创 LeetCode 336. Palindrome Pairs
#include #include #include #include using namespace std;/* Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list. So that the concatenation of two words.
2016-05-17 05:23:53 437
原创 LeetCode 214. Shortest Palindrome
This problem was solved using some ideas from GeeksforGeeks.The longest palindrome string would be reverse itself + itself.We need to find the max prefix of the new string.The shortest palindrom
2016-05-17 00:11:08 388
原创 LeetCode 5. Longest Palindromic Substring
#include #include using namespace std;/* Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longes
2016-05-16 22:33:56 203
原创 LeetCode 132. Palindrome Partitioning II
#include #include #include using namespace std;/* Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome par
2016-05-16 11:56:30 214
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人