摘要: Say you have an array for which the ith element is the price of a given stock on day i. 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. Solution: For each element, calculate the max differe. 阅读全文
posted @ 2014-03-12 09:27 beehard 阅读(156) 评论(0) 推荐(0) 编辑
摘要: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].Solution: dfs(recursion) 1 class Solution { 2 public: 3 vector > res; 4 5 void permute(vector& num, vector& per.. 阅读全文
posted @ 2014-03-12 07:54 beehard 阅读(157) 评论(0) 推荐(0) 编辑
摘要: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&qu 阅读全文
posted @ 2014-03-12 04:53 beehard 阅读(117) 评论(0) 推荐(0) 编辑
摘要: 2SumGiven an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the t... 阅读全文
posted @ 2014-03-12 02:29 beehard 阅读(258) 评论(0) 推荐(0) 编辑