随笔分类 -  Leetcode

[leetcode] ZigZag Conversion *
摘要:1 /* 2 * Just find the rule; Do not forget to check 3 * special cases (e.g. nRows = 1 ...) 4 * 5 * @author: HZT 6 * @date: 2013-3-9 7 */ 8 9 #include <iostream>10 #include <string>11 using namespace std;12 13 class Solution {14 public:15 string convert(string s, int nRows) {16 //... 阅读全文

posted @ 2013-03-09 00:21 龙豆 阅读(364) 评论(0) 推荐(0) 编辑

[leetcode] Longest Palindromic Substring *
摘要:1 /* 2 * DP 3 * 4 * d[i][j] represents the length of longest Palindromic 5 * substring of s[i...j]. 6 * 7 * |- j-i+1 (s[i] == s[j] && s[i+1...j-1] is Palindrome) 8 * d[i][j] = | 9 * |- max{ d[i+1][j], d[i][j-1] }10 *11 * Note: use circular array.12 */13 14 #incl... 阅读全文

posted @ 2013-03-08 23:31 龙豆 阅读(276) 评论(0) 推荐(0) 编辑

[leetcode] Add Two Numbers *
摘要:1 /* 2 * Easy 3 * 4 * @author: HZT 5 * @date: 2013-3-7 6 */ 7 8 #include <iostream> 9 using namespace std;10 11 struct ListNode{12 int val;13 ListNode *next;14 ListNode(int x) : val(x), next(NULL) {}15 };16 17 class Solution {18 public:19 ListNode *addTwoNumbers(ListNode *l1, ... 阅读全文

posted @ 2013-03-07 22:06 龙豆 阅读(347) 评论(0) 推荐(0) 编辑

[leetcode] Longest Substring Without Repeating Characters
摘要:1 /* 2 * Use two points, pBeg and pEnd, pointing to the begin and end 3 * of current substring without repeating respectively. Integer 4 * Array pos[256] records the last occurrence position of each 5 * ASCII character. 6 * 7 * For each loop, pEnd moved forward one character. If the new... 阅读全文

posted @ 2013-03-07 21:22 龙豆 阅读(271) 评论(0) 推荐(0) 编辑

[Leetcode] Median of Two Sorted Arrays
摘要:大致思路:先寻找每个数组的中位数,因为是排好顺序的数组,因此,可以在O(1)时间内找到。然后,比较这两个数字的大小。如果A的中位数大于B的中位数,则在A的前半个数组和B的后半个数组中寻找; 反之,在B的前半个数组和A的后半个数组寻找。根据递归方程,解得时间复杂度是O(lg(n)).A general version:Find the Kth element of two sorted arrays A[] and B[]. 1 #include <cstdio> 2 #include <iostream> 3 #include <cstdlib> 4 usi 阅读全文

posted @ 2013-03-04 23:06 龙豆 阅读(1188) 评论(0) 推荐(0) 编辑

[Leetcode] Two Sum
摘要:1. Sort :O(n*lgn)2. two points search from both ends to middle :O(n) 1 struct Node 2 { 3 int val; 4 int index; 5 Node(){} 6 Node(int v, int idx):val(v), index(idx){} 7 }; 8 9 bool compare(const Node &lhs, const Node &rhs)10 {11 return lhs.val < rhs.val;12 }13 14 cla... 阅读全文

posted @ 2013-03-02 16:49 龙豆 阅读(576) 评论(0) 推荐(0) 编辑

导航

点击右上角即可分享
微信分享提示