【代码随想录算法训练营11期】Day 6 | C++

242. 有效的字母异位词

解题思路:用数组做哈希映射

class Solution {
public:
    bool isAnagram(string s, string t) {
        if (s.size() != t.size())   return false;
        
        // 1.初始化字母“偏移”表,把所有字母对应的偏移位都置0
        int record[26] = {0};
        // 2.先把字符映射到数组中并登记出现次数,再减去登记次数
        for (int i = 0; i < s.size(); i ++) {
            // s[i] - 'a'代表第i个字母对a的偏移量,例如i = 0,则有'a' - 'a' = 0
            record[s[i] - 'a'] ++; // 登记字符出现次数
            record[t[i] - 'a'] --; // 减去字符出现次数,减到0代表是有效的字母异同位
        }
        // 3.如果数组中某位元素没变成0,那么字符串s和t中存在字母出现次数不一致的情况
        for (int i = 0; i < sizeof(record) / sizeof(record[0]); i ++) {
            if (record[i] != 0 ) {
                return false;
            }
        }
        // 4.record数组所有元素都为零0,说明字符串s和t是字母异位词
        return true;
    }
};

349. 两个数组的交集

解题思路:用set做哈希映射

class Solution {
public:
    vector intersection(vector& nums1, vector& nums2) {
        unordered_set nums_set(nums1.begin(), nums1.end());
        unordered_set result_set;
        for (int i = 0; i < nums2.size(); i ++) {
            if (nums_set.find(nums2[i]) != nums_set.end())
                result_set.insert(nums2[i]);
        }
        vector res (result_set.begin(), result_set.end());
        return res;
    }
};

202. 快乐数

解题思路:用set做哈希映射

class Solution {
public:
    int getNext(int n) {
        int sum = 0;
        while (n) {
            sum += (n % 10) * (n % 10);
            n /= 10;
        }
        return sum;
    }

    bool isHappy(int n) {
        unordered_set res_set;
        if (n == 1) return true;
        while (1) {
            int sum = getNext(n);
            if (sum == 1) return true;
            if (res_set.find(sum) != res_set.end()) {
                return false;
            }else {
                res_set.insert(sum);
            }
            n = sum;
        }
    }
};

1. 两数之和

解题思路:

1.暴力解法:

class Solution {
public:
    vector twoSum(vector& nums, int target) {
        vector res;
        for (int i = 0; i < nums.size(); i ++) {
            for (int j = i + 1; j < nums.size(); j ++) {
                if (nums[i] + nums[j] == target) {
                    res.push_back(i);
                    res.push_back(j);
                }
            }
        }
        return res;
    }
};

2.暴力解法:

class Solution {
public:
    vector twoSum(vector& nums, int target) {
        vector res;
        for (int i = 0; i < nums.size(); i ++) {
            int tmp = target - nums[i];
            for (int j = i + 1; j < nums.size(); j ++) {
                if (nums[j] == tmp) {
                    res.push_back(i);
                    res.push_back(j);
                }
            }
        }
        return res;
    }
};

3.用map做哈希映射

class Solution {
public:
    vector twoSum(vector& nums, int target) {
        unordered_map res_map;
        for (int i = 0; i < nums.size(); i ++) {
            // 先在map中查找target - nums[i],若查找失败,则在map中插入(nums[i], i)
            auto iter = res_map.find(target - nums[i]); // 用迭代器来搜索
            // 待查找的元素存在map中,返回它的下标
            if (iter != res_map.end()) {
                return {iter -> second, i};
            }
            // 待查找的元素不在map中,插入一对新键值对
            res_map.insert(pair(nums[i], i));
        }
        return {};
    }
};

你可能感兴趣的:(代码随想录算法训练营11期,c++,力扣,哈希表)