头像

古娜拉黑暗之神呜呼啦呼变身

郑州大学




离线:1天前


最近来访(8)
用户头像
D.devil
用户头像
zombotany
用户头像
无名氏DU
用户头像
INnoVation
用户头像
未命名_5

活动打卡代码 AcWing 3554. 二进制

#include<iostream>
#include<algorithm>
#include<cstring>
#include <climits>
using namespace std;
typedef long long LL;
int main(){
    int T;
    scanf("%d",&T);

    string str;
    for(int i = 0; i < T; i ++){
        cin >> str;
        //cout<<str<<"!!!"<<endl;
//cout<<str<<endl;
        LL res = 0;
        for(int j = 0; j <= 31; j ++){
            res = res<<1;
            res += (str[j] - '0');
        }
        //cout<<str[31] - '0'<<endl;
        //cout<<res<<endl;
        string ans;
        LL m=res+1;
        for(int i = 31; i >= 0; i --){
            if(m>>i&1){
                ans += '1';
            }
            else{
                ans += '0';
            }
        }
        m >>= 32;
        while (m > 0){
            if(m>>0&1){
                ans='1' + ans;
            }
            else{
                ans  ='0'+ans;
            }
            m>>=1;
        }
        cout<<ans<<endl;
        m=res+3;
        //cout<<m<<endl;

        //cout<<m<<endl;
        ans="";
        for(int i = 31; i >= 0; i --){
            if(m>>i&1){
                ans += '1';
            }
            else{
                ans += '0';
            }
        }
        m >>= 32;
        ///cout<<m<<endl;
        while (m > 0){
            if(m>>0&1){
                ans='1' + ans;
            }
            else{
                ans  ='0'+ans;
            }
            m>>=1;
        }
        cout<<ans<<endl;
    }
    return 0;
}




class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string,vector<string>> hash;
        for(auto& str: strs){
            string nstr = str;
            sort(nstr.begin(), nstr.end());
            hash[nstr].push_back(str);
        }
        vector<vector<string>> res;
        for(auto& item : hash){
            res.push_back(item.second);
        }
        return res;
    }
};


活动打卡代码 LeetCode 48. 旋转图像

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < i; j++){
                swap(matrix[i][j],matrix[j][i]);
            }
        }
        for(int i = 0; i < n; i++){
            for(int j = 0, k = n-1; j < k; j ++,k --){
                swap(matrix[i][j], matrix[i][k]);
            }
        }
    }
};


活动打卡代码 LeetCode 47. 全排列 II

class Solution {
public:
    vector<vector<int>> ans;
    vector<bool> st;
    vector<int> path;
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        st=vector<bool>(nums.size());
        path=vector<int>(nums.size());
        dfs(nums, 0);
        return ans;
    }
    void dfs(vector<int> nums, int u){
        if(u == nums.size()){
            ans.push_back(path);
        }
        for(int i = 0; i < nums.size(); i ++){
            if(!st[i]){
                if(i && nums[i-1] == nums[i] && !st[i-1])continue;
                st[i]=true;
                path[u] = nums[i];
                dfs(nums, u+1);
                st[i] = false;
            }

        }
    }
};


活动打卡代码 LeetCode 46. 全排列

class Solution {
public:
    vector<vector<int>> ans;
    vector<int> path;
    vector<bool> st;
    vector<vector<int>> permute(vector<int>& nums) {
        path = vector<int>(nums.size());
        st = vector<bool>(nums.size());
        dfs(nums, 0);
        return ans;
    }
    void dfs(vector<int> nums,int u){
        if(u == nums.size())ans.push_back(path);
        for(int i= 0; i < nums.size(); i ++){
            if(!st[i]){
                path[u] = nums[i];
                st[i] = true;
                dfs(nums, u+1);
                st[i] = false;
            }
        }
    }
};


活动打卡代码 LeetCode 43. 字符串相乘

像小学一样列竖式

class Solution {
public:
    string multiply(string num1, string num2) {
        vector<int> A, B;
        int n = num1.size(), m = num2.size();
        for(int i = n - 1; i >= 0; i --) A.push_back(num1[i] - '0');
        for(int i = m - 1; i >= 0; i --) B.push_back(num2[i] - '0');


        vector<int> C(n + m);
        for(int i = 0; i < n; i ++ ){
            for(int j = 0; j < m; j ++ ){
                C[i + j] += A[i] * B[j];
            }
       }

        int t = 0;

        for (int i = 0, t = 0; i < C.size(); i ++ ) {
            t += C[i];
            C[i] = t % 10;
            t /= 10;
        }

        int k = C.size() - 1;
        while (k >0 && !C[k]) k --;
        string res;
        while (k >=0 ) res += C[k --] + '0';
        return res;
    }
};


活动打卡代码 LeetCode 42. 接雨水

class Solution {
public:
    int trap(vector<int>& height) {
        stack<int>stk;
        int res=0;
        int h = 0;
        int ans = 0;
        for(int i = 0; i < height.size(); i ++){
            if(h>height[i])res+=(h-height[i]);
            else h=height[i];
            ans += height[i];
        }
        //cout<<res<<endl;
        h=0;
        for(int i = height.size()-1; i >= 0; i --){
            if(h>height[i])res+=(h-height[i]);
            else h=height[i];
        }
        //cout<<res<<endl;
        //cout<<h<<endl;
        return res + ans - h*height.size();
    }
};



第一种,但是不是常数级别

class Solution {
public:
    int firstMissingPositive(vector<int>& nums) {
        unordered_set<int>hash;
        for(auto x : nums)hash.insert(x);
        int res=1;
        while(hash.count(res))res++;
        return res;
    }
};


活动打卡代码 AcWing 3333. K-优字符串

#include<iostream>
#include<algorithm>
#include<cstring>

int T,n,k;
const int N=200010;

char a[N];
using namespace std;
int main(){
    scanf("%d",&T);
    for(int i = 0; i < T; i ++){
        scanf("%d %d",&n,&k);
        //cout<<n<<" "<<k<<endl;
        for(int j=0;j<n;j++){
            scanf(" %c",&a[j]);
            //cout<<a[j]<<endl;
        }
        //scanf("%s",a);
        //cout<<endl;
        int cnt=0;
        for(int m =0,j = n-1; m < j; m ++,j--){
            if(a[m]!=a[j])cnt++;

        }

        //cout<<cnt<<endl;
        printf("Case #%d: %d\n",i+1,abs(k-cnt));


    }
    return 0;
}


活动打卡代码 LeetCode 31. 下一个排列

class Solution {
public:

    void nextPermutation(vector<int>& nums) {
        int k=nums.size()-1;
        while(k>0 &&nums[k-1]>=nums[k])k--;
        if(k<=0){
            reverse(nums.begin(),nums.end());
        }
        else{
            int t=k;
            while(t<nums.size()&&nums[t]>nums[k-1])t++;
            swap(nums[k-1],nums[t-1]);
            reverse(nums.begin()+k,nums.end());
        }
    }
};