LeetCode 1-5题解

1 简单

Soluion

  • map
  • 排序 + 双指针

Sample Code (map)

class Solution {
public:
    map<int, int> mp;
    map<int, int> pos;
    vector<int> twoSum(vector<int>& nums, int target) {
        int len = nums.size();
        for(int i = 0; i < len; ++i){
            if(mp[target - nums[i]]){
                return vector<int>{pos[target - nums[i]], i};
            }else {
                mp[nums[i]] = 1;
                pos[nums[i]] = i;
            }
        }
        return vector<int>{-1, -1};
    }
};

2 中等

Soluion

  • 按位模拟加法
  • 链表操作

Sample Code

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode * res = 0; int c = 0;
        ListNode * p = 0;
        while(l1 || l2){
            int l, r, sum;
            if(l1) {
                l = l1->val;
                l1 = l1->next;
            }else l = 0;
            if(l2) {
                r = l2->val;
                l2 = l2->next;
            }else r = 0;
            sum = l + r + c;
            c = sum / 10;
            ListNode * newNode = new ListNode(sum % 10);
            if(p) {
                p->next = newNode;
                p = newNode;
            }
            else p = res = newNode;
        }

3 中等

Soluion

  • 用map维护一个不出现重复字符的字符串左右边界l,r
  • s[l]...s[r]无重复字符
  • 更新res

Sample Code (map)

class Solution {
public:
    map<int, int> mp;
    int lengthOfLongestSubstring(string s) {
        if(s == "") return 0;
        int res = 1; 
        int l = 1; int r = 1; mp[s[0]] = 1;
        for(int i = 1; s[i]; ++i){
            if(mp[s[i]] >= l && mp[s[i]] <= r){
                l = mp[s[i]] + 1;
            }
            r = i + 1;
            mp[s[i]] = i + 1;
            res = max(res, r - l + 1);
            //cout << res << endl;
        }
        return res;
    }
};

4 困难

Soluion

  • 二分答案(可参考两等长数组二分答案查找中位数的题解)
  • 考虑数组长度情况确定l,r

Sample Code (map)

class Solution {
public:
    double find(vector<int> & arr){
        if(arr.size() == 0) return 0;
        int len = arr.size();
        if(len & 1) return arr[len / 2];
        return (arr[len / 2] + arr[len / 2 - 1]) / 2.0;
    }
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int len1 = nums1.size(); int len2 = nums2.size();
        if(len1 == 0) return find(nums2);
        if(len2 == 0) return find(nums1);
        int pos = (len1 + len2) / 2 + 1;
        int l; int r = min(pos, len1);
        l = max(pos - len2, 0);
        int pos1, pos2;
        while(l <= r){
            int mid = (l + r) >> 1;
            int lpos = mid; int rpos = pos - mid;
            if(lpos == 0 || rpos == 0){
                if(lpos == 0){
                    if(nums1[lpos] < nums2[rpos - 1]) l = mid + 1;
                    else{
                        pos1 = -1;
                        pos2 = rpos - 1;
                        break;
                    }
                }else{
                    if(nums1[lpos - 1] > nums2[rpos]) r = mid - 1;
                    else{
                        pos1 = lpos - 1;
                        pos2 = -1;
                        break;
                    }
                }
                // break;
            }else{
                if(lpos < len1 && nums1[lpos] < nums2[rpos - 1]) l = mid + 1;
                else{
                    if(rpos < len2 && nums1[lpos - 1] > nums2[rpos]) r = mid - 1;
                    else {
                        pos1 = lpos - 1;
                        pos2 = rpos - 1;
                        break;
                    }
                }
            }
        }
       // cout << pos1 << ‘ ‘ << pos2 << endl;
        double fi, se;
        if(pos1 == -1){
            fi = nums2[pos2];
            se = nums2[pos2 - 1];
        }
        else if(pos2 == -1){
            fi = nums1[pos1];
            se = nums1[pos1 - 1];
        }
        else if(nums1[pos1] > nums2[pos2]){
            fi = nums1[pos1]; --pos1;
            if(pos1 < 0) se = nums2[pos2];
            else se = max(nums1[pos1], nums2[pos2]);
        }else{
            fi = nums2[pos2]; --pos2;
            if(pos2 < 0) se = nums1[pos1];
            else se = max(nums1[pos1], nums2[pos2]);
        }
        if(len1 % 2 == len2 % 2) return (fi + se) / 2;
        return fi;
    }
};

5 中等

Soluion

  • dp
  • 维护l, r, len

Sample Code (map)

class Solution {
public:
    bool dp[1010][1010];
    string longestPalindrome(string s) {
        int maxStart = 0;
        int maxEnd = 1;
        int maxLen = 1;
        for(int i = 1; s[i]; ++i)
            for(int j = 0; j < i; ++j)
                if(s[i] == s[j]){
                    if(i - j <= 2 || dp[i - 1][j + 1]){
                        dp[i][j] = true;
                        if(i - j + 1 > maxLen){
                            maxLen = i - j + 1;
                            maxStart = j;
                            maxEnd = i + 1;
                        }
                    }
                }
        return s.substr(maxStart, maxLen);
    }
};

LeetCode 1-5题解

上一篇:零基础入门Python数据分析,只需要看懂这一张图,附下载链接!


下一篇:爬虫绕过cloudflare验证