代码随想录算法训练营第五天| 242.有效的字母异位词 , 349. 两个数组的交集 , 202. 快乐数 , 1. 两数之和

发布时间 2023-06-12 22:07:46作者: 博二爷

242.有效的字母异位词 

繁冗版:

1,思路:

  先建立两个map,对应两个字符串对应的字符,同时对他们进行计数,如果这两个数字相等,那么就是相等

2,代码

 1 bool isAnagram_complicate(string s, string t) 
 2 {
 3     unordered_map<char, int> existedCharBys;
 4 
 5     for (auto charItem : s)
 6     {
 7         if (existedCharBys.find(charItem) == existedCharBys.end())
 8         {
 9             existedCharBys.insert(make_pair(charItem, 0));
10         }
11         existedCharBys[charItem]++;
12     }
13 
14     unordered_map<char, int> existedCharByt;
15 
16     for (auto charItem : t)
17     {
18         if (existedCharByt.find(charItem) == existedCharByt.end())
19         {
20             existedCharByt.insert(make_pair(charItem, 0));
21         }
22         existedCharByt[charItem]++;
23     }
24     
25     // s > t
26     if (existedCharBys.size() < existedCharByt.size())
27     {
28         swap(existedCharBys, existedCharByt);
29     }
30 
31     for (auto Item : existedCharBys)
32     {
33         if (existedCharByt[Item.first] != Item.second)
34         {
35             return false;
36         }
37     }
38 
39     return true;
40 }

简单版:

1,思路:

  因为一共有26个字母,所以只用定义一个26长度的数组,同时对一个是++,另一个是--,如果为0那么就是相等

2,代码:

 1 bool isAnagram(string s, string t)
 2 {
 3     int charItem[26] = {};
 4 
 5     // cahr 和 staring[i] 不同
 6     for (int i = 0; i < s.size(); i++) {
 7         charItem[s[i] - 'a']++;
 8     }
 9     for (int i = 0; i < t.size(); i++) {
10         charItem[t[i] - 'a']--;
11     }
12 
13     for (auto num : charItem)
14     {
15         if (num != 0)
16         {
17             return false;
18         }
19     }
20     
21     return true;
22 }
 

349. 两个数组的交集 

思路:

  1,建立一个Set,如果另一个string中有这个set,那么就把值放到set中,同时使用

  vector<int>(set.begin, set.end)进行插入

代码:

 1 vector<int> intersection(vector<int>& nums1, vector<int>& nums2) 
 2 {
 3     unordered_set<int> result;
 4     unordered_set<int> intersectSet;
 5     for (int num1 : nums1)
 6     {
 7         intersectSet.insert(num1);
 8     }
 9 
10     for (int num2 : nums2)
11     {
12         if (intersectSet.find(num2) != intersectSet.end())
13         {
14             result.insert(num2);
15         }
16     }
17 
18     return vector<int>(result.begin(), result.end());
19 }
 

202. 快乐数 

难点:

1,怎么快速把一个数字找出来它的所有位数

2,怎么判断应该结束,当前状态处于无限循环中

代码:

数字-》打散成位数:

 1 int getJudgeNum(int n)
 2 {
 3     int result = 0;
 4     while (n != 0)
 5     {
 6         result += (n % 10) * (n % 10);
 7         n = n / 10;
 8     }
 9 
10     return result;
11 }

判断快乐数

 1 bool isHappy(int n)
 2 {
 3     //先找出来 每一个位置的 数字
 4     unordered_set<int> selectedNum;
 5     while (1)
 6     {
 7 
 8         n = getJudgeNum(n);
 9         if (n == 1)
10         {
11             return true;
12         }
13         //注意循环的时候可能会相等
14         if (selectedNum.find(n) != selectedNum.end())
15         {
16             return false;
17         }
18         selectedNum.insert(n);
19     }
20 
21 }