头像

贪心的小马




离线:2小时前


活动打卡代码 AcWing 792. 高精度减法

#include<iostream>
#include<string>
#include<vector>
using namespace std;

bool emp(vector<int> &A,vector<int> &B){
    if(A.size() != B.size())return A.size() > B.size();

    for(int i = A.size()-1;i >= 0;i--){
        if(A[i] !=B[i])
        return A[i] >B[i];
    }

    return true;
}

vector<int> sub(vector<int> &A,vector<int> &B){
    vector<int> C;
    for(int i = 0,t = 0;i<A.size();i++){
        t =A[i] -t;
        if(i < B.size())t -=B[i];
        C.push_back((t + 10)%10);
        if(t<0)t =1;
        else t =0;
    }
    while(C.size()>1&&C.back()==0)C.pop_back();
    return C;
}

int main(){
    string a,b;
    vector<int> A,B;
    cin >> a >> b;
    for(int i =a.size()-1;i>=0;i--)A.push_back(a[i]-'0');
    for(int i =b.size()-1;i>=0;i--)B.push_back(b[i]-'0');

    vector<int> C;
    if(emp)C = sub(A,B);
    else {
        C=sub(B,A);
        cout<<"-";
    }
    for(int i=C.size()-1;i>=0;i--)cout<<C[i];
    return 0;
}


活动打卡代码 AcWing 791. 高精度加法

#include<iostream>
#include<vector>
using namespace std;

vector<int> add(vector<int> &A,vector<int> &B){
    if(A.size()<B.size())return add(B,A);
    vector<int> C;
    int t =0;
    for(int i =0;i < A.size();i ++ ){
        t +=A[i];
        if(i < B.size())t += B[i];
        C.push_back(t%10);
        t /= 10;
    }
    if(t)C.push_back(t);
    return C;
}

int main(){
    string a,b;
    vector<int> A,B;
    cin >> a >> b;
    for(int i =a.size()-1;i>=0;i--)A.push_back(a[i] - '0');
    for(int i =b.size()-1;i>=0;i--)B.push_back(b[i] - '0');

    auto C = add(A,B);

    for(int i =C.size()-1;i>=0;i--)cout << C[i];
    cout<<endl;
    return 0;
}


活动打卡代码 AcWing 84. 求1+2+…+n

class Solution {
public:
    int getSum(int n) {
        int res=n;
        n>0 && (res =res + getSum(n-1));
        return res;
    }
};



class Solution {
public:
    int maxDiff(vector<int>& nums) {
    if(nums.empty())return 0;
     int minv = nums[0];
     int res = 0;
     for(int i = 1;i<nums.size();i++){
         res =max(res,nums[i]-minv);
         minv =min(minv,nums[i]);
     }
     return res;
    }
};



class Solution {
public:
    int lastRemaining(int n, int m){
        if(n==1)return 0;
        return (lastRemaining(n-1,m)+m)%n;
    }
};


活动打卡代码 AcWing 81. 扑克牌的顺子

class Solution {
public:
    bool isContinuous( vector<int> numbers ) {
        if(numbers.empty())return false;
        sort(numbers.begin(),numbers.end());
        int k=0;
        while(!numbers[k]){
            k++;
        }
        for(int i = k+1;i<numbers.size();i++){
            if(numbers[i]==numbers[i-1])return false;
        }
        return numbers.back() - numbers[k]<=4;
    }
};


活动打卡代码 AcWing 80. 骰子的点数

class Solution {
public:
    vector<int> numberOfDice(int n) {
        vector<int> ans;
        for(int i=0;i<=5*n;i++)ans.push_back(dfs(n,i));
        return ans;
    }

    int dfs(int n ,int sum){
        if(sum<0)return 0;
        if(n==0)return !sum;
        int res=0;
        for(int i =0;i<=5;i++)res+=dfs(n-1,sum-i);
        return res;
    }
};




class Solution {
public:
    vector<int> maxInWindows(vector<int>& nums, int k) {
        vector<int> res;
        deque<int> q;
        for(int i =0;i<nums.size();i++){
            while(q.size()&&q.front()<=i-k)q.pop_front();
            while(q.size()&&nums[i]>=nums[q.back()])q.pop_back();
            q.push_back(i);
            if(i>=k-1)res.push_back(nums[q.front()]);
        }
        return res;
    }
};



class Solution {
public:
    vector<int> findNumbersWithSum(vector<int>& nums, int target) {
        bool t =true;
        vector<int> ans;
        for(int i = 0;i<nums.size()&&t;i++){
            int a =target-nums[i];
            for(int j =i+1;j<nums.size();j++){
                if(!(a - nums[j])){
                    ans.push_back(target-a);
                    ans.push_back(a);
                    t=false;
                    break;
                }
            }

        }
        return ans;
    }
};



class Solution {
public:
vector<int> ans;
unordered_map<int,int> res; 
    vector<int> findNumsAppearOnce(vector<int>& nums) {
        for(auto x:nums){
            res[x]++;
        }
        int t =2;
        for(auto x:nums){
            if(res[x]==1){
                ans.push_back(x);
                t--;
                if(t==0)break;
            }
        }
        return ans;
    }
};