09 2012 档案

摘要: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 "BANC".Note:If there is no such window in S that covers all characters in T, return the 阅读全文
posted @ 2012-09-29 03:53 ETCOW 阅读(369) 评论(0) 推荐(0)
摘要:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any point in time.递归: 1 public static int MinimumPathSum(List<List<int>> grid) 2 { 3 ... 阅读全文
posted @ 2012-09-29 03:32 ETCOW 阅读(302) 评论(0) 推荐(0)
摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 1 public static ListNode MergeTwoSortedLists(ListNode A, ListNode B) 2 { 3 if (A == null) 4 return B; 5 ... 阅读全文
posted @ 2012-09-29 00:26 ETCOW 阅读(338) 评论(0) 推荐(0)
摘要:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. 1 public static void MergeSortedArray(int[] A, int m, int[] B, i... 阅读全文
posted @ 2012-09-28 05:04 ETCOW 阅读(304) 评论(0) 推荐(0)
摘要:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. public class ListNode { public int val; public ListNode next; public ListNode() { } public ListNode(int x) { th... 阅读全文
posted @ 2012-09-27 22:53 ETCOW 阅读(510) 评论(0) 推荐(0)
摘要: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]. 1 public static List<Interval> MergeIntervals(List<Interval> intervals) 2 { 3 List<Interval> ret = new List<Interval>(); 4 5 ... 阅读全文
posted @ 2012-09-27 22:40 ETCOW 阅读(400) 评论(0) 推荐(0)
摘要:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 1 public static double MedianofTwoSortedArrays(int[] A, int[] B) 2 { 3 int m = A.Length; 4 int n... 阅读全文
posted @ 2012-09-26 23:09 ETCOW 阅读(878) 评论(0) 推荐(0)
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6.More practice:If you have figured out the O(n) solution, try coding another solution 阅读全文
posted @ 2012-09-25 23:51 ETCOW 阅读(358) 评论(0) 推荐(0)
摘要:Given a 2D matrix fill with 0's and 1's, find the largest rectangle containing all ones and return its area. 1 public static int MaximalRectangle(List<List<char>> matrix) 2 { 3 int n = matrix.Count; 4 int m = matrix[0].Count; 5 int curr_area = 0; 6... 阅读全文
posted @ 2012-09-25 05:30 ETCOW 阅读(381) 评论(0) 推荐(0)
摘要:Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()", which has length = 2.Another example is ")()())", where the lo 阅读全文
posted @ 2012-09-21 04:47 ETCOW 阅读(395) 评论(0) 推荐(0)
摘要:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1. 阅读全文
posted @ 2012-09-21 04:09 ETCOW 阅读(410) 评论(0) 推荐(0)
摘要: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. 1 public static string LongestPalindromicSubstring(string s) 2 { 3 if (s.Length <= 1) 4 ... 阅读全文
posted @ 2012-09-20 05:37 ETCOW 阅读(435) 评论(0) 推荐(0)
摘要:Write a function to find the longest common prefix string amongst an array of strings 1 public static string LongestCommonPrefix(List<string> strs) 2 { 3 if (strs.Count == 0) 4 return ""; 5 if (strs.Count == 1) 6 return strs[0];... 阅读全文
posted @ 2012-09-20 03:42 ETCOW 阅读(322) 评论(0) 推荐(0)
摘要:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", &q 阅读全文
posted @ 2012-09-20 03:29 ETCOW 阅读(457) 评论(0) 推荐(0)
摘要:Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defined as a character sequence consists of non-space characters only.For example, Given s = "He 阅读全文
posted @ 2012-09-19 05:42 ETCOW 阅读(274) 评论(0) 推荐(0)
摘要:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].The largest rectangle is shown in the shaded area, which has 阅读全文
posted @ 2012-09-15 00:17 ETCOW 阅读(542) 评论(2) 推荐(0)
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of jumps.For example:Given array A = [2,3,1,1,4]The minimum 阅读全文
posted @ 2012-09-14 22:39 ETCOW 阅读(379) 评论(0) 推荐(0)
摘要:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine if you are able to reach the last index.For example:A = [2,3,1,1,4], return true.A = [3,2,1,0,4], return fals 阅读全文
posted @ 2012-09-13 22:18 ETCOW 阅读(270) 评论(0) 推荐(0)
摘要:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999. 1 static string[] D1 = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" }; 2 static string[] D1 阅读全文
posted @ 2012-09-13 22:06 ETCOW 阅读(397) 评论(0) 推荐(0)
摘要: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.Example 1:Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].Example 2:Given [1,2],[3,5],[ 阅读全文
posted @ 2012-09-13 22:00 ETCOW 阅读(632) 评论(0) 推荐(0)
摘要:Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.needle in the haystack 大海捞针的意思其实就是找出 “短字符串” 在 “长字符串” 中的位置。 1 public static string strStr(string haystack, string needle) 2 { 3 if (haystack.Length == 0) 4... 阅读全文
posted @ 2012-09-07 05:47 ETCOW 阅读(1447) 评论(2) 推荐(0)
摘要:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.For example, given n = 2, return [0,1,3,2]. Its gray c 阅读全文
posted @ 2012-09-07 04:58 ETCOW 阅读(537) 评论(0) 推荐(0)
摘要:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()" 1 public static List<string> GenerateParentheses 阅读全文
posted @ 2012-09-06 05:00 ETCOW 阅读(500) 评论(2) 推荐(0)
摘要: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 space.JAVA 正解如下: 1 public class Solution { 2 public int firstMissingPositive(int[] A) { 3 // Start ... 阅读全文
posted @ 2012-09-06 04:37 ETCOW 阅读(521) 评论(9) 推荐(1)