qingcheng奕  
01 2014 档案
  • LeetCode OJ--Gray Code **
    摘要: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奕 阅读(299) 评论(0) 推荐(0)
  • LeetCode OJ--Symmetric Tree **
    摘要: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奕 阅读(203) 评论(0) 推荐(0)
  • LeetCode OJ--Merge Two Sorted Lists
    摘要: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奕 阅读(195) 评论(0) 推荐(0)
  • LeetCode OJ--Rotate List
    摘要: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奕 阅读(163) 评论(0) 推荐(0)
  • LeetCode OJ--Set Matrix Zeroes **
    摘要: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奕 阅读(143) 评论(0) 推荐(0)
  • LeetCode OJ--Longest Consecutive Sequence ***
    摘要: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奕 阅读(132) 评论(0) 推荐(0)
  • LeetCode OJ--Reverse Nodes in k-Group **
    摘要: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奕 阅读(159) 评论(0) 推荐(0)
  • LeetCode OJ--Median of Two Sorted Arrays ***
    摘要: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奕 阅读(156) 评论(0) 推荐(0)
  • LeetCode OJ--Search for a Range
    摘要:http://oj.leetcode.com/problems/search-for-a-range/要求复杂度为O(lgn),用二分查找的思想。#include #include using namespace std;class Solution {public: void fun(int* A,int start,int end,int target,vector &ans) { if(start>end || ( start == end && A[start]!= target )) { ans.push_back(-1)... 阅读全文
    posted @ 2014-01-11 17:49 qingcheng奕 阅读(141) 评论(0) 推荐(0)
  • 构造函数和析构函数的调用
    摘要:#include using namespace std;class Reader{public: Reader() { xCoordinate = NULL; xCoordinate = (int*)malloc(sizeof(int)*100); coutDoHandle(); delete p3Converter; //不调用Reader2的构造和析构 Converter *p3Converter = new Converter(); delete p3Converter; return 0;} 阅读全文
    posted @ 2014-01-11 15:40 qingcheng奕 阅读(345) 评论(0) 推荐(0)
  • VS debug调试方法
    摘要:F5 开始调试,执行到断点Shift + F5 停止调试F9 在光标所在行添加断点Shift + F9 QuickWatchShift Ctrl F9 delete all 断点F10 单步执行F11 进入调用的函数Shift F11 跳出这次调用的函数另外还可以用Disable all breakpoints可以右键点击添加breakpoint condition.比如 int i = 0; 条件可以是 i==5,或者 i has changed.如果 string str,则可以 strcmp(str,"onestring") ==0这样的。对于char *str ;s 阅读全文
    posted @ 2014-01-09 22:41 qingcheng奕 阅读(1756) 评论(0) 推荐(0)
  • LeetCode OJ--Add Two Numbers
    摘要:http://oj.leetcode.com/problems/add-two-numbers/将用链表表示的两个数相加,(2 -> 4 -> 3) + (5 -> 6 -> 4) 就是342 + 465.刚开始把题目给理解错了,做复杂了。主要是对指针的理解。ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reus.. 阅读全文
    posted @ 2014-01-09 20:33 qingcheng奕 阅读(143) 评论(0) 推荐(0)
  • LeetCode OJ--Regular Expression Matching
    摘要:http://oj.leetcode.com/problems/regular-expression-matching/问题给想复杂了,只有p中可以含有. *,s中的字符都是确定的。想了好久,最终还是参考了网上的答案。等我再想想。#include #include #include using namespace std;class Solution {public: bool isMatch(const char *s, const char *p) { if (s == NULL || p == NULL) return false; if ... 阅读全文
    posted @ 2014-01-07 16:20 qingcheng奕 阅读(177) 评论(0) 推荐(0)
  • LeetCode OJ--Roman to Integer
    摘要:http://oj.leetcode.com/problems/roman-to-integer/罗马数字到自然数字的转换,先自己查相关的背景知识,然后分析清楚了。可以简化写,嗯嗯。“简化”,抓到本质。#include #include #include using namespace std;class Solution {public: int change(char ch) { int num = 0; switch(ch) { case 'I': num = 1; break... 阅读全文
    posted @ 2014-01-06 19:04 qingcheng奕 阅读(164) 评论(0) 推荐(0)
  • LeetCode OJ--Largest Rectangle in Histogram
    摘要:最近在学动态规划,初略一看以为动态规划来了,但是一分析发现不具备最优子结构,所以不是动态规划。那么对左边界和长度做个两层循环,遍历所有的可能,复杂度为O(n2),代码如下:class Solution {public: int largestRectangleArea(vector &height) { int smallest; int ans = 0; int size; for(int i = 0;iheight[i+j-1]) { smallest = h... 阅读全文
    posted @ 2014-01-03 19:32 qingcheng奕 阅读(166) 评论(0) 推荐(0)
  • LeetCode OJ--Best Time to Buy and Sell Stock III
    摘要:http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/这三道题,很好的进阶。1题简单处理,2题使用贪心,3题使用动态规划。话说这叫一维动态规划,嗯。又复习了《算法导论》中和贪心以及动态规划有关的知识,记录如下:动态规划的标志:最优子结构、子问题重叠。 1.找最优子结构 2.定义递归公示(列一个式子出来,并定义好这个式子到底是什么意思)。 3.写自底向上或递归备忘录法。比如本问题:f(i,j) = max{f(i,k)+f(k,j)} 其中:f(i,j)表示从i到j的所有数,进行一次交易能获得... 阅读全文
    posted @ 2014-01-02 19:00 qingcheng奕 阅读(181) 评论(0) 推荐(0)
  • LeetCode OJ--Best Time to Buy and Sell Stock II
    摘要:http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/第二问,是说可以进行无数次买卖。贪心法#include #include using namespace std;class Solution {public: int maxProfit(vector &prices) { if(prices.empty()) return NULL; if(prices.size()==1) return 0; int ans = 0... 阅读全文
    posted @ 2014-01-02 15:51 qingcheng奕 阅读(203) 评论(0) 推荐(0)
  • LeetCode OJ--Best Time to Buy and Sell Stock
    摘要:找一个数后面最大的数,求出一列数中这样的最大差值。首先想到了两层循环O(n*n)复杂度。然后超时,代码如下:class Solution {public: int maxProfit(vector &prices) { if(prices.empty()) return NULL; if(prices.size()==1) return 0; int ans = 0; for(int i = 0;imax) max = prices[j... 阅读全文
    posted @ 2014-01-01 16:44 qingcheng奕 阅读(142) 评论(0) 推荐(0)
  • LeetCode OJ——Unique Binary Search Trees
    摘要:class Solution {public: int numTrees(int n) { if(!n||n==1)return 1; vector numVector; numVector.assign(n+1,0); numVector[0]=1; numVector[1]=1; for(int i=2;i<=n;++i) { for(int j=0;j<=i-1;++j) { numVector[i]+=numV... 阅读全文
    posted @ 2014-01-01 14:53 qingcheng奕 阅读(119) 评论(0) 推荐(0)