代码随想录算法训练营第六天|LeetCode242.有效的字母异位词、349.两个数组的交集、202.快乐数、1.两数之和

什么时候用哈希表?

需要快速找到某个元素是否存在时。

哈希表常用数据结构以及如何选择?

  1. 数组:元素所有可能值的范围较短,用一个数组可以将元素所有可能值保存下来,当元素所有可能值范围较大比如上亿个可能值,无法再用连续存储空间来表示就不再适用。

  1. set:仅需要查找某元素是否存在,同时元素可能值范围很大。

  1. map: 需要找到某元素是否存在,同时还要了解这个元素某些属性。

题目链接:242.有效的字母异位词

刚接触这道题想的用map来记录每个字母出现的次数,map可以简化,将每个字母离字符'a'的偏移量来提供数组的索引值。

class Solution {
public:
    bool isAnagram(string s, string t) {
        vector<int> hash(26, 0);
        for(int i = 0; i < s.length(); i++) {
            hash[s[i] - 'a']++;
        }
        for(int i = 0; i < t.length(); i++) {
            hash[t[i] - 'a']--;
        }
        for(int i = 0; i < hash.size(); i++) {
            if(hash[i] != 0) return false;
        }
        return true;
    }
};

题目链接:349. 两个数组的交集

对c++标准库中容器使用的不熟练、原理不熟悉导致刚开始写的代码很臃肿,set初始化方式很笨:

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> hash;
        vector<int> result;
        for(size_t i = 0; i < nums1.size(); i++) {
            if(hash.find(nums1[i]) == hash.end()) {
                hash.insert(nums1[i]);
            }
        }
        for(size_t i = 0; i < nums2.size(); i++) {
            if(hash.find(nums2[i]) != hash.end()){
                hash.erase(hash.find(nums2[i]));
                result.push_back(nums2[i]);
            }
        }
        return result;
    }
};

看了大佬写的代码之后感觉自己写的就像rubbish:

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> hash(nums1.begin(), nums1.end());
        unordered_set<int> result;
        for(int i : nums2) {
            if (hash.find(i) != hash.end()) {
                result.insert(i);
            }
        }
        return vector<int>(result.begin(), result.end());
    }
};

由于这道题规定了nums1与nums2值的范围是[0, 1000],因此可以选用数组来构建哈希表:

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        vector<int> result;
        bool hash[1001] = {false};
        for(int i : nums1) {
            if(!hash[i]) hash[i] = true;
        }
        for(int i : nums2) {
            if(hash[i]) {
                result.push_back(i);
                hash[i] = false;
            }
        }
        return result;
    }
};

题目链接:202. 快乐数

单纯看这道题想不到用hash表,同时不知道为什么如果不收敛到1,一定会陷入死循环,为什么出现过的值会重新出现,而不是每一次结果都不同。

class Solution {
public:
    bool isHappy(int n) {
        unordered_set<int> hash;
        while (hash.find(n) == hash.end()){
            if(n == 1) {
                return true;
            }
            hash.insert(n);
            n = sum(n);
        }
        return false;
    }
    int sum(int n) {
        int result = 0;
        while(n > 0) {
            result += (n % 10) * (n % 10);
            n /= 10;
        }
        return result;
    }
};

题目链接: 1. 两数之和

自己写的时候想到用map,没想到一边向map里插入值一边寻找符合条件的值。

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> hash;
        for(int i = 0; i < nums.size(); i++) {
            hash[nums[i]] = i;
        }
        for(int i = 0; i < nums.size(); i++) {
            if(hash.find(target - nums[i]) != hash.end() && \
            hash.find(target - nums[i])->second != i) {
                return vector<int> {i, hash.find(target - nums[i])->second};
            }
        }
        return vector<int> {};
    }
};

优化之后:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int> hash;
        for(int i = 0; i < nums.size(); i++) {
            if(hash.find(target - nums[i]) == hash.end()) {
                hash[nums[i]] = i;
            }
            else {
                return vector<int>{hash.find(target - nums[i])->second, i};
            }
        }
        return vector<int>{};
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第二十二天的算法训练营主要涵盖了Leetcode题目中的三道题目,分别是Leetcode 28 "Find the Index of the First Occurrence in a String",Leetcode 977 "有序数组的平方",和Leetcode 209 "长度最小的子数组"。 首先是Leetcode 28题,题目要求在给定的字符串中找到第一个出现的字符的索引。思路是使用双指针来遍历字符串,一个指向字符串的开头,另一个指向字符串的结尾。通过比较两个指针所指向的字符是否相等来判断是否找到了第一个出现的字符。具体实现的代码如下: ```python def findIndex(self, s: str) -> int: left = 0 right = len(s) - 1 while left <= right: if s[left == s[right]: return left left += 1 right -= 1 return -1 ``` 接下来是Leetcode 977题,题目要求对给定的有序数组中的元素进行平方,并按照非递减的顺序返回结果。这里由于数组已经是有序的,所以可以使用双指针的方法来解决问题。一个指针指向数组的开头,另一个指针指向数组的末尾。通过比较两个指针所指向的元素的绝对值的大小来确定哪个元素的平方应该放在结果数组的末尾。具体实现的代码如下: ```python def sortedSquares(self, nums: List[int]) -> List[int]: left = 0 right = len(nums) - 1 ans = [] while left <= right: if abs(nums[left]) >= abs(nums[right]): ans.append(nums[left ** 2) left += 1 else: ans.append(nums[right ** 2) right -= 1 return ans[::-1] ``` 最后是Leetcode 209题,题目要求在给定的数组中找到长度最小的子数组

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值