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

Leetcode 刷题打卡记录 包括Solution

🔗
 楼主| wglacier 2019-1-31 16:30:39 | 只看该作者
全局:
0002 Add Two Numbers
  1. class Solution {
  2. private:
  3.     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2, int carry) {
  4.         if (l1 == NULL && l2 == NULL && carry == 0) {
  5.             return NULL;
  6.         }
  7.         int r = carry;
  8.         if (l1 != NULL) r += l1->val;
  9.         if (l2 != NULL) r += l2->val;
  10.         ListNode* node = new ListNode(r % 10);
  11.         node->next = addTwoNumbers(l1?l1->next:NULL, l2? l2->next:NULL, r / 10);
  12.         return node;
  13.     }
  14. public:
  15.     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
  16.         return addTwoNumbers(l1, l2, 0);
  17.     }
  18. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 16:31:19 | 只看该作者
全局:
0003 Longest Substring Without Repeating Characters
  1. class Solution {
  2. public:
  3.     int lengthOfLongestSubstring(string s) {
  4.         int ret = 0;
  5.         int max_t = 0;
  6.         vector<int> buf(256, -1);
  7.         for (int i = 0; i < s.size(); i++) {
  8.             char c = s[i];
  9.             if (buf[c] < 0) {
  10.                 max_t++;
  11.             } else {
  12.                 max_t = min(max_t + 1, i - buf[c]);
  13.             }
  14.             buf[c] = i;
  15.             ret = max(ret, max_t);
  16.         }
  17.         return ret;
  18.     }
  19. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 16:33:05 | 只看该作者
全局:
0004 Median of Two Sorted Arrays
  1. class Solution {
  2. public:
  3.     double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
  4.         int z = nums1.size() + nums2.size();
  5.         int m = (z -1) / 2;
  6.         int i = 0, j = 0;
  7.         int k = 0;
  8.         int v1 = 0, v2 = 0;
  9.         while (i < nums1.size() || j < nums2.size()) {
  10.             int v = 0;
  11.             if (j >= nums2.size() || ( i < nums1.size() && nums1[i] < nums2[j])) {
  12.                 v = nums1[i];
  13.                 i += 1;
  14.             } else {
  15.                 v = nums2[j];
  16.                 j += 1;
  17.             }
  18.             if (k == m) {
  19.                 v1 = v;
  20.             } else if (k == m+1) {
  21.                 v2 = v;
  22.                 break;
  23.             }
  24.             k += 1;
  25.         }
  26.         if (z % 2 != 0) {
  27.             return v1;
  28.         } else {
  29.             return (v1 + v2) / 2.0;
  30.         }
  31.     }
  32. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 16:33:53 | 只看该作者
全局:
0005. Longest Palindromic Substring
  1. class Solution {
  2. public:
  3.     string longestPalindrome(string s) {
  4.         vector<vector<int>> v(s.size(), vector<int>(s.size(), 0));
  5.         for(int i = 0; i < s.size(); i++) {
  6.             v[i][i] = 1;
  7.         }
  8.         
  9.         int r1 = 0, r2 = 0, r_len = 1;
  10.         for(int j = 1; j < s.size(); j++) {
  11.             for(int i = 0; i < s.size(); i++) {            
  12.                 if (i + j >= s.size()) continue;
  13.                 int a = i, b = i + j;
  14.                 if (s[a] != s[b]) continue;
  15.                
  16.                 if (b - a <= 2 || v[a+1][b-1] > 0) {
  17.                     if (b - a <= 2) {
  18.                         v[a][b] = b - a + 1;
  19.                     } else {
  20.                         v[a][b] = v[a+1][b-1] + 2;
  21.                     }
  22.                     if (v[a][b] > r_len) {
  23.                         r_len = v[a][b];
  24.                         r1 = a, r2 = b;
  25.                     }
  26.                 }
  27.             }
  28.         }
  29.         return s.substr(r1, r2 - r1 + 1);
  30.     }
  31. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 16:34:48 | 只看该作者
全局:
Another solution for 0005
//Expand around centers

  1. ```C++
  2. class Solution {
  3. private:
  4.     int findStr(const string& s, int& a, int& b) {
  5.         while(a >= 0 && b < s.size() && s[a] == s[b]) {
  6.             a--, b++;
  7.         }
  8.         a++, b--;
  9.         if (a > b) return 0;
  10.         return b-a+1;
  11.     }
  12. public:
  13.     string longestPalindrome(string s) {
  14.         int s0 = 0, e0 = 0;
  15.         for(int i = 1; i < s.size(); i++) {
  16.             int a = i-1, b = i+1;
  17.             int r = findStr(s, a, b);
  18.             if (r > (e0-s0+1)) {
  19.                 s0 = a;
  20.                 e0 = b;
  21.             }
  22.             a = i-1;
  23.             b = i;
  24.             r = findStr(s, a, b);
  25.             if (r > (e0-s0+1)) {
  26.                 s0 = a;
  27.                 e0 = b;
  28.             }
  29.         }
  30.         return s.substr(s0, e0-s0+1);
  31.     }
  32. };
复制代码

回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 16:35:54 | 只看该作者
全局:
0006 Zigzag Conversion
  1. class Solution {
  2. public:
  3.     string convert(string s, int numRows) {
  4.         if (numRows < 2 || s.size() < 2) return s;
  5.         
  6.         vector<string> ret = vector<string>(numRows, "");
  7.         int step = -1;
  8.         int r = 0;
  9.         for(auto c : s) {
  10.             ret[r] += c;
  11.             if (r == 0 || (r == numRows - 1)) step *= -1;
  12.             r += step;
  13.         }
  14.         return std::accumulate(ret.begin(), ret.end(), string(""));
  15.     }
  16. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 16:36:55 | 只看该作者
全局:
0007. Reverse Integer
  1. class Solution {
  2. public:
  3.     int reverse(int x) {
  4.         int x0 = x;
  5.         if (x < 0) x *= -1;
  6.         
  7.         int r = 0;
  8.         while (x > 0) {
  9.             int t = x % 10;
  10.             if (r*10/10 != r) return 0;
  11.             r = r * 10 + t;
  12.             if (r & 0x80000000) return 0;
  13.             x /= 10;
  14.         }
  15.         if (x0 < 0) r *= -1;
  16.         return r;
  17.     }
  18. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 16:37:40 | 只看该作者
全局:
0008. String to Integer (atoi)
  1. #include <climits>

  2. class Solution {
  3. private:
  4.     int overFlow(int sign) {
  5.         if (sign > 0) return INT_MAX;
  6.         else return INT_MIN;
  7.     }
  8. public:
  9.     int myAtoi(string str) {
  10.         int r = 0;
  11.         int sign = 1;
  12.         auto i = str.find_first_not_of(' ');
  13.         if (i == string::npos) return 0;
  14.         if (str[i] == '-') {
  15.             sign = -1;
  16.             i++;
  17.         } else if (str[i] == '+')
  18.             i++;

  19.         for (; i < str.size(); i++) {
  20.             char c = str[i];
  21.             if (!isdigit(c)) break;
  22.             
  23.             if (r * 10 / 10 != r)
  24.                 return overFlow(sign);
  25.             r = r * 10;
  26.             int v = (c - '0') * sign;
  27.             if( sign > 0 && (r + v < r))
  28.                 return overFlow(sign);
  29.             else if (sign < 0 && (r + v > r))
  30.                 return overFlow(sign);
  31.             r = r + v;
  32.         }
  33.         return r;
  34.     }
  35. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 16:39:13 | 只看该作者
全局:
0009. Palindrome Number
  1. // Construct another integer
  2. class Solution {
  3. public:
  4.     bool isPalindrome(int x) {
  5.         int r = 0;
  6.         int x0 = x;
  7.         while(x > 0) {
  8.             r = r*10 + x % 10;
  9.             x = x / 10;
  10.         }
  11.         return x0 == r;
  12.     }
  13. };
复制代码


Compare bit by bit from the two ends
  1. class Solution {
  2. public:
  3.     bool isPalindrome(int x) {
  4.         if (x < 0) return false;
  5.         
  6.         const int max_u = 1000000000;
  7.         int upbase = max_u;
  8.         int dobase = 10;

  9.         while(upbase > 0 && (x / upbase == 0))
  10.             upbase /= 10;
  11.         if (upbase < 10) return true;
  12.         
  13.         while(upbase >= dobase) {
  14.             int a = 0;
  15.             if (upbase == max_u) a = x / upbase;
  16.             else a = x % (upbase*10) / upbase;
  17.             
  18.             int b = x % dobase / (dobase/10);
  19.             if (a != b) return false;
  20.             upbase /= 10;
  21.             dobase *= 10;
  22.         }
  23.         return true;
  24.     }
  25. };
复制代码
回复

使用道具 举报

🔗
 楼主| wglacier 2019-1-31 16:39:51 | 只看该作者
全局:
0010. Regular Expression Matching
  1. class Solution {
  2. public:
  3.     bool isMatch(string s, string p) {
  4.         if (p.empty())
  5.             return s.empty();

  6.         vector<vector<bool>> dp(s.size()+1, vector<bool>(p.size()+1, false));
  7.         dp[0][0] = true;
  8.         if (!s.empty())
  9.             dp[1][1] = p[0] == '.' || p[0] == s[0];

  10.         for (int j = 2; j <= p.size(); j++) {
  11.             // special cases, second char in p is '*', like 'a*b*c*' can match ''
  12.             dp[0][j] = p[j-1] == '*' && dp[0][j-2];

  13.             for (int i = 1; i <= s.size(); i++) {
  14.                 if (p[j-1] != '*') {
  15.                     dp[i][j] = dp[i-1][j-1]
  16.                         && (p[j-1] == '.' || s[i-1] == p[j-1]);
  17.                 } else {
  18.                     dp[i][j] = dp[i][j-2]   // match 0 time
  19.                         // || dp[i][j-1]
  20.                         || (dp[i-1][j]
  21.                              && (p[j-2] == '.' || s[i-1] == p[j-2]));
  22.                 }
  23.             }
  24.         }
  25.         return dp[s.size()][p.size()];
  26.     }
  27. };

  28. int main(int argc, char* argv[]) {
  29. vector<tuple<string,string,bool>> v = {
  30.     {"aa", "a", false},
  31.     {"a", "a", true},
  32.     {"", "a*", true},
  33.     {"", "ABC", false},
  34.     {"abc", "abcd", false},
  35.     {"abc", "babc", false},
  36.     {"abc", ".*abc", true},
  37.     {"abc", ".*bc", true},
  38.     {"abc", ".*c", true},
  39.     {"abc", ".*", true},
  40.     {"abc", "a*b*c*", true},
  41.     {"abc", "a*b*c", true}
  42. };
  43.     for (auto a : v) {
  44.         bool r = Solution().isMatch(get<0>(a), get<1>(a));
  45.         if (r != get<2>(a)) {
  46.             cout << "error: " << get<0>(a) << ' '  << get<1>(a) << ' ' << r << ", expected: " << get<2>(a) << endl;
  47.         }
  48.     }
  49.     return 0;
  50. }
复制代码
回复

使用道具 举报

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

本版积分规则

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