一个本硕双非的小菜鸡,备战24年秋招,计划刷完卡子哥的刷题计划,加油!
推荐一手卡子哥的刷题网站,感谢卡子哥。代码随想录
242. 有效的字母异位词
Note:第一道哈希表的题目,初入哈希。
class Solution {
public:
bool isAnagram(string s, string t) {
int record[26] = {0};
for (int i = 0; i < s.size(); i++) {
record[s[i] - 'a']++;
}
for (int i = 0; i < t.size(); i++) {
record[t[i] - 'a']--;
}
for (int i = 0; i < 26; i++) {
if (record[i] != 0)
return false;
}
return true;
}
};
383. 赎金信
Note:哈希表的题,很简单
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int res[26] = {0};
for (int i = 0; i < ransomNote.size(); i++) {
res[ransomNote[i] - 'a']++;
}
for (int i = 0; i < magazine.size(); i++){
res[magazine[i] - 'a']--;
}
for (int i = 0; i < 26; i++)
{
if(res[i] > 0) return false;
}
return true;
}
};
49. 字母异位词分组
Note:哈希表的变种,但是核心原理是一样的
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> res;
unordered_map<string, vector<string>> mp;
for (string& str :strs) {
string key = str;
sort(key.begin(), key.end());
mp[key].emplace_back(str);
}
for (auto it = mp.begin(); it != mp.end(); it++) {
res.emplace_back(it->second);
}
return res;
}
};
438. 找到字符串中所有字母异位词
Note:哈希表改版
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
int sLen = s.size(), pLen = p.size();
if (sLen < pLen) {
return vector<int>();
}
vector<int> ans;
vector<int> sCount(26);
vector<int> pCount(26);
for (int i = 0; i < pLen; ++i) {
++sCount[s[i] - 'a'];
++pCount[p[i] - 'a'];
}
if (sCount == pCount) {
ans.emplace_back(0);
}
for (int i = 0; i < sLen - pLen; ++i) {
--sCount[s[i] - 'a'];
++sCount[s[i + pLen] - 'a'];
if (sCount == pCount) {
ans.emplace_back(i + 1);
}
}
return ans;
}
};
349. 两个数组的交集
Note:set,一种新的哈希结构体
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> result_set;
unordered_set<int> nums_set(nums1.begin(), nums1.end());
for (int num : nums2) {
if (nums_set.find(num) != nums_set.end())
result_set.insert(num);
}
return vector<int>(result_set.begin(), result_set.end());
}
};
350. 两个数组的交集 II
Note:用Map比较简单
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
vector<int> ans;
unordered_map<int, int> hash;
for (auto &i : nums1) ++hash[i];
for (auto &i : nums2) {
if (hash.find(i) != hash.end()) {
ans.emplace_back(i);
--hash[i];
if (hash[i] == 0) hash.erase(i);
}
}
return ans;
}
};
202. 快乐数
Note:就像卡子哥说的,看似一道数学题,实则要用哈希表
class Solution {
public:
//取每个位的平方和
int getSum (int n) {
int sum = 0;
while (n) {
sum += (n % 10) * (n % 10);
n /= 10;
}
return sum;
}
bool isHappy(int n) {
unordered_set<int> set;
while (1) {
int sum = getSum(n);
if (sum == 1) return true;
if (set.find(sum) != set.end())
return false;
else
set.insert(sum);
n = sum;
}
}
};
1. 两数之和
Note:第一道题也可以用哈希表解决!
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
std::unordered_map <int, int> map;
for (int i = 0; i < nums.size(); i++) {
auto iter = map.find(target - nums[i]);
if (iter != map.end())
return {iter->second, i};
else
map.insert(pair<int, int> (nums[i], i));
}
return {};
}
};
哈希表的用处就是快速判断一个元素是否在集合中,共有三种常用的结构。