欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > 【day01】数字统计 | 两个数的交集 | 点击消除

【day01】数字统计 | 两个数的交集 | 点击消除

2025/9/24 4:31:20 来源:https://blog.csdn.net/2301_79582015/article/details/147677482  浏览:    关键词:【day01】数字统计 | 两个数的交集 | 点击消除

1.数字统计(模拟 + 数学)

题目链接:[NOIP2010]数字统计_牛客题霸_牛客网

算法思路:

算法思路: 常规操作: 循环提取末尾,然后⼲掉末尾~

#include <bits/stdc++.h>using namespace std;int main()
{int l , r;cin >> l >> r;int res = 0;for(int i = l;i <= r;i ++){int tmp = i;while(tmp){if(tmp % 10 == 2) res ++;tmp /= 10;}}cout << res << endl;return 0;
}

2.两个数组的交集

题目链接:两个数组的交集_牛客题霸_牛客网

解题思路:

a. 将其中⼀个数组丢进哈希表中;

b. 遍历另⼀个数组的时候,在哈希表中看看就好了。

#include <unordered_map>
class Solution 
{bool hash[1024] = { 0 };
public:vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {vector<int>ret;for(auto x : nums1) hash[x] = true;for(auto x : nums2){if(hash[x]){ret.push_back(x);hash[x] = false;}}return ret;}
};

3.点击消除

题目链接:点击消除_牛客题霸_牛客网

解题思路:使用栈来模拟消除

#include <bits/stdc++.h>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 += ch;}cout << (st.size() == 0 ? "0" : st) << endl;return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com