qingcheng奕  
上一页 1 ··· 11 12 13 14 15 16 17 18 19 ··· 23 下一页

2014年2月12日

摘要: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/要求可以用重复元素,但是最大重复次数为两次。#include using namespace std;class Solution {public: int removeDuplicates(int A[], int n) { if(n == 0) return 0; if(n == 1) return 1; int times = 1; int p... 阅读全文
posted @ 2014-02-12 15:01 qingcheng奕 阅读(122) 评论(0) 推荐(0) 编辑
 
摘要: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/删除数组中的重复元素,要求为原地算法。进行一遍遍历,记录下一个可用位置,也就是用来存储不重复元素的位置。class Solution {public: int removeDuplicates(int A[], int n) { if(n == 0) return 0; if(n == 1) return 1; int preOne = A[0]; int... 阅读全文
posted @ 2014-02-12 14:41 qingcheng奕 阅读(121) 评论(0) 推荐(0) 编辑

2014年1月21日

摘要: http://oj.leetcode.com/problems/gray-code/求格雷码的表示,主要应用递归。递归生成码表这种方法基于格雷码是反射码的事实,利用递归的如下规则来构造:1位格雷码有两个码字(n+1)位格雷码中的前2n个码字等于n位格雷码的码字,按顺序书写,加前缀0(n+1)位格雷码中的后2n个码字等于n位格雷码的码字,按逆序书写,加前缀1#include #include #include using namespace std;class Solution {public: vector > ans; vector generateGrayCode(int i,... 阅读全文
posted @ 2014-01-21 12:33 qingcheng奕 阅读(291) 评论(0) 推荐(0) 编辑

2014年1月19日

摘要: http://oj.leetcode.com/problems/symmetric-tree/判断树是否对称class Solution {public:bool compare(TreeNode *left, TreeNode *right){ if(left == NULL && right != NULL) return false; if(left != NULL && right == NULL) return false; if(left == NULL && right == NULL) return true; ... 阅读全文
posted @ 2014-01-19 17:23 qingcheng奕 阅读(197) 评论(0) 推荐(0) 编辑

2014年1月18日

摘要: http://oj.leetcode.com/problems/merge-two-sorted-lists/有序链表的归并排序#include using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { // default as asend sort... 阅读全文
posted @ 2014-01-18 10:26 qingcheng奕 阅读(188) 评论(0) 推荐(0) 编辑

2014年1月17日

摘要: http://oj.leetcode.com/problems/rotate-list/取得后面k个节点,然后截断插到前面。如果k比list长,则按照求余算。去后面的k个节点:使用两个指针,第一个指针比第二个指针先走k步,然后两个一起往后走,等到第一个到达最后一个节点,第二个就是倒数第k个节点。#include using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution {public: ... 阅读全文
posted @ 2014-01-17 22:07 qingcheng奕 阅读(154) 评论(0) 推荐(0) 编辑

2014年1月16日

摘要: http://oj.leetcode.com/problems/set-matrix-zeroes/因为空间要求原地,所以一些信息就得原地存储。使用第一行第一列来存本行本列中是否有0.另外对于第一个元素[0][0],需要额外处理。#include #include using namespace std;class Solution {public: void setZeroes(vector > &matrix) { int flag = 0; int flag2 = 0; for(int i = 0;i > matrix; vector... 阅读全文
posted @ 2014-01-16 17:02 qingcheng奕 阅读(135) 评论(0) 推荐(0) 编辑

2014年1月15日

摘要: http://oj.leetcode.com/problems/longest-consecutive-sequence/起初想的是排序,查了下O(n)的排序算法有计数排序、基数排序、桶排序。后来考虑到数据的范围不知道,并且还有可能是负数,这几种方法都不太适用。之后想到了容器,Map、Set的查找是哈希查找,复杂度为O(1).#include #include #include using namespace std;class Solution {public: unordered_set dict; int findLongestConsective(int num) {... 阅读全文
posted @ 2014-01-15 16:41 qingcheng奕 阅读(128) 评论(0) 推荐(0) 编辑

2014年1月14日

摘要: http://oj.leetcode.com/problems/reverse-nodes-in-k-group/ 链表 指针 对链表翻转的变形#include using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; ListNode *reverse(ListNode * l2) { ListNode *n1,*n2,*before; n1 = l2; n2 = n1->next; ... 阅读全文
posted @ 2014-01-14 19:11 qingcheng奕 阅读(153) 评论(0) 推荐(0) 编辑

2014年1月12日

摘要: http://oj.leetcode.com/problems/median-of-two-sorted-arrays/找两个有序数组的中位数,因为有序数组,并且复杂度要求O(lg(m+n))所以想到了使用二分,但具体怎么做,没分析清楚。参考了网上http://blog.csdn.net/zxzxy1988/article/details/8587244,其他的类似找两个有序数组中第 k 大的也可以这样处理。#include using namespace std;double findKth(int a[],int m,int b[],int n,int k){ if(m>n) //总 阅读全文
posted @ 2014-01-12 21:14 qingcheng奕 阅读(147) 评论(0) 推荐(0) 编辑
上一页 1 ··· 11 12 13 14 15 16 17 18 19 ··· 23 下一页