代码随想录算法训练营Day6 | 242.有效的字母异位词 349. 两个数组的交集 202. 快乐数 1. 两数之和
LeetCode 242.有效的字母异位词
题目链接:242.有效的字母异位词
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Constraints:
1 <= s.length, t.length <= 5 * 104
s and t consist of lowercase English letters.
Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
思路:
1.用数组作为哈希表
2.数组下标为索引
3.字符串的差值为数组下标
class Solution {
public:
bool isAnagram(string s, string t) {
vector<int> A(26, 0);
for(int i=0; i<s.size(); i++){
A[s[i]-'a']++;
}
for(int j=0; j<t.size(); j++){
A[t[j]-'a']--;
}
for(int k=0; k<26; k++){
if (A[k]!=0){
return false;
}
}
return true;
}
};
注意 :
- 注意数组的定义也可以用 int A[26] = {0};
349. 两个数组的交集
题目链接:349. 两个数组的交集
Given two integer arrays nums1 and nums2, return an array of their
intersection
. Each element in the result must be unique and you may return the result in any order.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
Explanation: [4,9] is also accepted.
Constraints:
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
思路:
先将nums1转化为set,去重后遍历nums2查找,存在的元素添加至结果集
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> res;
unordered_set<int> nums_set(nums1.begin(),nums1.end());
for (int num:nums2){
if (nums_set.find(num) != nums_set.end()){
res.insert(num);
}
}
return vector<int>(res.begin(),res.end());
}
};
注意 :
- set 定义为
unordered_set<int> res;
- 数组转set和set转数组类似:
unordered_set<int> nums_set(nums1.begin(),nums1.end());
- set转数组:
vector<int>(res.begin(),res.end());
LeetCode 第202题. 快乐数
题目链接:LeetCode 第202题. 快乐数
Write an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy.
Return true if n is a happy number, and false if not.
Example 1:
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
Example 2:
Input: n = 2
Output: false
Constraints:
1 <= n <= 231 - 1
思路:
1.写一个求squred sum的函数;
2.循环求sum,如果为1,return true;
3.如果重复,return false;
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> A;
int S;
while(true){
S = getSum(n);
if (S==1){
return true;
}
if(A.find(S) != A.end()){
return false;
}
else{
A.insert(S);
}
n = S;
}
注意 :
- 如何实现一个数的平方和 循环
- 如何实现循环求Squared sum
- S=1即终止
LeetCode 1. 两数之和
题目链接:1. 两数之和
思路:
将nums[i]和i插入hashmap
检验 target-nums[i]是否在map里
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map <int,int> A;
for (int i; i<nums.size(); i++){
auto iter = A.find(target-nums[i]);
if (iter!=A.end()){
return {iter->second, i};
}
A.insert(pair<int,int>(nums[i],i));
}
return {};
}
};
注意 :
- 是
unordered_map
pair<int,int>(nums[i],i)
表示tuple- tuple第二个元素为->second