- 博客(49)
- 收藏
- 关注
原创 LeetCode 94. Binary Tree Inorder Traversal
树的中序遍历,递归。代码:class Solution {public: vector inorderTraversal(TreeNode *root) { vector ret; gao(root, ret); return ret; }private: void gao(TreeNode* node, vector
2014-07-27 07:16:39 738
原创 LeetCode 93. Restore IP Addresses
递归+剪枝。代码:class Solution {public: vector restoreIpAddresses(string s) { vector now; gao(now, 0, s, 0); return ret; }private: void gao(vector& now, int depth,
2014-07-27 07:06:32 914
原创 LeetCode 92. Reverse Linked List II
代码:class Solution {public: ListNode *reverseBetween(ListNode *head, int m, int n) { if (head==NULL || m==n) { return head; } ListNode *cut=head, *cur=head;
2014-07-27 06:55:49 683
原创 LeetCode 91. Subsets
排序S, 用vector num记录不同的数字,用vector cnt为这些数字计数。(怎么gan代码:
2014-07-26 05:14:16 752
原创 LeetCode 88. Merge Sorted Array
O(n+m)空间 O(n+m)时间实现代码:class Solution {public: void merge(int A[], int m, int B[], int n) { int i=0, j=0, index=m; vector vt(m+n, 0); while ( i<m && j<n ) {
2014-07-26 04:59:40 696
原创 LeetCode 83. Remove Duplicates from Sorted List II
代码:class Solution {public: ListNode *deleteDuplicates(ListNode *head) { auto cur = head; ListNode* pre_node = NULL; while (cur != NULL) { bool need = true;
2014-07-22 06:49:44 727
原创 LeetCode 81. Search in Rotated Sorted Array II
和LeetCode 32. Search in Rotated Sorted Array类似,二分递归求解。数组任意的一次向右滚动(一个单位),考虑最后两个元素A[n-1], A[n-2]. 若A[n-1]事实上为最初的A[0], 那么这次滚动后,数组将回到最初的状态,A[0'] , 成为升序数组,二分搜索即可;若A[n-1]并非最初的A[0], 那么必有A[n-2] A[0'] >=
2014-07-22 05:50:03 747
原创 LeetCode 82. Remove Duplicates from Sorted List
代码:class Solution {public: ListNode *deleteDuplicates(ListNode *head) { auto cur = head; while (cur != NULL) { auto next = cur->next; while (next!=NULL &&
2014-07-22 05:49:36 730
原创 LeetCode 80. Remove Duplicates from Sorted Array II
在升序数组中,每个数字最多只能出现2次。代码:class Solution {public: int removeDuplicates(int A[], int n) { int length = 0; for (int i=0; i < n; ) { A[length ++] = A[i ++]; if (i<n &&
2014-07-22 05:03:37 787
原创 LeetCode 79. Word Search
递归搜索即可,注意标记已访问过的点。代码:class Solution {public: bool exist(vector > &board, string word) { if (board.empty()==true || word.empty()==true) { return false; } size_t n=board.size(),
2014-07-17 09:53:54 830
原创 LeetCode 76. Minimum Window Substring
用头尾指针记录S的子串,当子串包含T中的所有字符时,尝试收缩头指针begin,
2014-07-17 01:15:00 703
原创 LeetCode 75. Sort Colors (O(n) one pass实现)
用两个变量red, white记录当前最后一个红色、最后一个白色的坐标
2014-07-15 05:11:24 750
原创 LeetCode 73. Set Matrix Zeroes (O(nm)时间 O(1)空间实现)
要求用O(1)空间实现,O(nm)时间则是遍历矩阵每个元素必要的。
2014-07-15 03:11:43 902
原创 LeetCode 72. Edit Distance
自然语言处理(NPL)里的一个问题,用动态规划解。dp[i][j]表示word1前i个字符和word2前j个z
2014-07-14 10:34:35 872
原创 LeetCode 70. Climbing Stairs (O(n)时间, O(1)空间实现)
爬楼梯,一次爬1步或2步,求欲到达第n步,有几种爬法。
2014-07-12 07:23:50 890
原创 LeetCode 67. Plus One
代码:class Solution {public: vector plusOne(vector &digits) { int carry = 1; vector ret; for (int i = digits.size()-1; i >= 0; -- i) { ret.insert(ret.begin(), (di
2014-07-11 11:24:24 649
原创 LeetCode 60. Rotate List
代码:class Solution {public: ListNode *rotateRight(ListNode *head, int k) { if (head == NULL) { return NULL; } int size = 0; auto fast = head, slow = head;
2014-07-04 06:09:18 522
原创 LeetCode 59. Permutation Sequence
代码:class Solution {public: string getPermutation(int n, int k) { int num[] = {0, 1, 2, 6, 24, 120, 720, 5040, 40320}; bool vi[10] = {false, false}; string ret = string(n, 0
2014-07-04 05:52:41 618
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人