上一页 1 2 3 4 5 6 ··· 11 下一页
摘要: https://oj.leetcode.com/problems/rotate-image/90度顺时针旋转一个矩阵。很容易联想到矩阵转置。但是矩阵转置是将第i行变成了第i列。而这个旋转的结果是要使第i行变成第n-i列。所以在转置之后再将每一列调整到其应该在的地方即可。还有一种方法是注意到每个元素的... 阅读全文
posted @ 2014-10-20 20:24 zombies 阅读(205) 评论(0) 推荐(0) 编辑
摘要: https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/当允许相等时,就要考虑各种边界条件。我的策略是把每个>,l){ int mid=(l+r)/2; if (x==A[mid... 阅读全文
posted @ 2014-10-20 18:52 zombies 阅读(162) 评论(0) 推荐(0) 编辑
摘要: https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/用一个cnt记录不重复的部分,后面每遇到不重复的cnt++即可。class Solution {public: int removeDuplicates(i... 阅读全文
posted @ 2014-10-20 03:03 zombies 阅读(101) 评论(0) 推荐(0) 编辑
摘要: https://oj.leetcode.com/problems/linked-list-cycle-ii/在判断成环问题的基础上需要做一些推理才能得到简洁的解法。首先,假设环的起点距离head有m步。则当p到达起点时,q的位置在环上的m%n处。那么p,q相遇的位置相对于起点就是(n-m%n),在这... 阅读全文
posted @ 2014-10-20 02:55 zombies 阅读(121) 评论(0) 推荐(0) 编辑
摘要: https://oj.leetcode.com/problems/unique-binary-search-trees-ii/跟二叉树个数计数的那道题类似,不同的是需要把所有的可能都返回,并整合成结果。/** * Definition for binary tree * struct TreeNod... 阅读全文
posted @ 2014-10-19 16:47 zombies 阅读(141) 评论(0) 推荐(0) 编辑
摘要: https://oj.leetcode.com/problems/3sum-closest/和3sum类似。不同的是这次需要逼近一个值,实际上跟相等类似,用l和r指针不断移动,然后反复取最小即可。class Solution {public: int n,m; int threeSumC... 阅读全文
posted @ 2014-10-19 10:59 zombies 阅读(129) 评论(0) 推荐(0) 编辑
摘要: https://oj.leetcode.com/problems/powx-n/其实是很简单的递归就能解。每次将其降低到n/2即可。需要注意各种边界条件,包括n为最小的负数的情况。class Solution {public: double pow(double x, int n) { ... 阅读全文
posted @ 2014-10-19 02:19 zombies 阅读(142) 评论(0) 推荐(0) 编辑
摘要: https://oj.leetcode.com/problems/gas-station/计算每个加油站的加油差diff[]。得到一个数组。从贪心的角度来说,如果我们找到一个最大子串,那么从他的起点l开始走,能够连续一直走并且累积最大量的汽油。一个猜想是:如果这些汽油不足以走完全程,那么无论从哪里都... 阅读全文
posted @ 2014-10-19 02:05 zombies 阅读(246) 评论(0) 推荐(0) 编辑
摘要: https://oj.leetcode.com/problems/maximum-subarray/一直累加子串并求最大。当子串小于0时就抛弃掉,这样总能找到最大的那个子串和。class Solution {public: int maxSubArray(int A[], int n) { ... 阅读全文
posted @ 2014-10-19 01:34 zombies 阅读(154) 评论(0) 推荐(0) 编辑
摘要: https://oj.leetcode.com/problems/distinct-subsequences/首先要理解题目:count the number of distinct subsequences ofTinS.意思是,数出T在S的唯一子序列的个数。也就是有多少个S的子序列是T。子序列可... 阅读全文
posted @ 2014-10-19 00:41 zombies 阅读(236) 评论(0) 推荐(0) 编辑
上一页 1 2 3 4 5 6 ··· 11 下一页