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;
}