- 博客(158)
- 收藏
- 关注
原创 Leetcode: Wildcard Matching
class Solution {public: bool isMatch(const char *s, const char *p) { const char *sp = NULL; const char *ss = NULL; while(*s) { if(*p == *s || *p == '?'
2014-08-10 23:06:52 142
原创 Leetcode: Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total nu
2014-08-10 22:46:43 144
原创 Leetcode: LRU Cache
class LRUCache{private: int cap;int size;list l;unordered_map::iterator> um;public: LRUCache(int capacity) { cap = capacity; size = 0; } int get(int key) {
2014-08-10 22:06:43 105
原创 Leetcode: String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ca
2014-08-10 12:28:18 115
原创 Leetcode: Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy approach; that i
2014-08-09 22:43:54 101
原创 Longest Palindromic Substring
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 longest palindromic substring.c++:class Solu
2014-08-09 22:28:15 86
原创 Leetcode: Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.For example, given s = "aab",Return
2014-08-09 22:19:10 93
原创 Leetcode: Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example,X X X XX O O X
2014-08-09 22:07:43 123
原创 Leetcode: Word Break II
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.Return all such possible sentences.For example, givens = "
2014-08-09 13:32:33 100
原创 Leetcode: Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.c++:class Solution {public: stri
2014-08-09 13:02:30 93
原创 Leetcode: Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3
2014-08-09 12:36:18 91
原创 Leetcode: Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be non
2014-08-09 01:56:54 79
原创 Leetcode: Populating Next Right Pointers in Each Node II
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL)
2014-08-09 01:49:24 89
原创 Leetcode: First Missing Positive
Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant
2014-08-09 01:22:53 91
原创 Leetcode: Clone Graph
c++:/** * Definition for undirected graph. * struct UndirectedGraphNode { * int label; * vector neighbors; * UndirectedGraphNode(int x) : label(x) {}; * }; */class Solution {pub
2014-08-08 21:01:28 119
原创 Leetcode: Valid Number
Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambiguo
2014-08-08 15:39:10 93
原创 Leetcode: Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \ gr
2014-08-08 14:11:34 83
原创 Leetcode: Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.E
2014-08-08 12:06:51 84
原创 Leetcode: Merge Intervals
Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].c++:/** * Definition for an interval. * struct I
2014-08-08 12:02:40 107
原创 Leetcode: Implement strStr()
Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.c++:class Solution {public: char *strStr(char *haystack, char
2014-08-08 11:09:54 95
原创 Leetcode: Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input st
2014-08-08 10:57:55 105
原创 Leetcode: Simplify Path
class Solution {public: string simplifyPath(string path) { stack q; string name; name.clear(); string ret = ""; int len = path.length(); if(len ==
2014-08-07 21:40:32 73
原创 Leetcode: N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.c++:class Solution {public: int ret; int* use; i
2014-08-07 21:31:51 63
原创 Leetcode: Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is
2014-08-07 20:59:42 90
原创 Leetcode: Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [
2014-08-07 20:48:19 65
原创 Leetcode: Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]
2014-08-07 20:43:50 68
原创 Leetcode: Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BAN
2014-08-07 16:11:44 112
原创 Leetcode: Median of Two Sorted Arrays
class Solution {public: double findHelper(int A[], int m, int B[], int n, int k) { if(m > n) { return findHelper(B, n, A, m, k); } if(m == 0) { ret
2014-08-07 15:40:29 87
原创 Leetcode: Permutation Sequence
class Solution {public: string getPermutation(int n, int k) { int factor[8] = {1,2,6,24,120,720,5040,40320}; string ss = "123456789"; string s(ss,0,n); string ret
2014-08-06 22:10:02 66
原创 Leetcode: Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", ret
2014-08-06 21:53:08 57
原创 Leetcode: Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:Only one letter can be changed at a timeEach intermediate word m
2014-08-06 21:38:54 91
原创 Leetcode: Word Break
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet"
2014-08-06 16:27:04 70
原创 Leetcode: Longest Substring Without Repeating Characters
class Solution {public: int lengthOfLongestSubstring(string s) { int size = s.size(); if(size <= 1) { return size; } int i; int use[256
2014-08-06 11:17:46 78
原创 Leetcode: Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to
2014-08-05 22:29:35 73
原创 Leetcode: Reverse Linked List II
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *re
2014-08-05 21:37:36 81
原创 Leetcode: Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devis
2014-08-05 21:26:52 73
原创 Leetcode: N-Queens
class Solution {public: vector > ret; bool *ch; bool check(vector &ans, int index, int p) { int i; int len = ans.size(); for(i = 0;i < index;i ++) {
2014-08-05 21:20:21 63
原创 Leetcode: Binary Tree Zigzag Level Order Traversal
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Soluti
2014-08-05 21:09:43 67
原创 Leetcode: Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.c++:/** * Definition for singly-
2014-08-04 22:20:33 80
原创 Leetcode: Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combina
2014-08-04 21:49:07 78
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人