代码随想录刷第6天

文章包含四个LeetCode问题的C++解决方案:检查两个字符串是否为字母异位词,找出两个整数数组的交集,判断一个数是否为快乐数,以及找到数组中和为目标值的两个数。所有解法均利用了哈希表提高效率。

代码随想录刷题第6天

有效的字母异位词

/*
 * @lc app=leetcode.cn id=242 lang=cpp
 *
 * [242] 有效的字母异位词
 */

// @lc code=start
#include <iostream>
#include<unordered_set>
using namespace std;

class Solution {
public:
    bool isAnagram(string s, string t) {
        int record[26] = {0};
        int n = s.size(),m = t.size();
        for (int i = 0; i < n; i++)
        {
            record[s[i] - 'a']++;
        }
        
        for (int j = 0; j < m; j++)
        {
            record[t[j] - 'a']--;
        }

        for (int k = 0; k < 26; k++)
        {
            if (record[k] != 0)
            {
                return false;
            }
            
        }
        return true;
    }
};
// @lc code=end


两个数组的交集

/*
 * @lc app=leetcode.cn id=349 lang=cpp
 *
 * [349] 两个数组的交集
 */

#include<vector>
#include<unordered_set>
using namespace std;
// @lc code=start
class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
    
    unordered_set<int> result_set;
    unordered_set<int> set(nums1.begin(),nums1.end());

    for(int num:nums2){
        if (set.find(num) != set.end())
        {
            result_set.insert(num);
        }
    }
    vector<int> result(result_set.begin(),result_set.end());
    return result;
    }
};
// @lc code=end


快乐数

/*
 * @lc app=leetcode.cn id=202 lang=cpp
 *
 * [202] 快乐数
 */

// @lc code=start
#include<vector>
#include<unordered_set>
using namespace std;
class Solution {
public:
    int happy_num(int x){
        int num = 0;
        while (x)
        {
            int n = x % 10 ;
            num += n*n;
            x /= 10;
        }
        return num;
    }

    bool isHappy(int n) {
        unordered_set<int> result;
    while(true){
        result.insert(n);
        n = happy_num(n);
        if (n == 1)
        {
            return true;
        }
        else if (result.find(n) != result.end())
        {
            break;
        }
        
    }
        return false;
        
    }
};
// @lc code=end


两数之和

/*
 * @lc app=leetcode.cn id=1 lang=cpp
 *
 * [1] 两数之和
 */

// @lc code=start
#include<string.h>
#include<vector>
#include<algorithm>
#include<unordered_map>
using namespace std;
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};
            }else{
                map.insert(pair<int,int>(nums[i],i));
            }
            
        }
        return {};
        
    }
};
// @lc code=end


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值