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;
}