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

Leetcode 刷题打卡记录 包括Solution

🔗
 楼主| wglacier 2019-1-31 19:08:31 | 只看该作者
全局:
0031. Next Permutation
  1. class Solution {
  2. public:
  3.     void nextPermutation(vector<int>& nums) {
  4.         if (nums.empty()) return;
  5.         // look for the first desc from back
  6.         int i = nums.size() - 1;
  7.         while (i > 0) {
  8.             if (nums[i] > nums[i-1])
  9.                 break;
  10.             i--;
  11.         }
  12.         // the last perm, go back to the original state
  13.         if (i == 0) {
  14.             sort(nums.begin(), nums.end());
  15.             return;
  16.         }
  17.         // find the next large number for nums[i-1] behind it
  18.         sort(nums.begin()+i, nums.end());
  19.         auto it = upper_bound(nums.begin()+i, nums.end(), nums[i-1]);
  20.         swap(*(nums.begin()+i-1), *it);
  21.         sort(nums.begin()+i, nums.end());
  22.         
  23.     }
  24. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 19:09:12 | 只看该作者
全局:
0036. Valid Sudoku
  1. class Solution {
  2. public:
  3.     bool isValidSudoku(vector<vector<char>>& board) {

  4.         for (int i = 0; i < board.size(); i++) {
  5.             int buf_r[256] = {0};
  6.             int buf_c[256] = {0};
  7.             for (int j = 0; j < board[i].size(); j++) {
  8.                 // rows
  9.                 char c = board[i][j];
  10.                 if (c != '.') {
  11.                     if (buf_r[c] != 0)
  12.                         return false;
  13.                     buf_r[c] = 1;
  14.                 }
  15.                 // cols
  16.                 c = board[j][i];
  17.                 if (c != '.') {
  18.                     if (buf_c[c] != 0)
  19.                         return false;
  20.                     buf_c[c] = 1;
  21.                 }
  22.             }
  23.         }
  24.         // diags
  25.         for (int i = 0; i < board.size(); i+=3) {
  26.             for (int j = 0; j < board[i].size(); j+=3) {
  27.                 int buf_x[256] = {0};
  28.                 for (int k = 0; k < 9; k++) {
  29.                     char c = board[i + k/3][j + k%3];
  30.                     if (c != '.') {
  31.                         if (buf_x[c] != 0)
  32.                             return false;
  33.                         buf_x[c] = 1;
  34.                     }
  35.                 }
  36.             }
  37.         }
  38.         return true;
  39.     }
  40. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 19:09:57 | 只看该作者
全局:
0037. Sudoku Solver
  1. class Solution {
  2. private:
  3.     vector<vector<bool>> bufRows = vector<vector<bool>>(9, vector<bool>(256, false));
  4.     vector<vector<bool>> bufCols = vector<vector<bool>>(9, vector<bool>(256, false));
  5.     vector<vector<bool>> bufDias = vector<vector<bool>>(9, vector<bool>(256, false));
  6. public:
  7.     bool isValid(vector<vector<char>>& board, int i, int j, char c) {
  8.         return !bufRows[i][c]
  9.             && !bufCols[j][c]
  10.             && !bufDias[i/3*3 + j/3][c];
  11.     }
  12.     void setFlag(vector<vector<char>>& board, int i, int j, char c, bool value) {
  13.         bufRows[i][c] = value;
  14.         bufCols[j][c] = value;
  15.         bufDias[i/3*3 + j/3][c] = value;
  16.     }
  17.     bool solve(vector<vector<char>>& board, int i, int j) {
  18.         if (i == 9) return true;
  19.         if (j == 9) return solve(board, i+1, 0);
  20.         
  21.         char c = board[i][j];
  22.         if (c != '.') return solve(board, i, j+1);
  23.         
  24.         for (char d = '1'; d <= '9'; d++) {
  25.             if (!isValid(board,i, j, d))
  26.                 continue;
  27.             setFlag(board, i, j, d, true);
  28.             board[i][j] = d;
  29.             if (solve(board, i, j+1))
  30.                 return true;
  31.             board[i][j] = '.';
  32.             setFlag(board, i, j, d, false);
  33.         }
  34.         return false;
  35.     }
  36.     void solveSudoku(vector<vector<char>>& board) {
  37.        for (int i = 0; i < 9; i++) {
  38.            for (int j = 0; j < 9; j++) {
  39.                char c = board[i][j];
  40.                if (c == '.')
  41.                    continue;
  42.                bufRows[i][c] = true;
  43.                bufCols[j][c] = true;
  44.                bufDias[i/3*3 + j/3][c] = true;
  45.            }
  46.        }
  47.         solve(board, 0, 0);
  48.     }
  49. };
  50. ```
  51. ```c++
  52. class Solution {
  53. public:
  54.     bool validatePos(const vector<vector<char>>& board, int i, int j) {
  55.         // vertical
  56.         vector<bool> vcols(9, false);
  57.         for (int m = 0; m < 9; m++) {
  58.             // cols
  59.             if (board[m][j] == '.') continue;
  60.             
  61.             int num = board[m][j] - '1';
  62.             if (vcols[num]) {
  63.                 return false;
  64.             }
  65.             vcols[num] = true;
  66.         }
  67.         // 3x3
  68.         vector<bool> vsqrs(9, false);
  69.         int basei = i/3*3;
  70.         int basej = j/3*3;
  71.         for (int m = 0; m < 9; m++) {
  72.             char c = board[basei+m/3][basej+m%3];
  73.             if (c == '.') continue;
  74.             int num = c - '1';
  75.             if (vsqrs[num]) return false;
  76.             vsqrs[num] = true;
  77.         }
  78.         return true;
  79.     }
  80.     bool tryNext(vector<vector<char>>& board, vector<vector<bool>>& cache) {
  81.         bool started = false;
  82.         for (int i = 0; i < 9; i++) {
  83.             for (int j = 0; j < 9; j++) {
  84.                 char c = board[i][j];
  85.                 if (c != '.') continue;
  86.                
  87.                 // try every possible values
  88.                 for (int m = 0; m < 9; m++) {
  89.                     if (!cache[i][m]) continue;

  90.                     board[i][j] = m + '1';
  91.                     cache[i][m] = false;
  92.                     if (validatePos(board, i, j)
  93.                         && tryNext(board, cache)) {
  94.                         return true;
  95.                     }
  96.                     board[i][j] = '.';
  97.                     cache[i][m] = true;
  98.                 }
  99.                 // tried every possible ways still no luck, must have made a wrong attempt
  100.                 return false;
  101.             }
  102.         }
  103.         // nothing left to guess
  104.         return true;
  105.     }
  106.     void solveSudoku(vector<vector<char>>& board) {
  107.         // pre-calc per row
  108.         vector<vector<bool>> cache(9, vector<bool>(9, true));
  109.         for (int i = 0; i < 9; i++) {
  110.             for (int j = 0; j < 9; j++) {
  111.                 char c = board[i][j];
  112.                 if (c != '.') {
  113.                     cache[i][c - '1'] = false;
  114.                 }
  115.             }
  116.         }
  117.         tryNext(board, cache);
  118.     }
  119. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 19:10:45 | 只看该作者
全局:
0038. Count and Say
  1. class Solution {
  2. public:
  3.     string countAndSay(int n) {
  4.         string base = "1";
  5.         if (n < 2) return base;

  6.         while (n > 1) {
  7.             n--;
  8.             string out = "";
  9.             int idx = 0;
  10.             while (idx < base.size()) {
  11.                 int count = 0;
  12.                 char c = base[idx];
  13.                 int i = idx;
  14.                 for(; i < base.size(); i++) {
  15.                     if (base[i] != c) break;
  16.                     count++;
  17.                 }
  18.                 out += to_string(count) + c;
  19.                 idx = i;
  20.             }
  21.             base = out;
  22.         }
  23.         return base;
  24.     }
  25. };

  26. int main(int argc, char* argv[]) {
  27.     cout << Solution().countAndSay(10) << endl;

  28.     return 0;
  29. }
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 19:11:10 | 只看该作者
全局:
0042. Trapping Rain Water
  1. class Solution {
  2. public:
  3.     int accumulateWater(const vector<int>& height, int left, int right) {
  4.         int topH = min(height[left], height[right]);
  5.         int res = 0;
  6.         for (int i = left + 1; i < right; i++) {
  7.             if (height[i] < topH) {
  8.                 res += topH - height[i];
  9.             }
  10.         }
  11.         return res;
  12.     }
  13.     int trap(vector<int>& height, int l, int r) {
  14.         int left = l;
  15.         int i = left + 1;
  16.         int maxH = i;
  17.         int res = 0;
  18.         while (i <= r) {
  19.             if (height[i] > height[maxH])
  20.                 maxH = i;
  21.             // a bar is same or higher than left bar
  22.             if (height[i] >= height[left]) {
  23.                 res += accumulateWater(height, left, i);
  24.                 left = i++;
  25.                 maxH = i;
  26.                 continue;
  27.             }
  28.             i++;
  29.         }
  30.         // left is the highest
  31.         if (left < r - 1) {
  32.             res += accumulateWater(height, left, maxH);
  33.             return res + trap(height, maxH, r);
  34.         }
  35.         return res;
  36.     }
  37.     int trap(vector<int>& height) {
  38.         return trap(height, 0, height.size()-1);
  39.     }
  40. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 19:11:32 | 只看该作者
全局:
0045. Jump Game II
  1. class Solution {
  2. public:
  3.     int jump(vector<int>& nums) {
  4.         if (nums.size() < 2)
  5.             return 0;
  6.         int ret = 0;
  7.         int i = 0;
  8.         while (i < nums.size()-1) {
  9.             int val = nums[i];
  10.             if (i + val >= nums.size() - 1) {
  11.                 ret++;
  12.                 break;
  13.             }
  14.             int maxj = 0;
  15.             int nexti = i + 1;
  16.             for (int j = 1; j <= val; j++) {
  17.                 if (nums[i+j] + j > maxj) {
  18.                     maxj = nums[i+j] + j;
  19.                     nexti = i + j;
  20.                 }
  21.             }
  22.             i = nexti;
  23.             ret++;
  24.         }
  25.         return ret;
  26.     }
  27. };
  28. ```
  29. ```c++
  30. // count backwards
  31. class Solution {
  32. public:
  33.     int jump(vector<int>& nums) {
  34.         if (nums.size() < 2)
  35.             return 0;
  36.         if (nums.size() < 3) {
  37.             return 1;
  38.         }
  39.         vector<int> v(nums.size(), nums.size()+1);
  40.         v[nums.size()-1] = 0;
  41.         v[nums.size()-2] = 1;
  42.         for (int i = nums.size()-3; i >= 0; i--) {
  43.             for (int j = 1; j <= nums[i]; j++) {
  44.                 if (i+j >= nums.size())
  45.                     break;
  46.                 v[i] = min(v[i], v[i+j]+1);
  47.             }
  48.         }
  49.         return v[0];
  50.     }
  51. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 19:12:02 | 只看该作者
全局:
0054. Spiral Matrix
  1. class Solution {
  2. public:
  3.     vector<int> spiralOrder(vector<vector<int>>& matrix) {
  4.         if (matrix.empty() || matrix[0].empty())
  5.             return vector<int>();
  6.         
  7.         vector<int> res;
  8.         int m = matrix.size();
  9.         int n = matrix[0].size();
  10.         int layer = 0;
  11.         int i = 0, j = 0;
  12.         while(layer <= (min(m,n)-1)/2) {
  13.             res.push_back(matrix[i][j]);
  14.             // to right
  15.             if (j < n - layer) {
  16.                 j++;
  17.                 while(j < n - layer) {
  18.                     res.push_back(matrix[i][j]);
  19.                     j++;
  20.                 }
  21.                 j--;
  22.             }
  23.             // to bottom
  24.             if (i >= m - 1 - layer)
  25.                 break;
  26.             i++;
  27.             while (i < m - layer) {
  28.                 res.push_back(matrix[i][j]);
  29.                 i++;
  30.             }
  31.             i--;
  32.             
  33.             // to left
  34.             if (j <= layer)
  35.                 break;
  36.             j--;
  37.             while (j >= layer) {
  38.                 res.push_back(matrix[i][j]);
  39.                 j--;
  40.             }
  41.             j++;
  42.             
  43.             // to top
  44.             if (i <= layer)
  45.                 break;
  46.             i--;
  47.             while (i > layer) {
  48.                 res.push_back(matrix[i][j]);
  49.                 i--;
  50.             }
  51.             layer++;
  52.             i = layer;
  53.             j = layer;
  54.         }
  55.         return res;
  56.     }
  57. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 19:12:37 | 只看该作者
全局:
0055. Jump Game
  1. class Solution {
  2. public:
  3.     bool canJump(vector<int>& nums) {
  4.             int maxj = 0;
  5.             for (int i = 0; i < nums.size(); i ++) {
  6.                 if (i > maxj)
  7.                     return false;
  8.                 maxj = max(maxj, nums[i] + i);
  9.                 if (maxj >= nums.size() - 1)
  10.                     return true;
  11.             }
  12.         return false;
  13.     }
  14. };

  15. int main(int argc, char* argv[]) {
  16.     vector<vector<int>> v = {
  17.         {3,2,1,0,4},
  18.         {5,9,3,2,1,0,2,3,3,1,0,0}
  19.         };
  20.    
  21.     for (auto vv : v)
  22.      cout << Solution().canJump(vv) << endl;
  23.     return 0;
  24. }
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 19:13:06 | 只看该作者
全局:
0056. Merge Intervals
  1. struct Interval {
  2.     int start;
  3.     int end;
  4.     Interval() : start(0), end(0) {}
  5.     Interval(int s, int e) : start(s), end(e) {}
  6. };

  7. class Solution {
  8. public:
  9.     vector<Interval> merge(vector<Interval>& intervals) {
  10.         if (intervals.size() < 2)
  11.             return intervals;

  12.         sort(intervals.begin(), intervals.end(), [](Interval& a, Interval& b) {
  13.             return a.start < b.start;
  14.         });
  15.         vector<Interval> res;
  16.         int i = 0;
  17.         while (i < intervals.size() - 1) {
  18.             auto& a = intervals[i];
  19.             auto& b = intervals[i+1];
  20.             if (a.end < b.start) {
  21.                 res.push_back(a);
  22.             } else {
  23.                 b.start = a.start;
  24.                 b.end = max(b.end, a.end);
  25.             }
  26.             i++;
  27.         }
  28.         res.push_back(intervals[intervals.size() - 1]);
  29.         return res;
  30.     }
  31. };

  32. int main(int argc, char* argv[]) {
  33.     vector<Interval> v = {
  34.         Interval(1,5),
  35.         Interval(3,2),
  36.         Interval(5,8)
  37.         };

  38.     auto vv = Solution().merge(v);
  39.     for(auto a : vv)   
  40.         cout << a.start << ' ' << a.end << endl;
  41.     return 0;
  42. }
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 19:13:34 | 只看该作者
全局:
0057. Insert Interval
  1. class Solution {
  2. public:
  3.     vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
  4.         if (intervals.empty()) {
  5.             vector<Interval> res;
  6.             res.push_back(newInterval);
  7.             return res;
  8.         }

  9.         sort(intervals.begin(), intervals.end(), [](Interval&a, Interval& b) {
  10.             return a.start < b.start;
  11.         });

  12.         if (newInterval.end < intervals[0].start) {
  13.             intervals.insert(intervals.begin(), newInterval);
  14.             return intervals;
  15.         }
  16.         vector<Interval> res;
  17.         auto n = newInterval;
  18.         for (int i = 0; i < intervals.size(); i++) {
  19.             auto& t = intervals[i];
  20.             // the new is after t, put t in res
  21.             if (n.start > t.end) {
  22.                 res.push_back(t);
  23.                 continue;
  24.             }
  25.             // the new is before t
  26.             if (n.end < t.start) {
  27.                 res.push_back(n);
  28.                 res.insert(res.end(), intervals.begin()+i, intervals.end());
  29.                 return res;
  30.             }
  31.             // need merge
  32.             n.start = min(n.start, t.start);
  33.             n.end = max(n.end, t.end);
  34.         }
  35.         res.push_back(n);
  36.         return res;
  37.     }
  38. };
复制代码
回复

使用道具 举报

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

本版积分规则

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