Leetcode242.有效的字母异位词

题解

class Solution {
public:
    bool isAnagram(string s, string t) {
        unordered_map<char,int> s1;
        for(int i=0;i<s.length();i++){
            if(s1.count(s[i])==0){
                s1.insert({s[i],1});
            }else if(s1.count(s[i])==1){
                s1[s[i]]=s1[s[i]]+1;
            }
        }
        unordered_map<char,int> s2;
        for(int i=0;i<t.length();i++){
            if(s2.count(t[i])==0){
                s2.insert({t[i],1});
            }else if(s2.count(t[i])==1){
                s2[t[i]]=s2[t[i]]+1;
            }
        }
        if(s1.size()!=s2.size()){
            return false;
        }else{
        for(unordered_map<char,int>::iterator it=s1.begin();it!=s1.end();it++){
            if(s2.count(it->first)==0){
                return false;
            }else{
                if(it->second!=s2.at(it->first)){
                    return false;
                }
            }
        //cout<<it->first<<it->second<<endl;
    }
    return true;
        }
    }
};

笔记

1.求字符串的长度函数

string

string s;
cin>>s;
cout<<s.length()<<endl;//使用string()的成员方法length()获取字符串长度
cout<<s.size()<<endl;//使用string()的成员方法size()获取字符串长度

char

char s[100];
cin>>s;
cout<<strlen(s)<<endl;//用strlen函数,strlen()函数求出的字符串长度为有效长度,即不包含字符串末尾结束符 ‘\0’;

2.unordered_map

         unordered_map容器,即无序map容器,所谓“无序”,指的是unordered_map容器不会像map容器那样对存储的数据进行排序,该容器以键值对(pair类型)的形式存储数据,存储的各个键值对的键互不相同且不允许被修改。

构造函数与初始化

//1.模板类的默认构造函数,创建空的unordered_map
unordered_map<int, string> umap;

//2.使用初始化列表初始化
unordered_map<int, string> umap2({{3,"c"},{4,"d"}});	//隐式调用构造函数

//3.拷贝构造函数初始化
unordered_map<int, string> umap4(umap3);

//4.迭代器初始化
unordered_map<int, string> umap5(umap1.begin(), umap1.end());

当前容量

unordered_map<int, string> umap;
cout << "umap.size is" << umap.size() << endl;

迭代器

unordered_map<Key,T>::iterator it;
(*it).first;  //返回key值
(*it).second;//返回value值
it->first;//返回key值
it->second;//返回value值

//遍历输出 umap 容器中所有的键值对
for (auto iter = umap.begin(); iter != umap.end(); ++iter) {
        cout << "<" << iter->first << ", " << iter->second << ">" << endl;
    }

访问元素

umap.at("Mars") = 3396;//at函数
first["GOOG"] = "Google";//operator[],new element inserted

查找元素

unordered_map<std::string,double>::const_iterator got = umap.find(person);//查找key所在的元素。找到返回元素的迭代器。通过迭代器的first和second属性获取值。没找到返回unordered_map::end

size_type count ( const key_type& k ) const;//成员函数count判断集合中有没有键值k,如果无序map中有key k,那么返回值为1,否则返回值为0。

修改元素

map_name [key_name] = value//使用操作符,实现对应key的value值的覆盖,若是key值是原来不存在的key,那么实现了新的元素的插入。

插入元素

myrecipe.insert(mypantry.begin(), mypantry.end());	// range inseration

myrecipe.insert({{"sugar",0.8},{"salt",0.1},{"sugar",0.9}});// initializer list inseration

删除元素

umap.erase(umap.begin());// 根据位置,删除第一个:

umap.erase("France");// 根据key

umap.erase(umap.find("Germany"), umap.end());// range

umap.clear();//清空元素

遍历unordered_map

//1.值传递遍历
for(pair<int,int> kv:map){
        cout<<kv.first<<kv.second<<endl;
    }
//或使用auto
for(auto kv:map){
        cout<<kv.first<<kv.second<<endl;
    }

//2.引用传递遍历
for(const pair<int,int>& kv:map){
        cout<<kv.first<<kv.second<<endl;
    }

//或
for(pair<const int,int>& kv:map){
        cout<<kv.first<<kv.second<<endl;
    }
//或
for(auto& kv:map){
        cout<<kv.first<<kv.second<<endl;
    }

//3.使用迭代器遍历
for(unordered_map<int,int>::iterator it=map.begin();it!=map.end();it++){
        cout<<it->first<<it->second<<endl;
    }
//或
for(auto it=map.begin();it!=map.end();it++){
        cout<<it->first<<it->second<<endl;
    }

//4.结构化绑定
//值传递
for(auto [k,v]:map){
        cout<<k<<v<<endl;
    }
//或引用传递
for(auto& [k,v]:map){
        cout<<k<<v<<endl;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值