摘要: 思路: DP,设置数组res,存储以i结尾的连续数组和的最大值。如果i之前子数组最大和小于等于0,那么res[i] = nums[i],否则res[i] = nums[i] + res[i 1]。复杂度为$O(n)$。 阅读全文
posted @ 2017-06-08 09:55 UniMilky 阅读(108) 评论(0) 推荐(0) 编辑
摘要: 思路: 先转置,然后对称。复杂度为$O(n^2)$。每次用swap交换,额外占用一个空间,所以空间复杂度为$O(1)$。 class Solution { public: void rotate(vector & matrix) { for(int i = 0; i 阅读全文
posted @ 2017-06-08 09:19 UniMilky 阅读(133) 评论(0) 推荐(0) 编辑
摘要: 思路: 类似Combination Sum,但是数组有重复元素,同时要求元素不能重复使用。复杂度和Combination Sum类似。 阅读全文
posted @ 2017-06-08 08:53 UniMilky 阅读(104) 评论(0) 推荐(0) 编辑
摘要: 思路: 利用回溯,其运行过程可以看成遍历一颗4叉树(因为要求不重复,所以中间剪掉了部分树枝),复杂度为$O(n^h)$,$h$为树的高度,这里$h$相当于只用数组里最小的数加起来大于等于target时所用的数目。比如[2,3,6,7],target = 7。2+2+2+2 7,所以h为4。 clas 阅读全文
posted @ 2017-06-07 23:14 UniMilky 阅读(117) 评论(0) 推荐(0) 编辑
摘要: 思路: 直接遍历数组。复杂度为$O(n)$。 利用二分查找,注意最后利用mid找插入位置。复杂度为$O(log(n))$。 阅读全文
posted @ 2017-06-07 22:28 UniMilky 阅读(149) 评论(0) 推荐(0) 编辑
摘要: 思路: 二分查找,找到后分别向左向右遍历有多少重复的,然后记录左右端点即可,但是这样在某些情况下复杂度为$O(n)$,比如{8,8,8,8,8,8,8,8,8}。竟然过了。。。 总结: 1. 每次二分要判断是否找到 2. 测试程序要考虑到[],[a]两种特殊情况 阅读全文
posted @ 2017-06-07 15:56 UniMilky 阅读(111) 评论(0) 推荐(0) 编辑
摘要: 思路: 参考 http://blog.csdn.net/abcjennifer/article/details/40152323, 复杂度为 $O(nlog(n))$ class Solution { public: void nextPermutation(vector& nums) { int 阅读全文
posted @ 2017-06-07 09:17 UniMilky 阅读(132) 评论(0) 推荐(0) 编辑
摘要: 思路: 复杂度 $O(n)$。 class Solution { public: int removeElement(vector& nums, int val) { int res,k=0; for(int i = 0; i 阅读全文
posted @ 2017-06-07 08:32 UniMilky 阅读(148) 评论(0) 推荐(0) 编辑
摘要: 思路: 记录每个元素前面有几个重复的数字,但是写的时候感觉容易出错,还是第二种好一点。复杂度$O(n)$。 class Solution { public: int removeDuplicates(vector& nums) { int i = 0,k = 0; if(nums.size() & 阅读全文
posted @ 2017-06-06 22:59 UniMilky 阅读(230) 评论(0) 推荐(0) 编辑
摘要: 思路: 类似3Sum,利用map存储可以省去判断重复数组,复杂度为 $O(n^4)$,因为set判断元素是否重复也需要 $O(n)$. class Solution { public: vector fourSum(vector& nums, int target) { sort(nums.begi 阅读全文
posted @ 2017-06-06 21:20 UniMilky 阅读(146) 评论(0) 推荐(0) 编辑