11.9 LeetCode 题目汇总与解题思路

第一题:242. 有效的字母异位词

解法1:数组计数法(最优)

复制代码
class Solution {
    public boolean isAnagram(String s, String t) {
        if(s.length()!=t.length()) return false;
        int[] alpha = new int[26];
        for(int i=0;i<s.length();i++){
            alpha[s.charAt(i)-'a']++;
            alpha[t.charAt(i)-'a']--;
        }
        for(int i=0;i<26;i++){
            if(alpha[i]!=0) return false;
        }
        return true;
    }
}

特点

  • 时间复杂度:O(n)
  • 空间复杂度:O(1)
  • 只适用于小写字母场景

解法2:排序比较法

复制代码
class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.length()!=t.length()) return false;
        sort(s.begin(),s.end());
        sort(t.begin(),t.end());
        return s==t;
    }
};

特点

  • 时间复杂度:O(nlogn)
  • 空间复杂度:O(1) 或 O(n)(取决于排序算法)
  • 通用性强,但效率较低

解法3:哈希表法

复制代码
class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.length() != t.length()) return false;
        unordered_map<char, int> map;
        for(char c : s) map[c]++;
        for(char c : t) map[c]--;
        for(auto val : map) {
            if(val.second != 0) return false;
        }
        return true;
    }
};

特点

  • 时间复杂度:O(n)
  • 空间复杂度:O(k),k为字符集大小
  • 适用于Unicode字符

第二题:202. 快乐数

解法1:快慢指针法(Floyd判圈)

复制代码
class Solution {
public:
    int bitSqureSum(int n) {
        int sum = 0;
        while(n > 0) {
            int bit = n % 10;
            sum += bit * bit;
            n = n / 10;
        }
        return sum;
    }
    
    bool isHappy(int n) {
        int slow = n, fast = n;
        while(true) {
            fast = bitSqureSum(fast);
            fast = bitSqureSum(fast);  // 快指针走两步
            slow = bitSqureSum(slow);  // 慢指针走一步
            
            if(fast == 1) return true;   // 找到快乐数
            if(fast == slow) return false; // 进入循环
        }
    }
};

特点

  • 时间复杂度:O(logn)
  • 空间复杂度:O(1)
  • 数学证明:非快乐数最终会进入循环

解法2:哈希表记录法

复制代码
class Solution {
public:
    int bitSqureSum(int n) {
        int sum = 0;
        while(n > 0) {
            int bit = n % 10;
            sum += bit * bit;
            n = n / 10;
        }
        return sum;
    }
    
    bool isHappy(int n) {
        unordered_map<int,int> map;
        map[n] = 1;
        while(n != 1) {
            n = bitSqureSum(n);
            if(map[n] == 1) return false; // 出现重复,进入循环
            map[n] = 1;
        }
        return true;
    }
};

特点

  • 时间复杂度:O(logn)
  • 空间复杂度:O(logn)
  • 思路直观,容易理解

第三题:349. 两个数组的交集

解法1:数组哈希法

复制代码
class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        int hash[10000] = {0};
        vector<int> ans;

        for(int i=0;i<nums1.size();i++) {
            hash[nums1[i]] = 1;
        }

        for(int i=0;i<nums2.size();i++) {
            if(hash[nums2[i]] == 1) {
                ans.push_back(nums2[i]);
                hash[nums2[i]] = 2;  // 标记为已添加,避免重复
            }
        }
        return ans;
    }
};

特点

  • 时间复杂度:O(m+n)
  • 空间复杂度:O(10000)
  • 适用于数值范围已知的情况

解法2:双哈希表法

复制代码
class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> map, quchong;
        vector<int> ans;
        for(int a : nums1) {
            map[a] = 1;
        }
        for(int a : nums2) {
            if(map[a] && quchong[a] != 1) {
                quchong[a] = 1;
                ans.push_back(a);
            }
        }
        return ans;
    }
};

特点

  • 时间复杂度:O(m+n)
  • 空间复杂度:O(m)
  • 通用性强,适用于任意整数

第四题:1. 两数之和

解法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++) {
            if(map.count(target - nums[i]))
                return {i, map[target - nums[i]]};
            map[nums[i]] = i;
        }
        return {};
    }
};

特点

  • 时间复杂度:O(n)
  • 空间复杂度:O(n)
  • 一次遍历,边查边存

解法2:暴力枚举法

复制代码
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    for(int i=0;i<numsSize;i++){
        for(int j=i+1;j<numsSize;j++){
            if(nums[i]+nums[j]==target){
                *returnSize=2;
                int *ans=(int*)malloc(sizeof(int)*2);
                ans[0]=i;ans[1]=j;
                return ans;
            }
        }
    }
    *returnSize=0;
    return NULL;   
}

特点

  • 时间复杂度:O(n²)
  • 空间复杂度:O(1)
  • 思路简单,但效率低

核心技巧总结

技巧 适用场景 代表题目
数组计数 字符统计、数值范围固定 有效的字母异位词
快慢指针 循环检测、链表问题 快乐数、环形链表
哈希表记录 查找、去重、计数 两数之和、交集
排序+双指针 需要有序处理的场景 三数之和等

选择建议

  1. 字符/小范围数值 → 数组计数法
  2. 循环检测问题 → 快慢指针法
  3. 通用查找问题 → 哈希表法
  4. 简单场景 → 暴力法(小数据量)
相关推荐
2401_841495649 小时前
【LeetCode刷题】跳跃游戏Ⅱ
数据结构·python·算法·leetcode·数组·贪心策略·跳跃游戏
leaves falling9 小时前
动态规划讲解
算法·动态规划
钓鱼的肝9 小时前
GESP系列(3级)小杨的储蓄
开发语言·数据结构·c++·笔记·算法·gesp
MicroTech20259 小时前
MLGO微算法科技推出人工智能与量子计算融合新成果:基于QLSS与LCHS的量子DPM算法技术
人工智能·科技·算法
AndrewHZ9 小时前
【图像处理基石】[特殊字符]圣诞特辑:10+经典图像处理算法,让你的图片充满节日氛围感!
图像处理·人工智能·opencv·算法·计算机视觉·stable diffusion·节日氛围感
艾醒9 小时前
大模型原理剖析——矩阵吸收优化:LLM推理加速的核心原理与实践
算法
艾醒10 小时前
大模型原理剖析——多头并行 + 潜变量协同:原理、应用与部署优化
算法
KingRumn10 小时前
Linux信号之信号安全
linux·算法
智驱力人工智能10 小时前
从合规到习惯 海上作业未穿救生衣AI识别系统的工程实践与体系价值 未穿救生衣检测 AI救生衣状态识别 边缘计算救生衣监测设备
人工智能·深度学习·opencv·算法·目标检测·边缘计算
猎板PCB黄浩10 小时前
高多层线路板工厂专业选型指南:全流程评估体系与猎板适配场景解析
大数据·人工智能·算法·pcb