首页 > 编程语言 >代码随想录算法训练营第五天LeetCode242,349,202,1

代码随想录算法训练营第五天LeetCode242,349,202,1

时间:2023-01-02 22:11:06浏览次数:83  
标签:LeetCode242 202 return int res 随想录 vector https set

代码随想录算法训练营第五天|LeetCode242,349,202,1

LeetCode 242 有效的字母异位词

题目链接:https://leetcode.cn/problems/valid-anagram/description/
//开了两个哈希数组,两个哈希数组进行比较
class Solution {
public:
    bool isAnagram(string s, string t) {
        //用数组做一个映射
        int res_hash1[26] = {0};
        int res_hash2[26] = {0};
        //遍历s字符串,记录s字符串中字母出现的次数
        for(int i = 0; i < s.size(); i++){
            res_hash1[s[i] - 'a']++;
        }
        //遍历t字符串,记录t字符串中字母出现的次数
        for(int i = 0; i < t.size(); i++){
            res_hash2[t[i] - 'a']++;
        }
        //两个数组进行比较
        for(int i = 0; i < 26; i++){
            if(res_hash1[i] != res_hash2[i]){
                return false;
            }
        }
        return true;
    }
};
视频讲解链接:https://www.bilibili.com/video/BV1YG411p7BA/?spm_id_from=333.788&vd_source=8d6cce160628d93add1e5fa9299f622d
文章讲解链接:https://programmercarl.com/0242.有效的字母异位词.html
//时间复杂度为O(n),空间上因为定义是的一个常量大小的辅助数组,所以空间复杂度为O(1)。
class Solution {
public:
    bool isAnagram(string s, string t) {
        //用一个哈希数组做映射
        int res_hash[26] = {0};
        //遍历字符串s
        for(int i = 0; i < s.size(); i++){
            res_hash[s[i] - 'a']++;
        }
        //遍历字符串t
        for(int i = 0; i < t.size(); i++){
            res_hash[t[i] - 'a']--;
        }
        //可以看到,比起之前,只开辟了一个数组的空间
        for(int i = 0; i < 26; i++){
            if(res_hash[i] != 0){
                return false;
            }
        }
        return true;
    }
};

LeetCode 349 两个数组的交集

题目链接:https://leetcode.cn/problems/intersection-of-two-arrays/description/
//使用set
class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        //根据题目,输出结果中的每个元素一定是唯一的
        //用set存储数组
        set<int>res1(nums1.begin(), nums1.end()); 
        set<int>res2(nums2.begin(), nums2.end());
        //新建一个数组存储交集
        vector<int>intersection;
        for(auto &elem : res2){
            if(res1.find(elem) != res1.end()){
                intersection.push_back(elem);
            }
        }
        return intersection;

    }
};
视频讲解链接:https://www.bilibili.com/video/BV1ba411S7wu/?spm_id_from=333.788&vd_source=8d6cce160628d93add1e5fa9299f622d
文章讲解链接:https://programmercarl.com/0349.两个数组的交集.html
//使用unordered_set无序关联式容器,查询的时间复杂度为O(1)
class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        //可以使用unordered_set无序关联式容器,查询的时间复杂度为O(1)
        unordered_set<int> res(nums1.begin(), nums1.end());
        //创建一个unordered_set存放去重的交集结果
        unordered_set<int> results;
        //遍历
        for(auto &elem : nums2){
            if(res.find(elem) != res.end()){
                results.insert(elem);
            }
        }
        return vector<int>(results.begin(), results.end());

    }
};

LeetCode 202 快乐数

题目链接:https://leetcode.cn/problems/happy-number/description/
//利用unordered_set的特性,如果出现重复的平方和就可以返回false
class Solution {
public:
    //计算数字的每位平方之和
    int sum(int n){
	    int sum = 0;
	    while(n){
		    sum += (n % 10) * (n % 10);
		    n =  n / 10;
	    }
	    return sum;
    }

    bool isHappy(int n){
        //用unordered_set存储每次计算的平方和
        unordered_set<int> res;
        while(1){
            n = sum(n);
            if(n == 1){
                return true;
            }
            //跳出无限循环
            if(res.find(n) != res.end()){
                return false;
            }else{
                res.insert(n);
            }
                
        }
	
    }
};
视频讲解链接:无
文章讲解链接:https://programmercarl.com/0202.快乐数.html

LeetCode 1 两数之和

题目链接:https://leetcode.cn/problems/two-sum/description/
//使用map
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        //找出和为目标值target的两个整数
        //设置一个map存储元素值和下标
        map<int, int> judge;
        //遍历nums数组
        for(int i = 0; i < nums.size(); i++){
            auto iter = judge.find(target - nums[i]);
            if(iter != judge.end()){
                return vector<int>{iter->second, i};
            }
            else{
                judge.insert(pair<int, int>(nums[i], i));
            }
        }
        return{};
        
    }
};
视频讲解链接:https://www.bilibili.com/video/BV1aT41177mK/?spm_id_from=333.788&vd_source=8d6cce160628d93add1e5fa9299f622d
文章讲解链接:https://programmercarl.com/0001.两数之和.html

标签:LeetCode242,202,return,int,res,随想录,vector,https,set
From: https://www.cnblogs.com/shadowbringer/p/17020690.html

相关文章

  • Codeforces Good Bye 2022 CF 1770 F Koxia and Sequence 题解
    题目链接注意题目要求的是所有好序列的所有元素的XOR之和的XOR之和。我一开始以为是所有XOR之和的加法和导致不知道官方题解在讲什么。假设我们枚举一个位置\(1\lei\le......
  • the eleventh——2023.1.2
    scanf()函数一般只读取字符串中的一个单词,而不是一句话。例如:scanf("%s",name);printf("Hello,%s!",name) NingBabaHello,Ning!(后面的Baba在scanf这读取不到,在遇......
  • 2023.1.2周报
    本周总结:学习了《算法竞赛》第六章数论6.7-6.9、第七章组合数学7.1-7.6内容,牛客组合数学课程,做书上例题和习题。准备新手课堂文档和讲课,顺便出结训赛题目。大方向组合数......
  • 报告分享|2022年中国新冠医药研发趋势报告
    2020年爆发的新冠肺炎是百年一遇的全球性公共卫生事件,至今疫情虽有缓和趋势,但影响仍然存在。面对严峻疫情,中国全力投入新冠相关的研发创新,力图降低疫情影响、终结疫情蔓延......
  • 代码随想录day6 哈希表 LeetCode 242.有效的字母异位词 349. 两个数组的交集 202.
    当我们遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法242.有效的字母异位词 https://leetcode.cn/problems/valid-anagram/由于哈希表只有26个元素,故采......
  • 2023/01 LeetCode练习
    ......
  • 2023,整装待发!
    我是个怎么样的人呢.....高考过后,我常常断断续续的思索这些。但是对刚刚“解放”的我来说,思考这些还是过于疲惫和自找不快的一件事。所以就这样遗忘了,在混沌与半清醒......
  • HashMap-2023-1-2
    packageCollection;importjava.util.HashMap;importjava.util.Map;importjava.util.Scanner;importjava.util.Set;publicclassMapTest{publicMap<String,Stu......
  • 月薪集中在8k-17k、厌倦大小周、近三成的人没有跳槽过,2021-2022中国开发者调查报告发
    月薪集中在8k-17k、厌倦大小周、近三成的人没有跳槽过,2021-2022中国开发者调查报告发布「学不完的技术,跟不动的技术潮流」,过去一年,随着数字化、智能化趋势的来临,无论是传统......
  • 2023年跨年演讲对技术人的一点启发
    前言故事1.电动车与书店罗胖点评大叔点评故事2.《螃蟹与红酒》罗胖点评大叔点评故事3.《甘地与糖》罗胖点评大叔点评故事4.《60秒与10年》罗胖点评......