LeetCode-Hot100-002字母异位词

news/2025/3/1 13:08:53/

使用手写的hash,把每个字母的ascii码加起来再模,来定位hash表的索引。

不懂可以在评论区问我,正常题解可以参照leetcode官方题解

class Hash{
public:static const int MAXN = 10007;int size;vector<list<pair<string, int>>> table;Hash(): size(0), table(MAXN) {}bool check(string str1, string str2)  //检查是否为字母异位词{if(str1.length()!=str2.length()) {return false;}int count1[26] = {0}; int count2[26] = {0};for(int i = 0; i<str1.size(); i++){count1[str1[i]-'a']++;}for(int i = 0; i<str2.size(); i++){count2[str2[i]-'a']++;}for(int i = 0; i<26; i++){if(count1[i]!=count2[i]){return false;}}return true;}int f(std::string str){int sum = 0;for(auto ch: str){sum += ch-'a';}return sum%MAXN;}int& operator[](string key){int index = f(key);auto& bucket = table[index];for(auto& p: bucket){if(check(p.first, key)){return p.second;}}bucket.emplace_back(key, size);size++;return (--bucket.end())->second;}int find(string key){int index = f(key);auto& bucket = table[index];for(auto& p: bucket){if(check(p.first, key)){return p.second;}}return -1;}
};class Solution {
public:vector<vector<string>> groupAnagrams(vector<string>& strs) {Hash hash;vector<vector<string>> ans;for(auto& str: strs){if(hash.find(str)==-1){hash[str];ans.push_back(vector<string>{str});}else{ans[hash[str]].push_back(str);}}return ans;}
};

http://www.ppmy.cn/news/1575772.html

相关文章

Java进阶——注解一文全懂

Java注解&#xff08;Annotation&#xff09;是一种强大的元数据机制&#xff0c;为代码提供了附加信息&#xff0c;能简化配置、增强代码的可读性和可维护性。本文将深入探讨 Java 注解的相关知识。首先阐述了注解的基础概念&#xff0c;包括其本质、作用以及核心分类&#xf…

云计算相关

**云计算**是一种通过互联网提供计算资源和服务的模式&#xff0c;用户可以根据需求动态获取计算能力、存储、网络等资源&#xff0c;而无需管理底层基础设施。以下是云计算的详细介绍&#xff1a; ### 1. **核心概念** - **按需服务**: 用户根据需求获取资源&#xff0c;按使用…

【Linux第一弹】Linux基础指令(上)

目录 1.ls指令 1.1 ls使用实例 2.pwd指令 3.cd指令 3.1 cd使用实例 4.touch指令 4.1touch使用实例 5.mkdir指令 5.1mkdir使用实例 6.rmdir指令和rm指令 6.1 rmdir指令使用实例->: 6.2 rm指令使用实例 7.man指令 8.cp指令 8.1 cp 使用实例 9.mv指令 9.1mv使用…

Typora的Github主题美化

[!note] Typora的Github主题进行一些自己喜欢的修改&#xff0c;主要包括&#xff1a;字体、代码块、表格样式 美化前&#xff1a; 美化后&#xff1a; 一、字体更换 之前便看上了「中文网字计划」的「朱雀仿宋」字体&#xff0c;于是一直想更换字体&#xff0c;奈何自己拖延症…

实践教程:使用DeepSeek实现PDF转Word的高效方案

&#x1f388;Deepseek推荐工具 PDF文件因其跨平台、格式稳定的特性被广泛使用&#xff0c;但在内容编辑场景中&#xff0c;用户常需将PDF转换为可编辑的Word文档。传统的付费工具&#xff08;如Adobe Acrobat&#xff09;或在线转换平台存在成本高、隐私风险等问题。本文将使…

win11本地部署deepseek大模型(安装ollama+docker+open-webui)最终实现自己的项目可通过API调用投喂数据后的模型

硬件配置&#xff1a;笔记本win11&#xff0c;内存32G&#xff0c;CPU锐龙7 &#xff0c;无独显&#xff1b;只能考虑deepseek-r1:1.5b模型。 第一步&#xff1a;安装Ollama 此处不过多累赘了&#xff0c;https://ollama.com/官网选择对应的系统版本下载即可。 需要注意的是…

JavaScript系列(94)--Serverless实践

Serverless实践 ☁️ Serverless&#xff08;无服务器&#xff09;架构是云计算的一种新范式&#xff0c;它让开发者专注于业务逻辑而无需关心服务器运维。本文将详细介绍前端开发中的Serverless实践方案。 Serverless概述 &#x1f31f; &#x1f4a1; 小知识&#xff1a;Se…

【Git】解决 GitLab “Ensure URL is HTTPs“ 拉取的问题

问题背景 在使用 Git 拉取代码时&#xff0c;您可能遇到过以下错误信息&#xff1a; git pull origin dev_2.4.6 fatal: Unencrypted HTTP is not supported for GitHub. Ensure the repository remote URL is using HTTPS.这个错误提示实际上反映了一个重要的安全更新&#…