楼主: zhuli19901106
跳转到指定楼层
上一主题 下一主题
收起左侧

分享我的Lintcode题解,目前进度244/248

 
🔗
水逼一枚 2015-7-26 09:37:51 | 只看该作者
全局:
zhuli19901106 发表于 2015-7-22 00:57
Jump Game II
题意:跟Jump Game一样,不过这题求的是跳到终点的最少步数。
解法:只要跳到终点,就马上 ...

#135楼 Jump Game II似乎有点儿问题?lc上没法AC。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-26 15:01:00 | 只看该作者
全局:
水逼一枚 发表于 2015-7-26 09:37
#135楼 Jump Game II似乎有点儿问题?lc上没法AC。

确实,待我改改。这个错代码居然在lintcode上AC了,看来lintcode的数据很水。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-26 15:26:10 | 只看该作者
全局:
Jump Game II
题意:跟Jump Game一样,不过这题求的是跳到终点的最少步数。
解法:只要跳到终点,就马上退出循环即可。感谢水逼一枚指出代码中的错误,lintcode的数据真水,错代码都给AC了。
代码:
  1. class Solution {
  2. public:
  3.     int jump(vector<int> A) {
  4.         int n = A.size();
  5.         if (n < 2) {
  6.             return 0;
  7.         }
  8.         int i;
  9.         int oldr, r;
  10.         int ans = 0;
  11.         
  12.         oldr = r = 0;
  13.         i = 0;
  14.         while (i < n) {
  15.             if (r < i) {
  16.                 // Unreachable
  17.                 return -1;
  18.             }
  19.             
  20.             if (r >= n - 1) {
  21.                 break;
  22.             }
  23.             while (i <= oldr) {
  24.                 r = max(r, i + A[i]);
  25.                 ++i;
  26.             }
  27.             oldr = r;
  28.             ++ans;
  29.         }
  30.         return ans;
  31.     }
  32. };
复制代码
复杂度:时间O(N),空间O(1)。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-26 16:25:21 | 只看该作者
全局:
Maximal Square
题意:给定一个只含有01矩阵,求其中全部为1的正方形的最大面积。
解法:考虑这题和Largest Rectangle In Histogram的关联性。
代码:
  1. #include <algorithm>
  2. using namespace std;

  3. class Solution {
  4. public:
  5.     /**
  6.      * @param matrix: a matrix of 0 and 1
  7.      * @return: an integer
  8.      */
  9.     int maxSquare(vector<vector<int> > &matrix) {
  10.         v.clear();
  11.         dl.clear();
  12.         dr.clear();
  13.         
  14.         n = matrix.size();
  15.         if (n == 0) {
  16.             return 0;
  17.         }
  18.         m = matrix[0].size();
  19.         if (m == 0) {
  20.             return 0;
  21.         }
  22.         v.resize(m, 0);
  23.         dl.resize(m);
  24.         dr.resize(m);
  25.         int i, j;
  26.         int ans = 0;
  27.         for (i = 0; i < n; ++i) {
  28.             for (j = 0; j < m; ++j) {
  29.                 v[j] = matrix[i][j] ? v[j] + matrix[i][j] : 0;
  30.             }
  31.             ans = max(ans, largestHistogram());
  32.         }
  33.         return ans;
  34.     }
  35. private:
  36.     vector<int> v;
  37.     vector<int> dl, dr;
  38.     int n, m;
  39.    
  40.     int largestHistogram() {
  41.         int i;
  42.         for (i = 0; i <= m - 1; ++i) {
  43.             dl[i] = dr[i] = i;
  44.         }
  45.         for (i = 1; i <= m - 1; ++i) {
  46.             while (dl[i] - 1 >= 0 && v[dl[i] - 1] >= v[i]) {
  47.                 dl[i] = dl[dl[i] - 1];
  48.             }
  49.         }
  50.         for (i = m - 2; i >= 0; --i) {
  51.             while (dr[i] + 1 <= m - 1 && v[dr[i] + 1] >= v[i]) {
  52.                 dr[i] = dr[dr[i] + 1];
  53.             }
  54.         }
  55.         int ans = 0;
  56.         int len;
  57.         for (i = 0; i <= m - 1; ++i) {
  58.             len = min(v[i], dr[i] - dl[i] + 1);
  59.             ans = max(ans, len * len);
  60.         }
  61.         return ans;
  62.     }
  63. };
复制代码
复杂度:时间O(N ^ 2),空间O(N)。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-26 16:27:21 | 只看该作者
全局:
Longest Words
题意:很无聊的一题。
解法:略。
代码:
  1. #include <algorithm>
  2. using namespace std;

  3. class Solution {
  4. public:
  5.     /**
  6.      * @param dictionary: a vector of strings
  7.      * @return: a vector of strings
  8.      */
  9.     vector<string> longestWords(vector<string> &dictionary) {
  10.         int i;
  11.         int len = 0;
  12.         vector<string> ans;
  13.         
  14.         for (i = 0; i < dictionary.size(); ++i) {
  15.             len = max(len, (int)dictionary[i].length());
  16.         }
  17.         for (i = 0; i < dictionary.size(); ++i) {
  18.             if (len == dictionary[i].length()) {
  19.                 ans.push_back(dictionary[i]);
  20.             }
  21.         }
  22.         return ans;
  23.     }
  24. };
复制代码
复杂度:不怎么复杂。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-26 16:30:48 | 只看该作者
全局:
Space Replacement
题意:给定一个字符串,把其中的‘ ’替换成‘%20’,要求就地完成。
解法:首先顺着扫一次,确定有多少个空格。然后从尾到头扫一遍,进行替换。为什么从尾到头呢?因为这样避免了移动大量的元素。
代码:
  1. #include <cstring>
  2. using namespace std;

  3. class Solution {
  4. public:
  5.     /**
  6.      * @param string: An array of Char
  7.      * @param length: The true length of the string
  8.      * @return: The true length of new string
  9.      */
  10.     int replaceBlank(char string[], int length) {
  11.         if (string == NULL) {
  12.             return 0;
  13.         }
  14.         
  15.         int i, j;
  16.         char *s = string;
  17.         int n = length;
  18.         int m = n;
  19.         int ans;
  20.         for (i = 0; i < n; ++i) {
  21.             if (s[i] == ' ') {
  22.                 m += 2;
  23.             }
  24.         }
  25.         ans = m;
  26.         s[m--] = 0;
  27.         for (i = n - 1; i >= 0; --i) {
  28.             if (s[i] == ' ') {
  29.                 s[m--] = '0';
  30.                 s[m--] = '2';
  31.                 s[m--] = '%';
  32.             } else {
  33.                 s[m--] = s[i];
  34.             }
  35.         }
  36.         return ans;
  37.     }
  38. };
复制代码
复杂度:时间O(N),空间O(1)。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-26 16:38:54 | 只看该作者
全局:
Max Points on a Line
题意:给定N个整点,求出一条直线可以通过的最多点数。
解法:这题用O(N ^ 3)的时间可以暴力解决,就不提了。来想想O(N ^ 2)的解法。我们以每个点为中心,考虑其他所有点与这点构成的直线的斜率,而且为了避免精度问题,不用浮点数,而是用一个数对来表示。斜率相同,自然会落在一条线上。此处还要考虑到重合的点,这些点都要算进去。利用哈希表来保存斜率,可以做到平方复杂度。
代码:
  1. #include <algorithm>
  2. #include <map>
  3. using namespace std;

  4. /**
  5. * Definition for a point.
  6. * struct Point {
  7. *     int x;
  8. *     int y;
  9. *     Point() : x(0), y(0) {}
  10. *     Point(int a, int b) : x(a), y(b) {}
  11. * };
  12. */

  13. typedef struct Term {
  14.     Term(int x = 0, int y = 0) {
  15.         this->x = x;
  16.         this->y = y;
  17.     }
  18.    
  19.     int x, y;
  20.     bool operator < (const Term &other) const
  21.     {
  22.         if (x != other.x) {
  23.             return x < other.x;
  24.         } else {
  25.             return y < other.y;
  26.         }
  27.     }
  28.    
  29.     bool operator == (const Term &other) const
  30.     {
  31.         return x == other.x && y == other.y;
  32.     }
  33. } Term;

  34. int gcd(int x, int y)
  35. {
  36.     return x ? gcd(y % x, x) : y;
  37. }

  38. int abs(int x)
  39. {
  40.     return x >= 0 ? x : -x;
  41. }

  42. void normalize(Term &t)
  43. {
  44.     if (t.x == 0) {
  45.         t.y = t.y ? 1 : 0;
  46.         return;
  47.     }
  48.     if (t.y == 0) {
  49.         t.x = t.x ? 1 : 0;
  50.         return;
  51.     }
  52.    
  53.     if (t.x < 0) {
  54.         t.x = -t.x;
  55.         t.y = -t.y;
  56.     }
  57.     int g = gcd(t.x, abs(t.y));
  58.     t.x /= g;
  59.     t.y /= g;
  60. }

  61. class Solution {
  62. public:
  63.     /**
  64.      * @param points an array of point
  65.      * @return an integer
  66.      */
  67.     int maxPoints(vector<Point>& points) {
  68.         int n = points.size();
  69.         if (n < 3) {
  70.             return n;
  71.         }
  72.         
  73.         map<Term, int> mm;
  74.         map<Term, int>::iterator it;
  75.         Term t;
  76.         int i, j;
  77.         int zc;
  78.         int msum = 2;
  79.         int sum;
  80.         
  81.         for (i = 0; i < n; ++i) {
  82.             for (j = i + 1; j < n; ++j) {
  83.                 t.x = points[j].x - points[i].x;
  84.                 t.y = points[j].y - points[i].y;
  85.                 normalize(t);
  86.                 ++mm[t];
  87.             }
  88.             zc = mm[Term(0, 0)];
  89.             sum = 0;
  90.             for (it = mm.begin(); it != mm.end(); ++it) {
  91.                 if (it->first == Term(0, 0)) {
  92.                     continue;
  93.                 }
  94.                 sum = max(sum, it->second);
  95.             }
  96.             msum = max(msum, sum + zc + 1);
  97.             mm.clear();
  98.         }
  99.         return msum;
  100.     }
  101. };
复制代码
复杂度:时间O(N ^ 2),空间O(N)。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-26 16:43:10 | 只看该作者
全局:
Invert Binary Tree
题意1:问问Max Howell。
解法1:不会。
代码1:
  1. /**
  2. * Definition of TreeNode:
  3. * class TreeNode {
  4. * public:
  5. *     int val;
  6. *     TreeNode *left, *right;
  7. *     TreeNode(int val) {
  8. *         this->val = val;
  9. *         this->left = this->right = NULL;
  10. *     }
  11. * }
  12. */
  13. class Solution {
  14. public:
  15.     /**
  16.      * @param root: a TreeNode, the root of the binary tree
  17.      * @return: nothing
  18.      */
  19.     void invertBinaryTree(TreeNode *root) {
  20.         if (root == NULL) {
  21.             return;
  22.         }
  23.         TreeNode *p = root->left;
  24.         root->left = root->right;
  25.         root->right = p;
  26.         invertBinaryTree(root->left);
  27.         invertBinaryTree(root->right);
  28.     }
  29. };
复制代码
复杂度1:太复杂。

解法2:可以会。
代码2:
  1. #include <algorithm>
  2. #include <queue>
  3. using namespace std;
  4. /**
  5. * Definition of TreeNode:
  6. * class TreeNode {
  7. * public:
  8. *     int val;
  9. *     TreeNode *left, *right;
  10. *     TreeNode(int val) {
  11. *         this->val = val;
  12. *         this->left = this->right = NULL;
  13. *     }
  14. * }
  15. */
  16. class Solution {
  17. public:
  18.     /**
  19.      * @param root: a TreeNode, the root of the binary tree
  20.      * @return: nothing
  21.      */
  22.     void invertBinaryTree(TreeNode *root) {
  23.         // The iterative version.
  24.         if (root == NULL) {
  25.             return;
  26.         }
  27.         TreeNode *p;
  28.         queue<TreeNode *> q;
  29.         
  30.         q.push(root);
  31.         while (!q.empty()) {
  32.             p = q.front();
  33.             q.pop();
  34.             swap(p->left, p->right);
  35.             if (p->left != NULL) {
  36.                 q.push(p->left);
  37.             }
  38.             if (p->right != NULL) {
  39.                 q.push(p->right);
  40.             }
  41.         }
  42.     }
  43. };
复制代码
复杂度2:还行。
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-26 16:43:31 | 只看该作者
全局:
又被吞掉一题:Invert Binary Tree
回复

使用道具 举报

🔗
 楼主| zhuli19901106 2015-7-26 17:28:45 | 只看该作者
全局:
Find the Missing Number
题意:给定一个数组,其中0-N中除了一个数之外,其他的都出现一次。找出那个没出现的。
解法1:一个数组当两个用。
代码1:
  1. class Solution {
  2. public:
  3.     /**   
  4.      * @param nums: a vector of integers
  5.      * @return: an integer
  6.      */
  7.     int findMissing(vector<int> &nums) {
  8.         int n = nums.size();
  9.         
  10.         if (n == 0) {
  11.             return 0;
  12.         }
  13.         
  14.         int i;
  15.         nums.push_back(0);
  16.         for (i = 0; i < n; ++i) {
  17.             nums[nums[i] % (n + 1)] += n + 1;
  18.         }
  19.         for (i = 0; i <= n; ++i) {
  20.             if (nums[i] < n + 1) {
  21.                 break;
  22.             }
  23.         }
  24.         return i;
  25.     }
  26. };
复制代码
复杂度1:时间O(N),空间O(1)。

解法2:求和即可。
代码2:
  1. typedef long long int LL;
  2. class Solution {
  3. public:
  4.     /**   
  5.      * @param nums: a vector of integers
  6.      * @return: an integer
  7.      */
  8.     int findMissing(vector<int> &nums) {
  9.         int n = nums.size();
  10.         int i;
  11.         LL sum = 0;
  12.         for (i = 0; i < n; ++i) {
  13.             sum += nums[i];
  14.         }
  15.         return 1LL * n * (n + 1) / 2 - sum;
  16.     }
  17. };
复制代码
复杂度2:时间O(N),空间O(1)。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表