当前位置:网站首页>400-哈希表(1. 两数之和、454. 四数相加 II、383. 赎金信)
400-哈希表(1. 两数之和、454. 四数相加 II、383. 赎金信)
2022-06-22 05:35:00 【liufeng2023】
1. 两数之和

class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> mp;
for (int i = 0; i < nums.size(); i++)
{
auto iter = mp.find(target - nums[i]);
if (iter != mp.end())
{
return {
iter->second, i };
}
mp[nums[i]] = i;
}
return {
};
}
};

454. 四数相加 II

class Solution {
public:
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
unordered_map<int, int> mp;
for (int a : nums1)
{
for (int b : nums2)
{
mp[a + b]++;
}
}
int res = 0;
for (int c : nums3)
{
for (int d : nums4)
{
if (mp.find(0 - (c + d)) != mp.end())
{
res += mp[(0 - (c + d))];
}
}
}
return res;
}
};

383. 赎金信

class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int record[26] = {
0 };
if (ransomNote.size() > magazine.size()) return false;
for (int i = 0; i < magazine.size(); i++)
{
record[magazine[i] - 'a']++;
}
for (int i = 0; i < ransomNote.size(); i++)
{
record[ransomNote[i] - 'a']--;
if (record[ransomNote[i] - 'a'] < 0) return false;
}
return true;
}
};

边栏推荐
- The "decentralization" mode of independent stations has risen, sweeping the tide of cross-border enterprise transformation
- tmux -- ssh terminal can be closed without impact the server process
- Global and Chinese silicon carbide barrier Schottky diode market demand and future prospect report 2022-2027
- count registers in C code -- registers has one pattern
- I don't suggest you work too hard
- RGB及sRGB与XYZ坐标转换
- Adaboost
- [issue 26] 123hr experience of Tencent teg+ operation development
- Clion installation Download
- 使用SystemVerilog门模型描述的组合逻辑
猜你喜欢
随机推荐
错误:note: module requires Go 1.17
Combinatorial logic described using SystemVerilog gate model
Link a static library‘s all sections
Dos Bat 语法记录一
Improve your game‘s performance
vscode极简安装教程
C指針的理解
Implementation of large file fragment uploading based on webuploader
Delete the packaging use of pop-up components
串口(RS - 232)
EPP (Enhanced Parallel Port 增强型并口)
Serial port (RS - 232)
Trigger
Remove then add string from variable of Makefile
Vue des nombres élevés du point de vue de l'espace vectoriel (1) - - Introduction à la série
组合逻辑块的测试平台
Understanding of C pointer
我不建议你工作太拼命
Global and Chinese silicon carbide barrier Schottky diode market demand and future prospect report 2022-2027
Frame profiling









