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.快乐数
1.两数之和
代码
//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;
}