09 2013 档案

摘要:Given two words (startandend), and a dictionary, find the length of shortest transformation sequence fromstarttoend, such that:Only one letter can be changed at a timeEach intermediate word must exist in the dictionaryFor example,Given:start="hit"end="cog"dict=["hot",&q 阅读全文
posted @ 2013-09-17 11:08 feiling 阅读(2438) 评论(1) 推荐(0)
摘要:基本问题:1.两个链表中的第一个公共结点[解题思路]a.先求得两个链表的长度,得到链表长度差db.根据链表长度差,首先让长链表的指针先走d-1步,之后两个指针一起走,发现相同结点时就是公共结点 int len1 = 0, len2 = 0; ListNode p1 = head1, p2 = head2; while (p1 != null) { len1++; p1 = p1.next; } while (p2 != null) { len2++; ... 阅读全文
posted @ 2013-09-13 14:11 feiling 阅读(387) 评论(0) 推荐(0)
摘要:数组al[0...mid-1]和al[mid...num-1]两个部分都已经分别排好序。要求合并使得整个数组al有序。请给出合并merge的代码。要求空间复杂度为O(1)。 1 /* 2 数组a[begin, mid] 和 a[mid+1, end]是各自有序的,对两个子段进行Merge得到a[begin , end]的有序数组。 要求空间复杂度为O(1) 3 方案: 4 1、两个有序段位A和B,A在前,B紧接在A后面,找到A的第一个大于B[0]的数A[i], A[0...i-1]相当于merge后的有效段,在B中找到第一个大于A[i]的数B[j], 5 对数组A[i...j-1]循环右移j- 阅读全文
posted @ 2013-09-12 16:23 feiling 阅读(315) 评论(0) 推荐(0)
摘要:A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it.For example,Given encoded message"12", it cou 阅读全文
posted @ 2013-09-09 23:35 feiling 阅读(422) 评论(0) 推荐(0)
摘要:Given a stringSand a stringT, count the number of distinct subsequences ofTinS.A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,"ACE" 阅读全文
posted @ 2013-09-08 20:05 feiling 阅读(402) 评论(0) 推荐(0)
摘要:leetcode上关于permutation有如下几题Permutation SequenceNext PermutationPermutationsPermutations II 阅读全文
posted @ 2013-09-07 22:46 feiling 阅读(305) 评论(0) 推荐(0)
摘要:Give a string, which only contains a-z. List all the permutation of upcase and lowcase.For example, str = "ab", the output should be"ab", "aB", "Ab", "AB"for str = "abc", the output should be"abc", "abC", "aBc" 阅读全文
posted @ 2013-09-07 16:28 feiling 阅读(401) 评论(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.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 n 阅读全文
posted @ 2013-09-06 15:03 feiling 阅读(739) 评论(0) 推荐(0)
摘要:The string"PAYPALISHIRING"is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I GY I RAnd then read line by line:"PAHNAPLSIIGYIR"Write the code that will take a string and 阅读全文
posted @ 2013-09-06 11:36 feiling 阅读(729) 评论(0) 推荐(0)
摘要: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 XX X O XX O X XAfter running your function, the board should be:X X X XX X X XX X X 阅读全文
posted @ 2013-09-05 19:24 feiling 阅读(2176) 评论(0) 推荐(0)
摘要:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.» Solve this problem[解题思路]从前往后扫描,用一个临时变量记录分段数字。如果当前比前一个大,说明这一段的值应该是当前这个值减去上一个值。比如IV = 5 – 1否则,将当前值加入到结果中,然后开始下一段记录。比如VI = 5 + 1, II=1+1 1 public static int romanToInt(String s) { 2 // St... 阅读全文
posted @ 2013-09-04 22:54 feiling 阅读(220) 评论(0) 推荐(0)
摘要:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999. 1 public String intToRoman(int num) { 2 // Start typing your Java solution below 3 // DO NOT write main() function 4 StringBuffer result = new StringBuffer(); 5 ... 阅读全文
posted @ 2013-09-04 22:31 feiling 阅读(256) 评论(0) 推荐(0)
摘要:Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given"25525511135",return["255.255.11.135", "255.255.111.35"]. (Order does not matter)[解题思路]DFS + Backtracking给定的字符串分成4段,每段都0 剩余段数*32.剩余位数 restoreIpAddr 阅读全文
posted @ 2013-09-04 19:50 feiling 阅读(748) 评论(1) 推荐(0)
摘要: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 emtpy 阅读全文
posted @ 2013-09-04 17:01 feiling 阅读(863) 评论(0) 推荐(0)
摘要:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.[解题思路]1.brute force枚举所有sub-matrix(O(N^2), N = m*n) ,检查每个子矩阵是不是都是1,如果是更新最大面积,检查子矩阵是否都是1需要花费O(N). 故总的时间为O(N^3) N = m*n可以过小数据,大数据直接TLE 1 public int maximalRectangle(char[][] matr 阅读全文
posted @ 2013-09-02 17:23 feiling 阅读(351) 评论(0) 推荐(0)
摘要:Givenn, generate all structurally uniqueBST's(binary search trees) that store values 1...n.For example,Givenn= 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ ... 阅读全文
posted @ 2013-09-02 11:31 feiling 阅读(768) 评论(0) 推荐(0)
摘要:Givens1,s2,s3, find whethers3is formed by the interleaving ofs1ands2.For example,Given:s1="aabcc",s2="dbbca",Whens3="aadbbcbcac", return true.Whens3="aadbbbaccc", return false.[解题思路]DPLet F(i, j) denote if s1 of length i and s2 of length j could form s3 of len 阅读全文
posted @ 2013-09-02 10:50 feiling 阅读(585) 评论(0) 推荐(0)