这几道都比较简单,主要是熟悉哈希的操作
class Solution {
public:
bool isAnagram(string s, string t) {
int alphab[26] = {0};
for(int i=0; i < s.size(); i++)
{
alphab[ s[i] - 'a' ]++;
}
for( int i=0; i<t.size(); i++)
{
alphab[ t[i] - 'a' ]--;
}
for(int i=0; i<26; i++)
{
if( alphab[i] !=0 )
return false;
}
return true;
}
};
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> set1;
unordered_set<int> result1;
for(int i=0 ;i < nums1.size(); i++)
{
set1.insert(nums1[i]);
}
for(int i = 0; i<nums2.size(); i++)
{
if( set1.find(nums2[i]) != set1.end() )
result1.insert(nums2[i]);
}
return vector<int>(result1.begin(), result1.end() );
}
};
class Solution {
public:
int getsum(int n)
{
int sum=0;
while(n)
{
sum += (n%10) * (n%10);
n = n/10;
}
return sum;
}
bool isHappy(int n) {
unordered_set<int> set1 {0};
int result = 0;
while(1)
{
result = getsum(n);
n = result;
if( result == 1 )
return true;
if( set1.find(result) !=set1.end() )
return false;
else
set1.insert(result);
}
}
};
标签:202,return,int,nums1,set,set1,242,349,result
From: https://www.cnblogs.com/gqzz/p/18660638