头像

魔鬼

搬运工


访客:20092

离线:1天前


活动打卡代码 AcWing 433. ISBN号码

魔鬼
1天前
#include <iostream>
using namespace std;
int main(){
    string s;   cin >> s;
    string res = "";
    for(int i = 0; i < s.size(); i ++ ){
        if(s[i] == '-') continue;
        res += s[i];
    }
    int sum = 0;
    for(int i = 0; i < res.size() - 1; i ++ )   sum += (res[i] - '0') * (i + 1);;
    if(sum % 11 == 10){
        if(res[res.size() - 1] != 'X'){
            for(int i = 0; i < s.size() - 1; i ++ ) cout << s[i];
            cout << 'X' << endl;
        }  
        else    cout << "Right" << endl;
    }else{
        if(sum % 11 == res[res.size() - 1] - '0') cout << "Right" << endl;
        else{
            for(int i = 0; i < s.size() - 1; i ++ ) cout << s[i];
            cout << sum % 11 << endl;
        }    
    }
    return 0;
}



魔鬼
1天前
#include <iostream>
using namespace std;
int main(){
    string s;   cin >> s;
    string res = "";
    for(int i = 0; i < s.size(); i ++ ){
        if(s[i] == '-') continue;
        res += s[i];
    }
    int sum = 0;
    for(int i = 0; i < res.size() - 1; i ++ )   sum += (res[i] - '0') * (i + 1);;
    if(sum % 11 == 10){
        if(res[res.size() - 1] != 'X'){
            for(int i = 0; i < s.size() - 1; i ++ ) cout << s[i];
            cout << 'X' << endl;
        }  
        else    cout << "Right" << endl;
    }else{
        if(sum % 11 == res[res.size() - 1] - '0') cout << "Right" << endl;
        else{
            for(int i = 0; i < s.size() - 1; i ++ ) cout << s[i];
            cout << sum % 11 << endl;
        }    
    }
    return 0;
}



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



魔鬼
5天前

deque(双端队列) — 单调队列

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



魔鬼
5天前
class Solution {
public:
    int strToInt(string str) {
        int k = 0;
        while(k < str.size() && str[k] == ' ')   k ++ ;
        if(k == str.size()) return 0;
        int flag = 1;
        if(str[k] == '-'){
            flag = -1;
            k ++ ;
        }else if(str[k] == '+') k ++ ;
        long long res = 0;
        while(k < str.size()){
            if(str[k] >= '0' && str[k] <= '9')  res = res * 10 + (str[k] - '0');
            else    break;
            if(res * flag > INT_MAX)    return INT_MAX;
            else if(res * flag < INT_MIN)   return INT_MIN;
            k ++ ;
        }
        while( ++ k < str.size()){
            if(str[k] >= '0' && str[k] <= '9')  return 0;
        }
        return flag * res;
    }
};


活动打卡代码 AcWing 78. 左旋转字符串

魔鬼
5天前
class Solution {
public:
    string leftRotateString(string str, int n) {
        return str.substr(n) + str.substr(0, n);
    }
};


活动打卡代码 AcWing 77. 翻转单词顺序

魔鬼
5天前
class Solution {
public:
    string reverseWords(string s) {
        vector<string> tmp;
        int i = 0;
        string word = "";
        while(i < s.size()){
            if(s[i] != ' ') word += s[i];
            else{
                tmp.push_back(word);
                word = "";
            }
            i ++ ;
        }
        if(word != "")  tmp.push_back(word);
        string res = "";
        for(int i = tmp.size() - 1; i >= 0; i -- ){
            res += tmp[i];
            if(i)   res += " ";
        }  
        return res;
    }
};



魔鬼
5天前
class Solution {
public:
    vector<int> findNumbersWithSum(vector<int>& nums, int target) {
        unordered_set<int> st;
        for(int i = 0; i < nums.size(); i ++ ){
            if(st.count(target - nums[i]))  return {nums[i], target - nums[i]};
            st.insert(nums[i]);
        }
    }
};



魔鬼
5天前
class Solution {
public:
    vector<int> findNumsAppearOnce(vector<int>& nums) {
        int sum = 0;
        for(int i = 0; i < nums.size(); i ++ )  sum ^= nums[i];
        int k = 0;
        while(!(sum >> k & 1))  k ++ ;
        int x = 0;
        for(int i = 0; i < nums.size(); i ++ ){
            if(nums[i] >> k & 1)    x ^= nums[i];
        }
        return vector<int>{x, sum ^ x};
    }
};


活动打卡代码 AcWing 71. 二叉树的深度

魔鬼
6天前
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int treeDepth(TreeNode* root) {
        if(root == NULL)    return 0;
        return max(treeDepth(root->left), treeDepth(root->right)) + 1;
    }
};