从零开始的刷LeetCode生活 第38期 391-400

在这里插入图片描述

class Solution {
public:
    bool isRectangleCover(vector<vector<int>>& rectangles) {
        int x1 = INT_MAX;
        int y1 = INT_MAX;
        int x2 = INT_MIN;
        int y2 = INT_MIN;
        int area = 0;
        set<pair<int, int>>points;

        for(auto r : rectangles)
        {
            x1 = min(x1, r[0]);
            y1 = min(y1, r[1]);
            x2 = max(x2, r[2]);
            y2 = max(y2, r[3]);

            area += abs(r[2] - r[0]) * abs(r[3] - r[1]);
            pair<int, int>p1 = make_pair(r[0], r[1]);
            pair<int, int>p2 = make_pair(r[0], r[3]);
            pair<int, int>p3 = make_pair(r[2], r[1]);
            pair<int, int>p4 = make_pair(r[2], r[3]);

            if(!points.count(p1))
                points.insert(p1);
            else
                points.erase(p1);
            if(!points.count(p2))
                points.insert(p2);
            else
                points.erase(p2);
            if(!points.count(p3))
                points.insert(p3);
            else
                points.erase(p3);
            if(!points.count(p4))
                points.insert(p4);
            else
                points.erase(p4);
        }
        if(area != abs(x2 - x1) * abs(y2 - y1))
            return false;
        if(points.count(make_pair(x1, y1)) &&
            points.count(make_pair(x1, y2)) &&
            points.count(make_pair(x2, y1)) &&
            points.count(make_pair(x2, y2)) &&
            points.size() == 4)
            return true;
        else 
            return false;
    }
};

在这里插入图片描述

class Solution {
public:
    bool isSubsequence(string s, string t) {
        int k = 0;
        for(int i = 0; i < t.size() && k < s.size(); i ++)
            if(t[i] == s[k])
                k ++;
        return k == s.size();
    }
};

在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    bool validUtf8(vector<int>& data) {
        const int ONE = 1 << 7; //1000 0000
        const int TWO = 3 << 6; //1100 0000
        int k = 0;
        for(auto n : data)
        {
            if(k == 0)
            {
                if((n & TWO) == ONE)
                    return false;
                if(n & ONE)
                {
                    k = oneCount(n);
                    if(k > 4)
                        return false;
                    --k;
                }
            }else
            {
                if((n & TWO) != ONE)
                    return false;
                --k;
            }
        }
        return k == 0;
    }
    int oneCount(int n)
    {
        for(int i = 7; i >= 0; i --)
            if((n & (1 << i)) == 0)
                return 7 - i;
        return 8;
    }
};

在这里插入图片描述

class Solution {
public:
    string decodeString(string s) {
        int n = s.size();
        int num = 0;
        stack<int> numStack;
        stack<string> strStack;
        string cur = "";
        string res = "";
        for(int i = 0; i < n; i ++)
        {
            if(s[i] >= '0' && s[i] <= '9')
                num = 10 * num + s[i] - '0';
            else if(s[i] == '[')
            {
                numStack.push(num);
                strStack.push(cur);
                num = 0;
                cur.clear();
            }
            else if(s[i] >= 'a' && s[i] <= 'z' || (s[i] >= 'A' && s[i] <= 'Z'))
                cur += s[i];
            else if(s[i] == ']')
            {
                int k = numStack.top();
                numStack.pop();
                for(int j = 0; j < k; j ++)
                    strStack.top() += cur;
                cur = strStack.top();
                strStack.pop();
            }
        }
        res = cur;
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int longestSubstring(string s, int k) {
        int n = s.size();
        if(n == 0 || k > n) return 0;
        if(k < 2) return n;
        return dfs(s, k, 0, n - 1);
    }
    int dfs(string s, int k, int left, int right)
    {
        if(right - left + 1 < k) return 0;
        int a[26] = {0};
        for(int i = left; i <= right; i ++)
            a[s[i] - 'a'] ++;
        while(right - left + 1 >= k && a[s[left] - 'a'] < k)
            left ++;
        while(right - left + 1 >= k && a[s[right] - 'a'] < k)
            right --;
        if(right - left  + 1 < k) return 0;
        for(int i = left; i <= right; i ++)
            if(a[s[i] - 'a'] < k)
                return max(dfs(s, k, left, i - 1), dfs(s, k, i + 1, right));
        return right - left + 1;
    }
};

在这里插入图片描述

class Solution {
public:
    int maxRotateFunction(vector<int>& A) {
        long n = A.size(), sum = 0, f = 0;
        for(int i = 0; i < n; i ++)
        {
            sum += A[i];
            f += i * A[i];
        }
        long res = f;
        for(int i = n - 1; i >= 0; i --)
        {
            //F[k + 1] = f[k] + sum - n * Bk[n - 1]
            f += sum - n * A[i];
            res = max(res, f);
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int integerReplacement(int n) {
        return (int)dfs((long)n);
    }
    long dfs(long n)
    {
        if(n == 1) return 0;
        if(n % 2 == 0)
            return 1 + dfs(n / 2);
        else 
            return 1 + min(dfs(n + 1), dfs(n - 1));
    }
};

在这里插入图片描述

class Solution {
public:
    vector<int> res;
    Solution(vector<int>& nums) {
        res = nums;
    }
    
    int pick(int target) {
        int c = 0, index = 0;
        for(int i = 0; i < res.size(); i ++)
            if(res[i] == target)
            {
                c ++;
                if(rand() % c == 0) index = i;
            }
        return index;
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(nums);
 * int param_1 = obj->pick(target);
 */

在这里插入图片描述

class Solution {
struct Node
{
   double val;
   Node *parent;
   Node():parent(this){}
   Node(double v):val(v), parent(this){}
};

Node *find(Node *node)
{
   while(node != node->parent)
       node = node->parent;
   return node;
}

void union_(Node *a, Node *b, double num, unordered_map<string, Node*>&mp)
{
   Node *f1 = find(a), *f2 = find(b);
   if(f1 == f2) return;
   double ratio = b->val * num / a->val;
   for(auto n : mp)
   {
       if(f1 == find(n.second))
           n.second->val *= ratio;
   }
   f1->parent = f2;
}

public:
   vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
       vector<double>ans;
       unordered_map<string, Node*>mp;
       for(int i = 0; i != equations.size(); i ++)
       {
           if(mp.find(equations[i][0]) == mp.end()) mp[equations[i][0]] = new Node(values[i]);
           if(mp.find(equations[i][1]) == mp.end()) mp[equations[i][1]] = new Node(1.0);
           Node *a = mp[equations[i][0]], *b = mp[equations[i][1]];
           union_(a, b, values[i], mp); 
       }
       for(int i = 0; i != queries.size(); i ++)
       {
           if(mp.find(queries[i][0]) != mp.end() && mp.find(queries[i][1]) != mp.end())
           {
               Node *f1 = find(mp[queries[i][0]]), *f2 = find(mp[queries[i][1]]);
               if(f1 == f2)
                   ans.push_back(mp[queries[i][0]]->val / mp[queries[i][1]]->val);
               else
                   ans.push_back(-1.0);
           } 
           else    
               ans.push_back(-1.0);
       }
       return ans;
   }
};

在这里插入图片描述

class Solution {
public:
    // 1-9 9 10-99 20*9 100-999 300*9 1000-9999 4000*9
    int findNthDigit(int n) {
        int i = 1;
        while(n > i * (pow(10, i - 1)) * 9) //n = 999
        {
            n -= i * pow(10, i - 1) * 9;
            i ++;
        } //n = 999 - 9 - 180 = 810 i = 3
        int num = (n - 1) / i + pow(10, i - 1); // 819/3 = 269 + 100 = 369 mod=2
        string num_str = to_string(num);
        if(n % i == 0) return (num_str[i - 1] - '0');
        return num_str[n % i - 1] -'0';
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值