Map(映射)和Set(集合)
list:可重复,插入时间复杂度O(1),查询时间复杂度O(n);一般用数组或链表存储。
map:key-value;
set:不重复元素,查询时间为O(1)或O(log(n));一般用哈希表或二叉树存储
HashMap, HashSet, TreeMap, TreeSet
区别:
HashMap/HashSet :查询的时间复杂度为O(1);乱序
TreeMap/TreeSet:查询的时间复杂度为O(log(n));排好序的
https://leetcode-cn.com/problems/valid-anagram/
C++ 三种方法:1.直接用sort函数 2.map计数 3.效率最高
class Solution {
public:
bool isAnagram(string s, string t) {
sort(s.begin(),s.end());
sort(t.begin(),t.end());
return s==t;
}
};
class Solution {
public:
bool isAnagram(string s, string t) {
unordered_map<char,int> map;
if (s.size() != t.size())
ret