从零开始的刷LeetCode生活 第45期 473-485

在这里插入图片描述

class Solution {
public:
    int side;
    int sum = 0;
    vector<bool>st;
    bool makesquare(vector<int>& nums) {
        if(!nums.size()) return false;
        for(auto x : nums) sum += x;
        if(sum % 4) return false;
        side = sum / 4;
        sort(nums.begin(), nums.end());
        st = vector<bool>(nums.size(), false);
        return dfs(nums, 0, side, nums.size() - 1); //从大到小枚举
    }

    bool dfs(vector<int>&nums, int count, int side_dfs, int start)
    {
        if(!side_dfs)
        {
            if(++count == 4) return true;
            return dfs(nums, count, side, nums.size() - 1); //枚举另外一条边
        }
        for(int i = start; ~i; i --)
        {
            if(!st[i] && side_dfs >= nums[i])
            {
                //剪枝,前一个与现在一样,前一个失败则这个也失败
                if(i + 1 < nums.size() && !st[i + 1] && nums[i + 1] == nums[i]) continue;
                st[i] = true;
                if(dfs(nums, count, side_dfs - nums[i], i - 1)) return true;
                st[i] = false; 
            }
        }
        return false;
    }
};

在这里插入图片描述

class Solution {
public:
    int findMaxForm(vector<string>& strs, int m, int n) {
        if(strs.size() == 0) return 0;
        vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
        for(int i = 0; i < strs.size(); i ++)
        {
            int zeros = 0, ones = 0;
            for(auto j : strs[i])
            {
                if(j == '0') zeros ++;
                else ones ++;
            }
            for(int i = m; i >= zeros; i --)
                for(int j = n; j >= ones; j --)
                {
                    dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1);
                }
        }
        return dp[m][n];
    }
};

在这里插入图片描述

class Solution {
public:
    int findRadius(vector<int>& houses, vector<int>& heaters) {
        heaters.push_back(INT_MIN), heaters.push_back(INT_MAX);
        sort(heaters.begin(), heaters.end());
        int res = 0;
        for(auto &x : houses)
        {
            int l = 0, r = heaters.size() - 1;
            while(l < r)
            {
                int mid = l + r >> 1;
                if(heaters[mid] >= x) r = mid;
                else l = mid + 1;
            }
            res = max(res, (int)min(x + 0ll - heaters[r - 1], heaters[r] + 0ll - x));
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int findComplement(int num) {
        int tmp = num, c = 0;
        while(tmp > 0)
        {
            tmp >>= 1; //计算num有多少位
            c = (c << 1) + 1;
        }
        return num ^ c;
    }
};

在这里插入图片描述

class Solution {
public:
    int totalHammingDistance(vector<int>& nums) {
        int res = 0;
        for(int i = 0; i <= 30; i ++)
        {
            int ones = 0;
            for(auto x : nums)
                if(x >> i & 1)
                    ones ++;
            res += ones * (nums.size() - ones);
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    double rad, xc, yc;
    Solution(double radius, double x_center, double y_center) {
        rad = radius, xc = x_center, yc = y_center;
    }
    
    vector<double> randPoint() {
        double x0 = xc - rad;
        double y0 = yc - rad;
        while(1)
        {
            double xg = x0 + (double)rand() / RAND_MAX * rad * 2;
            double yg = y0 + (double)rand() / RAND_MAX * rad * 2;
            if(sqrt(pow((xg - xc), 2) + pow((yg - yc), 2)) <= rad)
                return {xg, yg};
        }
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(radius, x_center, y_center);
 * vector<double> param_1 = obj->randPoint();
 */

在这里插入图片描述

class Solution {
public:
   int largestPalindrome(int n) {
       int upper_bound = pow(10, n) - 1; //上边界
       int lower_bound = pow(10, n - 1);//下边界;第一个大于或等于num的数字
       for(int i = upper_bound; i >= lower_bound; -- i)
       {
           string tmp = to_string(i);
           long res = stol(tmp + string(tmp.rbegin(), tmp.rend()));
           for(long j = upper_bound; j * j >= res; --j)
               if(res % j == 0)
                   return res % 1337;
       }
       return 9;
   }
};

在这里插入图片描述

class Solution {
public:
    vector<double> medianSlidingWindow(vector<int>& nums, int k) {
        vector<double>res;
        multiset<int>window(nums.begin(), nums.begin() + k);

        auto mid = next(window.begin(), k / 2); //迭代器的下一个或上一个用next(),prev()
        //next 开头 下多少个
        for(int i = k; ; i ++)
        {
            res.push_back((*mid + 0ll + *next(mid, k % 2 - 1)) * 0.5);
            if(i == nums.size())
                return res;
            window.insert(nums[i]);
            if(nums[i] < *mid) -- mid;
            if(nums[i - k] <= *mid) ++mid;
            window.erase(window.lower_bound(nums[i - k]));
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int magicalString(int n) {
        string s = "122";
        for(int i = 2, k = 1; i < n; i++, k = 3 - k) //从第三位2开始 两个1
        {
            for(int j = 0; j < s[i] - '0'; j ++) s += to_string(k);
        }
        int res = 0;
        for(int i = 0; i < n; i ++)
            res += s[i] == '1';
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    char upper(char c)
    {
        if(c >= 'a' && c <= 'z')
            return c - 'a' + 'A';
        return c;
    }
    string licenseKeyFormatting(string S, int K) {
        string res;
        int cnt = 0;
        for(int i = S.size() - 1; ~i; i--)
        {
            if(S[i] == '-') continue;
            res += upper(S[i]);
            if(++cnt % K == 0) res += '-';
        }
        while(!res.empty() && res.back() == '-')
            res.pop_back();
        reverse(res.begin(), res.end());
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    string smallestGoodBase(string N) {
        long long n = stol(N);
        long long res = n - 1;
        for(int s = 2; s <= 59; ++s)
        {
            int k = pow(n, 1.0 / s);
            if(k > 1)
            {
                long long sum = 1, mul = 1;
                for(int i = 1; i <= s; ++i)
                {
                    mul *= k;
                    sum += mul;
                }
                if(sum == n)
                {
                    res = k;
                    break;
                }
            }
        }
        return to_string(res);
    }
};

在这里插入图片描述

class Solution {
public:
    int findMaxConsecutiveOnes(vector<int>& nums) {
        int res = 0;
        int tmp = 0;
        for(int i = 0; i < nums.size(); i ++)
        {            
            if(nums[i] == 1) res = max(res, ++tmp);
            else tmp = 0;
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值