若有unordered_map<int, int> mp;
查找x是否在map中
方法1: 若存在 mp.find(x)!=mp.end();
方法2: 若存在 mp.count(x)!=0;
unordered_map<key,T>::iterator it;
(*it).first;
(*it).second
for(unordered_map<key,T>::iterator iter=mp.begin();iter!=mp.end();iter++)
cout<<"key value is"<<iter->first<<" the mapped value is "<< iter->second;
for(auto& it : mp){
cout<< it.first <<it.second;
}
Leetcode给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> hash;
for (int i = 0; i < nums.size(); ++i) {
if (hash.find(target - nums[i]) != hash.end()) {
return vector<int>{hash[target - nums[i]], i};
}
hash[nums[i]] = i;
}
return {};
}
leetcode给定两个字符串 s 和 t,判断它们是否是同构的。 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
bool isIsomorphic(string s, string t) {
if (s.size() != t.size()) return false;
unordered_map<char, char> shash;
unordered_map<char, char> thash;
for (int i = 0; s[i] != '\0'; ++i) {
if (shash.find(s[i]) != shash.end() && shash[s[i]] != t[i]) return false;
if (thash.find(t[i]) != thash.end() && thash[t[i]] != s[i]) return false;
shash[s[i]] = t[i];
thash[t[i]] = s[i];
}
return true;
}
Leetcode给定一个整数数组,判断是否存在重复元素。 如果任意一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false 。
bool containsDuplicate(vector<int>& nums) {
unordered_map<int, int> frequence;
for(auto it : nums){
if(++frequence[it] > 1)
return true;
}
return false;
}
leetcode给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
输入: s = "anagram", t = "nagaram"
输出: true
bool isAnagram(string s, string t) {
if (s.size() != t.size()) return false;
unordered_map<int, int> hash;
for (int i = 0; s[i] != '\0'; ++i) {
hash[s[i]]++;
}
for (int i = 0; t[i] != '\0'; ++i) {
hash[t[i]]--;
}
for (auto it : hash) {
if (it.second) return false;
}
return true;
}
leetcode给定一个非空的整数数组,返回其中出现频率前 k 高的元素。
输入: nums = [1,1,1,2,2,3], k = 2
输出: [1,2]
static bool cmp(pair<int, int> v1, pair<int, int> v2){
return v1.second > v2.second;
}
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int,int> hash;
for (int i = 0; i < nums.size(); ++i) {
hash[nums[i]]++;
}
vector<pair<int, int> > arr(hash.begin(), hash.end());
sort(arr.begin(), arr.end(), cmp);
vector<int> res;
for(int i = 0; i < k; ++i){
res.push_back(arr[i].first);
}
return res;
}
leetcode给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
int longestPalindrome(string s) {
unordered_map<char,int> hash;
for (auto it : s) {
hash[it]++;
}
int res = 0;
for (auto it : hash) {
res += it.second % 2 == 0 ? it.second : it.second - 1;
}
return res = res < s.size() ? res + 1 : res ;
}
leetcode给定一个字符串,请将字符串里的字符按照出现的频率降序排列。
pair<char,int>
,再使用STL里面的sort()函数进行排序,sort的好处是可以自定义排序的规则。 static bool cmp(pair<char, int> a, pair<char, int> b) {
return a.second > b.second;
}
string frequencySort(string s) {
unordered_map<char, int>hash;
for (auto it : s) {
hash[it]++;
}
vector<pair<char, int> > arr(hash.begin(), hash.end());
sort(arr.begin(), arr.end(), cmp);
string res;
for (auto it : arr) {
while (it.second--) {
res.push_back(it.first);
}
}
return res;
}
Leetcode 和谐数组是指一个数组里元素的最大值和最小值之间的差别正好是1。
现在,给定一个整数数组,你需要在所有可能的子序列中找到最长的和谐子序列的长度。
输入: [1,3,2,2,5,2,3,7]
输出: 5
原因: 最长的和谐数组是:[3,2,2,2,3].
int findLHS(vector<int>& nums) {
unordered_map<int,int> hash;
int longest = 0;
for (int it : nums) {
hash[it]++;
}
for (auto it : hash) {
if(hash.find(it.first+1) != hash.end()){
longest = max(longest, it.second + hash[it.first+1]);
}
}
return longest;
}
leetcode 给你一个整数数组 arr,请你帮忙统计数组中每个数的出现次数。 如果每个数的出现次数都是独一无二的,就返回 true;否则返回 false。
set.size() == arr.size()
是否相同,相同说明无重复 bool uniqueOccurrences(vector<int>& arr) {
map<int, int> cnts;
for (auto num : arr) {
cnts[num]++;
}
unordered_set<int> set;
for (auto cnt : cnts) {
if (set.find(cnt.second) == set.end()) {
set.insert(cnt.second);
} else {
return false;
}
}
return true;
}
leetcode 设计一个支持在平均 时间复杂度 O(1) 下, 执行以下操作的数据结构。 注意: 允许出现重复元素。 insert(val):向集合中插入元素 val。 remove(val):当 val 存在时,从集合中移除一个 val。 getRandom:从现有集合中随机获取一个元素。每个元素被返回的概率应该与其在集合中的数量呈线性相关。
unordered_map<int, unordered_set<int>> idx;
vector<int> nums;
RandomizedCollection() {
}
bool insert(int val) {
bool res = idx.find(val) == idx.end();
nums.push_back(val);
idx[val].insert(nums.size() - 1);
return res;
}
bool remove(int val) {
if (idx.find(val) == idx.end()) {
return false;
}
int i = *(idx[val].begin()); // 取当前数值的下标
nums[i] = nums.back(); // 与最后一个数替换
idx[val].erase(i); // 更新当前数字的下标数组
idx[nums[i]].erase(nums.size() - 1); // 更新最后一个数的下标数组
if (i < nums.size() - 1) {
idx[nums[i]].insert(i);
}
if (idx[val].size() == 0) { // 删光了
idx.erase(val);
}
nums.pop_back(); // O(1)删除最后一个数字
return true;
}
int getRandom() {
return nums[rand() % nums.size()];
}
leetcode 给定两个数组,编写一个函数来计算它们的交集。
输入:nums1 = [1,2,2,1], nums2 = [2,2]
输出:[2]
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int, int> has;
for (auto num : nums1) {
has[num]++;
}
unordered_set<int> res;
for (auto num : nums2) {
if (has[num] > 0)
res.insert(num);
}
return vector<int> (res.begin(), res.end());
}
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
unordered_set<int> set1;
unordered_set<int> set2;
for (auto num : nums1) {
set1.insert(num);
}
for (auto num : nums2) {
set2.insert(num);
}
for (auto num : set1) {
if (set2.find(num) != set2.end()) {
res.push_back(num);
}
}
return res;
}
leetcode 给你两个数组,arr1 和 arr2, arr2 中的元素各不相同 arr2 中的每个元素都出现在 arr1 中 对 arr1 中的元素进行排序,使 arr1 中项的相对顺序和 arr2 中的相对顺序相同。未在 arr2 中出现过的元素需要按照升序放在 arr1 的末尾。
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
unordered_map<int, int> rank;
for (int i = 0; i < arr2.size(); ++i) {
rank[arr2[i]] = i;
}
sort(arr1.begin(), arr1.end(), [&](int x, int y){
if (rank.find(x) != rank.end()) {
return rank.find(y) != rank.end() ? rank[x] < rank[y] : true;
} else {
return rank.find(y) != rank.end() ? false : x < y;
}
});
return arr1;
}
vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
unordered_map<int, int> cnt;
unordered_set<int> set(arr2.begin(), arr2.end());
sort(arr1.begin(), arr1.end());
vector<int> res1;
for (auto num : arr1) {
cnt[num]++;
if (set.find(num) == set.end()) {
res1.push_back(num);
}
}
vector<int> res2;
for (auto num : arr2) {
while (cnt[num]) {
res2.push_back(num);
cnt[num]--;
}
}
res2.insert(res2.end(), res1.begin(), res1.end());
return res2;
}
leetcode 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。
输入:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]
输出:
2
解释:
两个元组如下:
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
unordered_map<int, int> hash;
for (auto a : A) {
for (auto b : B) {
hash[a + b]++;
}
}
int res = 0;
for (auto c : C) {
for (auto d : D) {
res += hash[-c - d];
}
}
return res;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。