2021_01_28 LeetCode刷题回顾

1700. 无法吃午餐的学生数量

简单模拟题。按照题目意思进行模拟即可。

class Solution {
public:
    int countStudents(vector<int>& students, vector<int>& sandwiches)
    {
        int m = students.size();
        int n = sandwiches.size();
        vector<int> eated(m, 1);
        int last = -1;
        for (int i = 0; i < n; i++) {
            bool findFlag = false;
            for (int j = last + 1; j < m; j++) {
                if (eated[j] == 0) {
                    continue;
                }
                if (students[j] == sandwiches[i]) {
                    eated[j] = 0;
                    last = j;
                    findFlag = true;
                    break;
                }
            }
            if (findFlag == true) {
                continue;
            }
            for (auto j = 0; j < last; ++j) {
                if (eated[j] == 0) {
                    continue;
                }
                if (students[j] == sandwiches[i]) {
                    eated[j] = 0;
                    last = j;
                    findFlag = true;
                    break;
                }
            }
            if (findFlag == true) {
                continue;
            }
            break;
        }
        int ans = 0, len1 = eated.size();
        for(int i = 0;i < len1; i++) 
                ans += eated[i];
        return ans;
    }
};

1356. 根据数字二进制下 1 的数目排序

简单模拟题。重写排序规则即可。

class Solution {
public:
    int get(int x){
        int res = 0;
        while (x) {
            res += (x % 2);
            x /= 2;
        }
        return res;
    }
    vector<int> sortByBits(vector<int>& arr) {
        vector<int> bit(10001, 0);
        for (auto x: arr) {
            bit[x] = get(x);
        }
        sort(arr.begin(),arr.end(),[&](int x,int y){
            if (bit[x] < bit[y]) {
                return true;
            }
            if (bit[x] > bit[y]) {
                return false;
            }
            return x < y;
        });
        return arr;
    }
};

1413. 逐步求和得到正数的最小值

简单模拟题。求取出求和过程中的最小值即可,同时进行正数和非负数的区分。

class Solution {
public:
    int minStartValue(vector<int>& nums) {
        int ans = 1, sum = 0, len1 = nums.size();
        for(int i = 0; i < len1; i++) {
            sum += nums[i];
            if(sum < ans)
                ans = sum;
        }
        if(ans >= 1)
            return 1;
        else
            return 1 - ans;
    }
};

1385. 两个数组间的距离值

简单模拟题。按照题目意思模拟即可。

class Solution {
public:
    int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
        int cnt = 0;
        for (auto &x: arr1) {
            bool ok = true;
            for (auto &y: arr2) {
                ok &= (abs(x - y) > d);
            }
            cnt += ok;
        }
        return cnt;
    }
};

1502. 判断能否形成等差数列

简单模拟题。排序两两相减即可。

class Solution {
public:
    bool canMakeArithmeticProgression(vector<int>& arr) {
        sort(arr.begin(), arr.end());
        for (int i = 1; i < arr.size() - 1; ++i) {
            if (arr[i] * 2 != arr[i - 1] + arr[i + 1]) {
                return false;
            }
        }
        return true;
    }
};

1710. 卡车上的最大单元数

简单模拟题。先排序,按照从单元容量从大到小的顺序,之后根据可以选择的数目,每次从大容量的数目减去,直到满足条件即可。

class Solution {
public:
    int maximumUnits(vector<vector<int>>& b, int s) {
        sort(b.begin(),b.end(),[](const auto& L, const auto& R){return L[1] > R[1];});
        int ans = 0, n = 0;
        for (int i = 0; i < b.size() && s > 0; i++) {
            n = min(s, b[i][0]);
            s -= n;
            ans += n * b[i][1];
        }
        return ans;
    }
};

1716. 计算力扣银行的钱

简单模拟题。找规律实现即可。

class Solution {
public:
    int totalMoney(int n) {
        int res = 0;
        for(int i = 1, money = 1; i <= n; i++){
            res += money++;
            if(i % 7 == 0) 
                money = i/7 + 1;
        }
        return res;
    }
};

1475. 商品折扣后的最终价格

简单模拟题。按照题目意思模拟即可。

class Solution {
public:
    vector<int> finalPrices(vector<int>& prices) {
        for (int i = 0; i < prices.size(); ++i)
            for (int j = i + 1; j < prices.size(); ++j)
                if (prices[j] <= prices[i]) {
                    prices[i] = prices[i] - prices[j];
                    break;
                }
        return prices;
    }
};

1636. 按照频率将数组升序排序

简单模拟题。按照题目意思模拟即可。

class Solution {
public:
    vector<int> frequencySort(vector<int>& nums) {
        unordered_map<int, int> cnt;
        int len1 = nums.size();
        for (int i = 0; i < len1; i++)
            cnt[nums[i]] += 1;
        sort(nums.begin(), nums.end(), [&cnt](int a, int b) {
            return (cnt[a] == cnt[b]) ? a > b : cnt[a] < cnt[b];
        });
        return nums;
    }
};

1619. 删除某些元素后的数组均值

简单模拟题。按照题目意思模拟即可。

class Solution {
public:
    double trimMean(vector<int>& arr) {
        int n = arr.size();
        int n5 = n*0.05;
        sort(arr.begin(), arr.end());
        double sum = 0;
        for(int i = n5; i < n- n5; i++)
            sum += arr[i];
        return sum / (n-2*n5);

    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值