摘要:
对于一个数组,找出这样一个序列对(i, j),满足A[i] < A[j],且使 j - i的值最大,输出j - i的值。这道题最初想到的是O(n^2)的解答,后来想到了用MergeSort时来记录最大序列对的方法,也可以用一个队列来做。总之解法还挺多。最后和http://ac.jobdu.com/problem.php?cid=1039&pid=19何海涛的这题非常相似。方法1:MergeSort,数组的元素需要记录原始的元素索引。 1 #include <iostream> 2 using namespace std; 3 4 struct Node 5 { 6 i 阅读全文
摘要:
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数组,记录每个数被使用的情况,如果当前的数和它前一个数相同,则前一个数必须被使用了,当前数才能被使用,这样就能 阅读全文
摘要:
Given an unsorted array provide two indices n1 and n2 such that if we only sort the elements between n1 and n2,then the whole array will become sorted.n1-n2 should be as minimum as possible.http://www.careercup.com/question?id=4345015参考上面一位大神的解答,非常精彩,不过理解起来有点费力。Let A be the array and T = 0.While A[T 阅读全文
摘要:
Find kth number in a BST递归实现,按照左根右的顺序来实现 1 #include <iostream> 2 using namespace std; 3 4 struct Node 5 { 6 Node *left; 7 Node *right; 8 int val; 9 Node():left(NULL), right(NULL){}10 };11 12 Node *findKthElement(Node *node, int &K)13 {14 if (node == NULL)15 return NULL;1... 阅读全文
摘要:
Sort an input string according to the given order string. There might be characters in input string that are not present in the order string and vice-versa. The characters of input string that are not present in the order string should come at the end of the output string in any order. Write code... 阅读全文