- 博客(64)
- 收藏
- 关注
原创 manacher算法 (O(n)求最长回文子串)
我们可以用O(n^2)暴力求解最长回文子串。之所以是这个复杂度,是因为我们对每个字符比较其两边元素是否相等时,我们都是从它最旁边的一个开始迭代的。但如果我们能以该字符为中心,其附近的某一段子串已为回文,在此基础上比较更远的元素,那么就有可能降低这个复杂度了。
2014-06-06 00:16:05 1845
原创 LeetCode 49. Pow(x, n)
x^n = x^(a[i]*(2^i) + a[i-1]*(2^i-1) + ... + a[0]*1 ), 其中
2014-06-30 06:42:38 640
原创 LeetCode 46. Jump Game
代码:class Solution {public: bool canJump(int A[], int n) { for (int i=0, bound=0; i <= bound; ++ i) { bound = max(A[i]+i, bound); if (bound >= n-1) { ret
2014-06-29 07:23:20 583
原创 LeetCode 45. Permutations II
当时做LeetCode 44. Permutations考虑的就是找出unique的来,
2014-06-29 04:17:14 580
原创 LeetCode 42. Multiply Strings
模拟乘法竖式计算,有个trick是用一维数组result[ a.size()+b.size() ]记录每个位
2014-06-29 03:50:04 646
原创 LeetCode 38. Combination Sum
candidats排升序去重,递归求解即可。代码:class Solution {public: vector > combinationSum(vector &candidates, int target) { sort(candidates.begin(), candidates.end()); for (size_t i = 0; i
2014-06-26 12:38:59 686
原创 LeetCode 36. Sudoku Solver
数独规则:考察每行、每列、以及九个九宫格中的任意一个(题中已用粗线分割),都不包含重复的数字(即至多只有一个1, 2, 3, ..., 9)这题可以用递归来解。好暴力啊
2014-06-24 12:26:53 795
原创 LeetCode 33. Search for a Range
二分搜索,找到index后,考察index两边的元素是否仍等于target进而确定begin和end即可
2014-06-21 05:09:21 620
原创 LeetCode 29. Substring with Concatenation of All Words
用一张map统计L的单词及频数;li
2014-06-21 01:52:20 672
原创 LeetCode 27. Implement strStr() (KMP算法-模式串匹配)
问needle是否是haystack的子串,若是,返回
2014-06-17 02:35:19 775
原创 LeetCode 26. Remove Duplicates from Sorted Array
和LeetCode 25. Remove Element差不多个意思。数组去重,剩下的元素排在前面,返回数组长度。代码:class Solution {public: int removeDuplicates(int A[], int n) { int ret = 0; for (int i = 0; i < n; ++ i)
2014-06-17 02:23:16 641
原创 LeetCode 24. Reverse Nodes in k-Group
使用常数空间(不可以用递归函数)来实现。一直迭代即可。代码:class Solution {public: ListNode *reverseKGroup(ListNode *head, int k) { ListNode *cur=head, *prev, *next; while (cur != NULL) { int i; for (i=0
2014-06-17 02:09:36 645
原创 LeetCode 21. Generate Parentheses
参考了代码:class Solution {public: vector generateParenthesis(int n) { vector ret; gao(0, 2*n, 0, 0, string(), ret); return ret; }private: void gao(int dep, in
2014-06-16 02:40:09 597
原创 LeetCode 18. Letter Combinations of a Phone Number
依题意做即可,输入为2-9间的数字。代码:class Solution {public: vector letterCombinations(string digits) { vector ret(1, ""); string letters[] = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "
2014-06-14 08:17:43 783
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人