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

Leetcode 刷题打卡记录 包括Solution

🔗
 楼主| wglacier 2019-3-4 05:14:20 | 只看该作者
全局:
535. Encode and Decode TinyURL
  1. class Solution {
  2.     unordered_map<long long, string> cache;
  3.    
  4.     const static int LEN = 7;
  5.     static const constexpr char* table = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  6. public:
  7.     // Encodes a URL to a shortened URL.
  8.     string encode(string longUrl) {
  9.         long long uid = cache.size() + 1;
  10.         cache.insert(make_pair(uid, longUrl));

  11.         string code;
  12.         for (int i = 0; i < LEN; i++) {
  13.             code += table[uid%62];
  14.             uid /= 62;
  15.         }
  16.         return string(code.rbegin(), code.rend());
  17.     }

  18.     // Decodes a shortened URL to its original URL.
  19.     string decode(string shortUrl) {
  20.         long long uid = 0;
  21.         for (auto &c : shortUrl) {
  22.             int a = 0;
  23.             if (c >= 'a' && c <= 'z') a = c - 'a';
  24.             else if (c >= 'A' && c <= 'Z') a = c - 'A' + 26;
  25.             else if (c >= '0' && c <= '9') a = c - '0' + 52;
  26.             else {
  27.                 // error
  28.             }
  29.             uid = uid * 62 + a;
  30.         }
  31.         auto it = cache.find(uid);
  32.         if (it == cache.end()) {
  33.             return "error";
  34.         }
  35.         return it->second;
  36.     }
  37. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-3-4 19:19:41 | 只看该作者
全局:
739. Daily Temperatures
  1. class Solution {
  2. public:
  3.     vector<int> dailyTemperatures(vector<int>& T) {
  4.         vector<int> res(T.size(), 0);
  5.         
  6.         for (int i = T.size()-2; i >= 0; i--) {
  7.             int j = i + 1;
  8.             while (j < T.size()) {
  9.                 if (T[i] < T[j]) {
  10.                     res[i] = j - i;
  11.                     break;
  12.                 }
  13.                 if (res[j] == 0) break;
  14.                
  15.                 if (T[i] == T[j]) {
  16.                     res[i] = res[j] + j - i;
  17.                     break;
  18.                 }
  19.                
  20.                 j = j + res[j];
  21.             }
  22.         }
  23.         
  24.         return res;
  25.     }
  26. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-3-5 17:50:33 | 只看该作者
全局:
760. Find Anagram Mappings
  1. class Solution {
  2. public:
  3.     vector<int> anagramMappings(vector<int>& A, vector<int>& B) {
  4.         unordered_map<int, list<int>> cache;
  5.         for (int i = 0; i < B.size(); i++) {
  6.             cache[B[i]].push_back(i);
  7.         }
  8.         vector<int> res(A.size(), 0);
  9.         for (int i = 0; i < A.size(); i++) {
  10.             auto it = cache.find(A[i]);
  11.             res[i] = it->second.front();
  12.             it->second.pop_front();
  13.         }
  14.         return res;
  15.     }
  16. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-3-5 17:51:29 | 只看该作者
全局:
904. Fruit Into Baskets
  1. class Solution {
  2. public:
  3.     int totalFruit(vector<int>& tree) {
  4.         if (tree.empty()) return 0;
  5.         
  6.         int res = 1;
  7.         int a = tree.front();
  8.         int a_count = 1;
  9.         int b;
  10.         bool bf = false;
  11.         int acc = 1;
  12.         for (int i = 1; i < tree.size(); i++) {
  13.             int c = tree[i];
  14.             // same as last one
  15.             if (c == a) {
  16.                 ++acc;
  17.                 a_count++;
  18.             } else {
  19.                 // the other basket is empty
  20.                 if (!bf) {
  21.                     bf = true;
  22.                     b = a;
  23.                     a = c;
  24.                     a_count = 1;
  25.                     ++acc;
  26.                 } else {
  27.                     if (c == b) {
  28.                         ++acc;
  29.                         swap(a, b);
  30.                         a_count = 1;
  31.                     } else {
  32.                         // discard b
  33.                         b = a;
  34.                         acc = a_count + 1;
  35.                         a = c;
  36.                         a_count = 1;
  37.                     }
  38.                 }
  39.             }
  40.             res = max(res, acc);
  41.         }
  42.         return res;
  43.     }
  44. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-3-21 05:26:56 | 只看该作者
全局:
30. Substring with Concatenation of All Words
Solution 2 using Trie, 44ms
  1. class Trie {
  2.     Trie *cc[26];
  3.     int pos;
  4. public:
  5.     Trie() {
  6.         pos = -1;
  7.         fill_n(cc, 26, nullptr);
  8.     }
  9.     void insert(const string &s, int position) {
  10.         Trie* p = this;
  11.         for (auto &c : s) {
  12.             int idx = c - 'a';
  13.             if (!p->cc[idx]) {
  14.                 p->cc[idx] = new Trie();
  15.             }
  16.             p = p->cc[idx];
  17.         }
  18.         p->pos = position;
  19.     }
  20.     int search(const string &s, int start, int sz) {
  21.         Trie* p = this;
  22.         for (int i = start; i < start + sz; i++) {
  23.             int idx = s[i] - 'a';
  24.             if (!p->cc[idx]) return -1;
  25.             p = p->cc[idx];
  26.         }
  27.         return p->pos;
  28.     }
  29. };

  30. class Solution {
  31. public:
  32.     vector<int> findSubstring(string s, vector<string>& words) {
  33.         vector<int> res;
  34.         if (words.empty() || s.empty())
  35.             return res;
  36.         unordered_map<string,int> mm;
  37.         for (auto &w : words) {
  38.             mm[w]++;
  39.         }
  40.         vector<int> flagTmpl(mm.size(), 0);
  41.         Trie trie;
  42.         int i = 0;
  43.         for (auto itt : mm) {
  44.             flagTmpl[i] = itt.second;
  45.             trie.insert(itt.first, i);
  46.             i++;
  47.         }

  48.         vector<int> dp(s.size(), -2);
  49.         const int WD_LEN = words[0].size();
  50.         for (int i = 0; i <= (int)s.size()-(int)(WD_LEN*words.size()); i++) {
  51.             if (dp[i] == -1) continue;

  52.             vector<int> flags(flagTmpl);
  53.             int flagCount = 0;
  54.             int start = i;
  55.             while (flagCount < words.size() && start <= s.size() - WD_LEN) {
  56.                 int pos = dp[start];
  57.                 if (pos == -1) break;
  58.                 if (pos == -2) {
  59.                     pos = trie.search(s, start, WD_LEN);
  60.                 }
  61.                 if (pos < 0) {
  62.                     dp[start] = -1;
  63.                     break;
  64.                 }
  65.                 if (flags[pos] < 1) {
  66.                     break;
  67.                 }
  68.                 flags[pos]--;
  69.                 dp[start] = pos;
  70.                 start += WD_LEN;
  71.                 ++flagCount;
  72.             }
  73.             if (flagCount == words.size()) {
  74.                 res.push_back(i);
  75.             }
  76.         }
  77.         return res;
  78.     }
  79. };
复制代码

回复

使用道具 举报

🔗
 楼主| wglacier 2019-3-22 18:48:59 | 只看该作者
全局:
41. First Missing Positive
Sulotion 2
  1. class Solution {
  2. public:
  3.     int firstMissingPositive(vector<int>& nums) {
  4.         if (nums.empty()) return 1;
  5.         
  6.         int i = 0;
  7.         while (i < nums.size()) {
  8.             while (true) {
  9.                 int v = nums[i];
  10.                 if (v == i+1) break;
  11.                
  12.                 if (v <= 0 || v > nums.size()) {
  13.                     nums[i] = -1;
  14.                     break;
  15.                 }
  16.                 if (v == nums[v-1]){
  17.                     nums[i] = -1;
  18.                     break;
  19.                 }

  20.                 swap(nums[i], nums[v-1]);
  21.             }
  22.             ++i;
  23.         }
  24.         i = 0;
  25.         for (; i < nums.size(); i++) {
  26.             if (nums[i] != i+1) return i+1;
  27.         }
  28.         return i+1;
  29.     }
  30. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-3-30 18:06:34 | 只看该作者
全局:
43. Multiply Strings
  1. class Solution {
  2. public:
  3.     string multiply(string num1, string num2) {
  4.         vector<int> res(num1.size() + num2.size(), 0);
  5.         for (int j = 0; j < num2.size(); j++) {
  6.             int d2 = num2[num2.size()-j-1] - '0';
  7.             int i = j;
  8.             for (int k = 0; k < num1.size(); k++) {
  9.                 int d1 = num1[num1.size()-k-1] - '0';
  10.                 int r = d1 * d2 + res[i];
  11.                 res[i] = r % 10;
  12.                 res[i+1] += r / 10;
  13.                 i++;
  14.             }
  15.         }
  16.         int i = res.size()-1;
  17.         while (i >= 0 && res[i] == 0) i--;
  18.         if (i < 0) return "0";
  19.         
  20.         string s;
  21.         while (i >= 0) {
  22.             s += to_string(res[i]);
  23.             i--;
  24.         }
  25.         return s;
  26.     }
  27. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-4-1 15:27:06 | 只看该作者
全局:
44. Wildcard Matching
  1. class Solution {
  2. public:
  3.     bool isMatch(string s, string p) {
  4.         vector<vector<bool>> dp(s.size()+1, vector<bool>(p.size()+1, false));
  5.         dp[0][0] = true;
  6.         for (uint32_t i = 0; i <= s.size(); i++) {
  7.             for (uint32_t j = 1; j <= p.size(); j++) {
  8.                 // for p starts with some '*'
  9.                 if (i == 0) {
  10.                     dp[0][j] = p[j-1] == '*' && dp[0][j-1];
  11.                 }
  12.                 else if (p[j-1] == s[i-1] || p[j-1] == '?')
  13.                     dp[i][j] = dp[i-1][j-1];
  14.                 else if (p[j-1] == '*')
  15.                     dp[i][j] = dp[i-1][j]   // match 0 or 1 time
  16.                      || dp[i][j-1];         // match 1+ times
  17.             }
  18.         }
  19.         return dp[s.size()][p.size()];
  20.     }
  21. };
复制代码
回复

使用道具 举报

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

本版积分规则

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