摘要:
Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"利用一个递归函数来产生,用一个参数来记录当前剩下可对应的左扩号数,只有leftNum > 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" 阅读全文
摘要:
Given two wordsword1andword2, find the minimum number of steps required to convertword1toword2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) Insert a characterb) Delete a characterc) Replace a character这题真是火大啊,犯了个低级错误,在private里开了个f[1000][1000],然后每次 阅读全文
摘要:
Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at 阅读全文
摘要:
Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.用类似动态规划的思想,到第i天买入,那么我能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的。 阅读全文
摘要:
http://www.careercup.com/question?id=3315662given a string find the number of distinct substrings of the string.ex:input-> aaaaoutput-> 4(a, aa, aaa, aaaa)input->abcdoutput->10(a, b, c, d, ab, bc, cd, abc, bcd, abcd)我的方法避免重复是设置一个used数组,记录每个数被使用的情况,如果当前的数和它前一个数相同,则前一个数必须被使用了,当前数才能被使用,这样就能 阅读全文