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

Leetcode 刷题打卡记录 包括Solution

🔗
 楼主| wglacier 2019-2-27 17:30:02 | 只看该作者
全局:
341. Flatten Nested List Iterator
  1. class NestedIterator {
  2. private:
  3.     using ContainerType = const vector<NestedInteger>;
  4.     using IterType = ContainerType::const_iterator;
  5.     stack<pair<ContainerType*, IterType>> mList;
  6.    
  7.     void locateData() {
  8.         if (mList.empty()) return;
  9.         while (true) {
  10.             if (mList.top().first->end() == mList.top().second) {
  11.                 if (mList.size() == 1) return;
  12.                 mList.pop();
  13.                 mList.top().second++;
  14.                 continue;
  15.             }
  16.             auto &this_list = *(mList.top().second);
  17.             if (this_list.isInteger()) return;
  18.             mList.push(make_pair(&(this_list.getList()), this_list.getList().begin()));
  19.         }
  20.     }
  21.     void incrIter() {
  22.         if (mList.empty()) return;
  23.         if (mList.top().first->end() == mList.top().second) {
  24.             if (mList.size() == 1)
  25.                 return;
  26.             mList.pop();
  27.         }
  28.         mList.top().second++;
  29.         locateData();
  30.     }
  31. public:
  32.     NestedIterator(vector<NestedInteger> &nestedList) {
  33.         mList.push(make_pair(&nestedList, nestedList.begin()));
  34.         locateData();
  35.     }

  36.     int next() {
  37.         // assert mList not empty and has next element
  38.         auto &top = mList.top();
  39.         auto pList = top.first;
  40.         auto iter = top.second;
  41.         int res;
  42.         if (iter == pList->end()) return -1;
  43.         
  44.         if ((*iter).isInteger()) {
  45.             res = (*iter).getInteger();
  46.         } else {
  47.             // not defined
  48.         }
  49.         incrIter();
  50.         
  51.         return res;
  52.     }

  53.     bool hasNext() {
  54.         return !mList.empty() && mList.top().second != mList.top().first->end();
  55.     }
  56. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-2-27 17:46:30 | 只看该作者
全局:
341. Flatten Nested List Iterator
Solution 2: Pre-processing

  1. class NestedIterator {
  2. private:
  3.     vector<int> m_list;
  4.     int m_idx;
  5.    
  6.     void add(const vector<NestedInteger> &nestedList) {
  7.         for (auto &node : nestedList) {
  8.             if (node.isInteger()) {
  9.                 m_list.push_back(node.getInteger());
  10.             } else {
  11.                 add(node.getList());
  12.             }
  13.         }
  14.     }
  15. public:
  16.     NestedIterator(vector<NestedInteger> &nestedList) {
  17.         add(nestedList);
  18.         m_idx = 0;
  19.     }

  20.     int next() {
  21.         return m_list[m_idx++];
  22.     }

  23.     bool hasNext() {
  24.         return m_idx < m_list.size();
  25.     }
  26. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-2-27 17:57:18 | 只看该作者
全局:
345. Reverse Vowels of a String
  1. class Solution {
  2. public:
  3.     string reverseVowels(string s) {
  4.         int i = 0, j = s.size()-1;
  5.         unordered_set<char> vws = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
  6.         while (i < j) {
  7.             while (i < j && vws.count(s[i]) == 0) i++;
  8.             while (i < j && vws.count(s[j]) == 0) j--;
  9.             swap(s[i++], s[j--]);
  10.         }
  11.         return s;
  12.     }
  13. };
复制代码
回复

使用道具 举报

🔗
gtonyoogle 2019-2-28 02:55:46 | 只看该作者
全局:
楼主加油,坚持
回复

使用道具 举报

🔗
 楼主| wglacier 2019-2-28 14:12:20 | 只看该作者
全局:
388. Longest Absolute File Path
  1. class Solution {
  2. public:
  3.     int lengthLongestPath(string input) {
  4.         stack<int> stk;
  5.         int level = 0;
  6.         string tok;
  7.         int res = 0;
  8.         int len = 0;
  9.         input += "\n";
  10.         bool isFile = false;
  11.         for (auto &c : input) {
  12.             if (c == '\t') {
  13.                 ++level;
  14.                 continue;
  15.             }
  16.             if (c == '\n') {
  17.                 if (!stk.empty()) {
  18.                     // same level, and the previous folder has finished
  19.                     while (level + 1 <= stk.size()) {
  20.                         if (stk.size() > 1) --len; // the path sep '/'
  21.                         len -= stk.top();
  22.                         stk.pop();
  23.                     }
  24.                 }
  25.                 if (len > 0) ++len; // the path sep '/'
  26.                 len += tok.size();
  27.                 if (isFile) {
  28.                     res = max(res, len);
  29.                 }
  30.                 stk.push(tok.size());
  31.                
  32.                 isFile = false;
  33.                 level = 0;
  34.                 tok.clear();
  35.                 continue;
  36.             }
  37.             if (c == '.') isFile = true;
  38.             tok += c;
  39.         }
  40.         return res;
  41.     }
  42. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-2-28 19:06:41 | 只看该作者
全局:
406. Queue Reconstruction by Height, medium
  1. class Solution {
  2. public:
  3.     vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
  4.         // sort so people with k == 0 appear first and sorted by height
  5.         sort(people.begin(), people.end(), [](auto &a, auto &b) {
  6.             return a.second < b.second ||
  7.                 (a.second == b.second && a.first < b.first);
  8.         });
  9.         vector<pair<int, int>> res;
  10.         int i = 0;
  11.         while (i < people.size()) {
  12.             auto &a = people[i];
  13.             if (a.second == 0) {
  14.                 res.push_back(a);
  15.             } else {
  16.                 auto it = res.begin();
  17.                 int gt = a.second;
  18.                 while (it != res.end()) {
  19.                     if (it->first >= a.first) {
  20.                         if (gt > 0) {
  21.                             gt--;
  22.                         } else {
  23.                             break;
  24.                         }
  25.                     }
  26.                     it++;
  27.                 }
  28.                 res.insert(it, a);
  29.             }
  30.             i++;
  31.         }
  32.         return res;
  33.     }
  34. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-2-28 19:07:06 | 只看该作者
全局:
406. Queue Reconstruction by Height
Solution 2
  1. class Solution {
  2. public:
  3.     vector<pair<int, int>> reconstructQueue(vector<pair<int, int>>& people) {
  4.         // sort by height desc then by k asc
  5.         sort(people.begin(), people.end(), [](auto &a, auto &b) {
  6.             return a.first > b.first ||
  7.                 (a.first == b.first && a.second < b.second);
  8.         });
  9.         // insert the person into their correct place.
  10.         // because they are sorted by descending height, inserting later people don't affect people in the queue.
  11.         vector<pair<int, int>> res;
  12.         for (int i = 0; i < people.size(); i++) {
  13.             res.insert(res.begin() + people[i].second, people[i]);
  14.         }
  15.         return res;
  16.     }
  17. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-3-3 06:02:02 | 只看该作者
全局:
421. Maximum XOR of Two Numbers in an Array
  1. class Trie {
  2.     Trie* cb[2];
  3.     int val;
  4. public:
  5.     Trie() {
  6.         cb[0] = cb[1] = nullptr;
  7.         val = 0;
  8.     }
  9.     void insert(int n) {
  10.         Trie* p = this;
  11.         for (int i = 30; i >= 0; i--) {
  12.             int bit = (n & ( 1 << i)) > 0;
  13.             if (!p->cb[bit])
  14.                 p->cb[bit] = new Trie();
  15.             p = p->cb[bit];
  16.         }
  17.         p->val = n;
  18.     }
  19.     int search(int n) {
  20.         Trie* p = this;
  21.         for (int i = 30; i >= 0; i--) {
  22.             int bit = (n & ( 1 << i)) > 0;
  23.             if (p->cb[!bit])
  24.                 p = p->cb[!bit];
  25.             else {
  26.                 if (p->cb[bit])
  27.                     p = p->cb[bit];
  28.                 else
  29.                     return 0;
  30.             }
  31.         }
  32.         return p->val;
  33.     }
  34. };

  35. class Solution {
  36.    
  37. public:
  38.     int findMaximumXOR(vector<int>& nums) {
  39.         if (nums.size() < 2) return 0;
  40.         
  41.         Trie root;
  42.         root.insert(nums.front());
  43.         int res = 0;
  44.         for (auto &a : nums) {
  45.             int b = root.search(a);
  46.             res = max(res, a ^ b);
  47.             root.insert(a);
  48.         }
  49.         return res;
  50.     }
  51. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-3-3 18:12:31 | 只看该作者
全局:
448. Find All Numbers Disappeared in an Array
  1. class Solution {
  2. public:
  3.     vector<int> findDisappearedNumbers(vector<int>& nums) {
  4.         vector<int> res;
  5.         for (int i = 0; i < nums.size(); i++) {
  6.             if (nums[i] == i + 1 ) continue;
  7.             
  8.             while (true) {
  9.                 int a = nums[i];
  10.                 if (a < 0 || a == i + 1) break;
  11.                 if (nums[a-1] == a) {
  12.                     nums[i] = -1;
  13.                     break;
  14.                 } else {
  15.                     swap(nums[i], nums[a-1]);
  16.                 }
  17.             }
  18.         }
  19.         for (int i = 0; i < nums.size(); i++) {
  20.             if (nums[i] != i + 1) {
  21.                 res.push_back(i + 1);
  22.             }
  23.         }
  24.         return res;
  25.     }
  26. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-3-3 18:12:56 | 只看该作者
全局:
448. Find All Numbers Disappeared in an Array
Solution 2
  1. class Solution {
  2. public:
  3.     vector<int> findDisappearedNumbers(vector<int>& nums) {
  4.         vector<int> res;
  5.         for (int i = 0; i < nums.size(); i++) {
  6.             int a = nums[i];
  7.             if (nums[abs(a) - 1] > 0) {
  8.                 nums[abs(a) - 1] *= -1;
  9.             }
  10.         }
  11.         for (int i = 0; i < nums.size(); i++) {
  12.             if (nums[i] > 0) {
  13.                 res.push_back(i + 1);
  14.             }
  15.         }
  16.         return res;
  17.     }
  18. };
复制代码
回复

使用道具 举报

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

本版积分规则

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