算法
byte_1998
这个作者很懒,什么都没留下…
展开
-
c++中map常用的操作
C++中map的常用操作如下: int main() { map<char, int> cnt; string str = "abcdef"; // map 中添加元素 for(auto c : str) cnt[c] ++; // 遍历map for(auto c : cnt) cout << c.first << " " << c.second << endl; cout <<原创 2021-11-14 13:32:44 · 938 阅读 · 0 评论 -
两个数组的交集
题目:给定两个数组,编写一个函数来计算它们的交集。 实现:使用双指针进行遍历 实现代码:C++ class Solution { public: vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { int n1 = nums1.size(), n2 = nums2.size(); sort(nums1.begin(), nums1.end()原创 2021-10-11 18:59:13 · 66 阅读 · 0 评论 -
字符串中的第一个唯一字符
题目:给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 解题:先使用哈希表去存储字符串中的每一个字符,接下来遍历字符串,找到第一个出现一次的字符 实现代码:C++ class Solution { public: int firstUniqChar(string s) { unordered_map<char, int> cnt; for(auto c : s) cnt[c] ++; for(int i原创 2021-10-11 18:56:11 · 116 阅读 · 0 评论