1.基本题目
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> maps;
for(int i = 0;i < nums.size(); i ++)
{
int other = target - nums[i];
if(maps.find(other) != maps.end())
return {maps[other],i};
maps[nums[i]] = i;
}
return {};
}
};
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
unordered_map<string, vector<string>> m;
for(auto& x: strs)
{
auto tmp = x;
sort(tmp.begin(), tmp.end());
m[tmp].push_back(x);
}
for(auto& n: m)
{
res.push_back(n.second);
}
return res;
}
};
class Solution {
public:
char firstUniqChar(string s) {
map<char,int> count;
for(auto c: s) count[c] ++;
char res = ' ';
for(auto c: s)
if(count[c] == 1) {res = c; break;}
return res;
}
};
class Solution {
public:
bool uniqueOccurrences(vector<int>& arr) {
unordered_map<int, int> m;
for(auto c : arr) m[c] ++;
unordered_map<int,int> nums(0);
for(auto pair: m) nums[pair.second] ++;
for(auto pair : nums)
if(pair.second > 1)
return false;
return true;
}
};