【代码随想录】242.有效的字母异位词、349. 两个数组的交集、202. 快乐数、1. 两数之和

242.有效的字母异位词、349. 两个数组的交集、202. 快乐数、1. 两数之和

242.有效的字母异位词-Easy

哈希表
思路:record用来上记录字符串s里字符出现的次数。

需要把字符映射到数组也就是哈希表的索引下标上,因为字符a到字符z的ASCII是26个连续的数值,所以字符a映射为下标0,相应的字符z映射为下标25。

再遍历 字符串s的时候,只需要将 s[i] - ‘a’ 所在的元素做+1 操作即可,并不需要记住字符a的ASCII,只要求出一个相对数值就可以了。 这样就将字符串s中字符出现的次数,统计出来了。

那看一下如何检查字符串t中是否出现了这些字符,同样在遍历字符串t的时候,对t中出现的字符映射哈希表索引上的数值再做-1的操作。

那么最后检查一下,record数组如果有的元素不为零0,说明字符串s和t一定是谁多了字符或者谁少了字符,return false。

最后如果record数组所有元素都为零0,说明字符串s和t是字母异位词,return true。

状态:已完成

// 242.有效的字母异位词
class Solution {
public:
    bool isAnagram(string s, string t) {
        int record[26] = {0};
        for(int i = 0; i < s.size(); i++){
            record[s[i] - 'a']++;
        }
        for(int i = 0; i < t.size(); i++){
            record[t[i] - 'a']--;
        }
        for(int i = 0; i < 26; i++){
            if(record[i] != 0){
                return false;
            }
        }
        return true;
    }
};

349. 两个数组的交集-Easy

状态:已完成

利用STL容器 unordered_set解题。

unordered_set容器,可直译为无序 set 容器,是一个没有重复数据的无序容器。

首先介绍unordered_set容器用法:
使用时需要添加头文件和命名空间

#include<vector>
using namespace std ;

// 创建空的set
unordered_set<int> set1;

// 拷贝构造
unordered_set<int> set2(set1);

// 使用迭代器构造 【常用】
unordered_set<int> set3(set1.begin(), set1.end());

// 使用数组作为其初值进行构造
unordered_set<int> set4(arr,arr+5);

// 使用处置列表进行构造
unordered_set<int> set6 {1,2,10,10};

// unordered_set常用函数有以下:
find() // 【常用】
myset.find(x) // 功能为:查找x,找到之后返回迭代器,失败则返回myset.end()。注意:unordered_set中数值不可重复,所以被寻找的数只有存在和不存在这两种情况,不可能出现多次。

insert()  // 【常用】
myset.insert(x) // 功能为:插入元素x

erase()
myset.erase(x) // 功能为:删除元素x,成功则返回1,失败则返回0

// 该部分参考Elec Liu的文章《STL容器——unordered_set的用法》
// 原文链接:https://blog.csdn.net/qq_44629819/article/details/131294191

代码随想录题解:

// 349. 两个数组的交集
class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> result_set; // 存放结果,并去重
        unordered_set<int> nums_set(nums1.begin(), nums1.end());
        // 发现nums2的元素 在nums_set里又出现过
        for(int num : nums2){
        	if(nums.find(num) != nums.end()){
        		result_set.insert(num);
        	}
        }
        return vector<int>(result_set.begin(), result_set.end());  // 最后要把set转成vector
    }
};

202. 快乐数-Medium

状态:已完成

代码随想录题解:

// 202. 快乐数
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> set;
        while(1){
            int sum = getSum(n);
            if(sum == 1){
                return true;
            }
            if(set.find(sum) != set.end()){
                return false;
            }
            else{
                set.insert(sum);
            }
            n = sum;
        }
    }
};

1. 两数之和-Easy

状态:已完成

代码随想录题解:

// 1. 两数之和
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map <int, int> map;
        for(int i = 0; i < nums.size(); i++){
            auto iter = map.find(target - nums[i]);
            if(iter != map.end()){
                return {iter->second, i};
            }
            map.insert(pair<int, int>(nums[i], i));
        }
        return {};
    }
};
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值