[笔试训练](一)

001

[NOIP2010]数字统计_牛客题霸_牛客网 (nowcoder.com)

 题目:

 题解:

遍历L到R之间的每个数i,将i转化为字符串s,遍历s的每个字符,出现一次'2',次数增加一次。

#include <iostream>
using namespace std;

int main() 
{
    int L=0,R=0,ret=0;
    scanf("%d %d",&L,&R);
    for(int i=L;i<=R;i++)
    {
        string str=to_string(i);
        int n=str.size();
        for(int j=0;j<n;j++)
        {
            if(str[j]=='2')
            {
                ret++;
            }
        }
    }
    printf("%d",ret);
    return 0;
}

002 

两个数组的交集_牛客题霸_牛客网 (nowcoder.com) 

 题目:

 题解:

创建两个哈希表hash1和hash2,分别存储nums1和nums2的元素,遍历hash1中值大于0的键,当在hash2中该键的值同样大于0时,则为公共元素。

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) 
    {
        unordered_map<int, int> hash1;
        unordered_map<int, int> hash2;
        vector<int> res;

        for(auto ch:nums1)
        {
            hash1[ch]++;
        }
        for(auto ch:nums2)
        {
            hash2[ch]++;
        }

        for(auto [a,b]:hash1)
        {
            if(b!=0)
            {
                if(hash2.count(a)>0)
                {
                    res.push_back(a);
                }
            }
        }
        return res;
    }
};

003 

 点击消除_牛客题霸_牛客网 (nowcoder.com)

 

题解:

 创建一个字符串st来模拟栈,遍历原字符串s,同时将字符逐个插入st中,当st.back()和遍历到的原字符串相同时,st.pop_back()掉这个字符,s继续向后遍历。

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

int main() 
{
    string s,st;
    cin>>s;
    for(auto ch:s)
    {
        if(st.size() && st.back()==ch) st.pop_back();
        else st.push_back(ch);
    }
    cout<<(st.size()==0?"0":st)<<endl;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲敲er

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值