【代码随想录算法训练营——Day6(Day5周日休息)】哈希表——242.有效的字母异位词、349.两个数组的交集、202.快乐数、1.两数之和

LeetCode题目链接

https://leetcode.cn/problems/valid-anagram/

https://leetcode.cn/problems/intersection-of-two-arrays/

https://leetcode.cn/problems/happy-number/

https://leetcode.cn/problems/two-sum/

题解

242.有效的字母异位词

这道题要想到用哈希表来做。同时注意最后的返回值经AI呈现可以直接返回为hash1==hash2,不失为一个新举措。

349.两个数组的交集

分两个哈希表即可。

202.快乐数

乍一看题目,不太会写。怎么样返回无限循环的false。

好吧,AI提示当n==4的时候就返回false,我不知道这是什么原因,看了题解再说吧,即看卡哥视频。

代码是写出来了,但不完全是自己写的,尤其是取个位数的循环条件和处理逻辑。

题解一定要看,代码很简洁,注意用到的数据结构和各个返回逻辑。

1.两数之和

想用双指针的写法,但发现题目不能让数组排序,要按照原顺序。

试了下两个for循环暴力解法就过了。再去看题解,应该有更合理的解法。

代码

cpp 复制代码
//242.有效的字母异位词
#include <iostream>
#include <vector>
#include <string>
using namespace std;

class Solution {
public:
    bool isAnagram(string s, string t) {
        vector<int> hash1(26, 0), hash2(26, 0);
        for (int i = 0;i < s.size();i++) {
            hash1[s[i] - 'a']++;
        }
		for (int i = 0;i < t.size();i++) {
			hash2[t[i] - 'a']++;
		}
        return hash1 == hash2;
    }
};

int main() {
    string str = "rat", t = "car";
    Solution s;
    printf("%d", s.isAnagram(str, t));
    return 0;
}
cpp 复制代码
//349.两个数组的交集
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        vector<int> hash1(1010, 0),hash2(1010,0), result;
        for (int i = 0;i < nums1.size();i++) {
            hash1[nums1[i]]++;
        }
        for (int i = 0;i < nums2.size();i++) {
            hash2[nums2[i]]++;
        }
        for (int i = 0;i < hash1.size();i++) {
			if (hash1[i] > 0 && hash2[i] > 0) result.push_back(i);
        }
        return result;
    }
};


int main() {
    Solution s;
    vector<int> nums1 = { 4,9,5 }, nums2 = { 9,4,9,8,4 };
	vector<int> result = s.intersection(nums1, nums2);
    for (int i = 0;i < result.size();i++) {
        printf("%d ", result[i]);
    }
	return 0;
}
cpp 复制代码
//202.快乐数
#include <iostream>
using namespace std;

class Solution {
public:
    bool isHappy(int n) {
        while (n != 1) {
            int sum = 0;
			while (n) {
				int digit = n % 10;
				sum += digit * digit;
				n /= 10;
			}
            n = sum;
            if (n == 4) {
                return false;
            }
        }
        return true;
    }
};

int main() {
    Solution s;
    int n = 2;
    printf("%d", s.isHappy(n));
    return 0;
}
cpp 复制代码
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        for(int i = 0; i < nums.size();i++){
            for(int j = i + 1;j < nums.size();j++){
                if(nums[i] + nums[j] == target) return {i, j};
            }
        }
        return {0, 0};
    }
};
相关推荐
货拉拉技术30 分钟前
Agent 场景下 Token 成本优化的实战技巧
算法
hahaha601640 分钟前
HLS高层次综合设计技巧--循环merge和循环split
人工智能·算法·计算机视觉
JAI科研41 分钟前
Deepseek Agent Harness教程(七) | Deepseek Harness不是一个内核加一堆插件
开发语言·人工智能·深度学习·算法·自然语言处理·transformer·vllm
positive_zpc1 小时前
进阶数据结构图——关键路径(四)
数据结构·图论·关键路径
集思广益的灰太狼1 小时前
变频器启动致PLC数据异常?西门子G120配合滤波器EMC抑制方案
人工智能·算法·工控·emc·电磁兼容·变频器·西门子
孙克旭_1 小时前
单链表进阶实操:5 道常考面试题详细解析【Java 实现】
java·开发语言·数据结构·单链表
方方洛1 小时前
vllm教程-03-架构设计
算法·架构
玄昌盛不会编程1 小时前
LeetCode——2091. 从数组中移除最大值和最小值
java·算法·leetcode
如何取名1 小时前
结合实际业务进行LRU淘汰算法优化设计(数据库开发日志)
数据库·算法
aichitang20242 小时前
快乐泛函每一天:希尔伯特空间中的最佳逼近与正交投影定理
人工智能·考研·算法·ai·面试·泛函分析