[leetcode_18]4Sum

给定数组,找出数组中4个数之和为目标数的所有不重复情况。 o(n^3*logn)险过。应该有更快的方法。 int cmp(int a,int b) { return a < b; } class Solution { public: vector<vector<int> > fourSum(vector<int> &num, int target) { sort(nu……

[leetcode_123]Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock系列第三题。之前两题分别说可以买卖任意多次和只能买卖一次。 此题是买卖两次。 买卖两次,找个节点分割问题,就把问题变成两个买卖一次的问题。但是超时。时间复杂度要到o(n^2) 另外一个思路。 按照买卖一次的方法处理正序,逆序 然后存储到达每个点时的max值。线性解法。 class Solut……

[leetcode_99]Recover Binary Search Tree

交换二叉搜索树的两个节点,要求复原。 不确定我的做法对不对,我只是在value的程度上把他们复原了。但是原文说不要破坏他们的结构? 想法很简单中序遍历+线性数组纠正 class Solution { public: void recoverTree(TreeNode *root) { if(root == NULL)return ; treeArray.clear(); inOrder(……

[leetcode_49]Anagrams

给定多个字符串,找出那些Anagrams的字符串组。 Anagrams:eat ate eta 这个题 排序,再排序。 int cmp(int a,int b) { return a < b; } struct Item { int index; string val; }; int cmpitem(Item a,Item b) { return a.val < b.val; } class Solution { public: vector anagrams(vector &……