目录
文章目录
- [242. 有效的字母异位词](https://leetcode.cn/problems/valid-anagram/description/)
- [349. 两个数组的交集](https://leetcode.cn/problems/intersection-of-two-arrays/description/)
- [202. 快乐数](https://leetcode.cn/problems/happy-number/description/)
- [1. 两数之和](https://leetcode.cn/problems/two-sum/description/)
242. 有效的字母异位词
Problem: 242. 有效的字母异位词
1.思路
哈希表
ASCII码
2.Code
class Solution {
public:
bool isAnagram(string s, string t) {
int hashmap_s[26], hashmap_st[26];
for(int i = 0; i < s.size(); i++){
hashmap_s[s[i]-'a']++;
}
for(int i = 0; i < t.size(); i++){
hashmap_st[t[i]-'a']++;
}
for(int i = 0; i < 26; i++){
if(hashmap_s[i]!=hashmap_st[i]) return false;
}
return true;
}
};
349. 两个数组的交集
Problem: 349. 两个数组的交集
1.思路
哈希表
难点在于数组内的元素不能重复,如果可以返回set的话就不用最后一步了,如果不想用set则需要判断vector内是否已经存在这个数字,还需要用find函数。
2.set的基本使用方法
复习了一下set的使用
#include <iostream>
#include <unordered_set>
int main() {
std::unordered_set<int> mySet;
// 插入元素
mySet.insert(1);
mySet.insert(2);
mySet.insert(3);
mySet.insert(4);
// 检查元素是否存在
if (mySet.find(2) != mySet.end()) {
std::cout << "找到元素 2" << std::endl;
} else {
std::cout << "未找到元素 2" << std::endl;
}
// 删除元素
mySet.erase(2);
// 遍历 unordered_set
for (auto elem : mySet) {
std::cout << elem << std::endl;
}
return 0;
}
3.Code
方法一:数组+set
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
int hashmap[1001] = {0};
vector<int> res;
unordered_set<int> record;
for(int i = 0; i < nums1.size(); i++){
hashmap[nums1[i]]++;
}
for(int i = 0; i < nums2.size(); i++){
if(hashmap[nums2[i]]) record.insert(nums2[i]);
}
for(auto it : record){
res.push_back(it);
}
return res;
}
};
方法二:set
学会了两种便捷的写法
- 其中
unordered_set<int> nums_set(nums1.begin(), nums1.end());
:整行代码构造了一个新的unordered_set,名为nums_set,并通过迭代器范围将nums1中的所有元素复制到这个新集合中。由于unordered_set的性质,任何重复的元素都会被自动剔除,只留下唯一的元素。
这种初始化方式非常有用,特别是当你需要从已有的数据集中快速创建一个不包含重复元素的集合时。例如,你可能想要统计某个数组中不同元素的数量,或者需要对数据进行去重处理。通过这种方式,可以轻松地实现这些需求,同时利用unordered_set提供的高效查找、插入和删除操作。
- 学会了容器中
for(int num : nums2)
的写法,num是值的类型,也可以用auto替代,num是容器的值,num2是容器的名称,这种写法适用于不需要访问容器的key时使用。(如果这个容器是vector的话,num就是nums2[i])
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> res;
unordered_set<int> hashtable(nums1.begin(),nums1.end());
for(int num : nums2){
if(hashtable.find(num)!=hashtable.end()) res.insert(num);
}
return vector<int> (res.begin(),res.end());
//return vector<int> re(res.begin(),res.end()); 这样写不对,上面一行的写法才对
}
};
202. 快乐数
Problem: 202. 快乐数
思路
这道题的思路在于需要先模拟一下过程,理清楚循环后续的可能情况
这个是比较难想的,必须和题目的特性结合
三种情况
1.跌为1
2.形成内循环(形成内循环的前提是,会重复达到一个数字)
3.值越来越大(证明不会)因为即使是13位数的9999999999999,在计算一轮都都会跌为四位数的1053,而四位数最大的9999都会跌为324,即四位数以上的数始终在一次或两次计算后会跌成三位数,形成内循环或者最终跌为1,所以3这种情况不需要处理
https://leetcode.cn/problems/happy-number/solutions/224894/kuai-le-shu-by-leetcode-solution
参考官方题解
下面两种方法的大体思路是一致的,不同的点在于环的检测
方法一:用哈希集合检测循环
方法二:弗洛伊德环【快慢指针】
第一次听说这个方法,理论是看的官方题解
Code
class Solution {
public:
bool isHappy(int n) {
//用哈希集合检测循环
unordered_set<int> hashtable;
int next = n;
while(n!=1){
//检测n是否出现过,出现过即形成了内循环,直接返回false
if(hashtable.find(n)!=hashtable.end()){
return false;
}else{
hashtable.insert(n);
}
next = 0;//重置
//计算next
while(n){
next += (n%10)*(n%10);
n /= 10;
}
n = next;
}
return true;
}
};
1. 两数之和
Problem: 1. 两数之和
1.思路
方法一:暴力
两个for循环
方法二:哈希表
使用map存下数和下标,检测互补值是否在前面出现过,如果出现了就返回他们的下标
2.Code
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> keyToIndex;
for(int i = 0; i < nums.size(); i++){
if(keyToIndex.find(target-nums[i])!=keyToIndex.end()){
return {i,keyToIndex[target-nums[i]]};
}
keyToIndex[nums[i]] = i;
}
return {};
}
};
刚开始写的
3.Code
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> keyToIndex;
for(int i = 0; i < nums.size(); i++){
keyToIndex[nums[i]] = i;
//这里有个错误是,在键值队插入到map之后在赋值的话,会导致如数组【3,3】,target=6时,会直接返回【0,0】因为发现的3这个值均在0处出现,如果在这里加上下面这句话,也会不对,如果是相同的键是会覆盖的
//if(keyToIndex.find(target-nums[i])!=keyToIndex.end()&&keyToIndex(target-nums[i])!=keyToIndex(nums[i])即想要的的所以值需要不一样
if(keyToIndex.find(target-nums[i])!=keyToIndex.end()){
return {i,keyToIndex[target-nums[i]]};
}
}
return {};
}
};