博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode – Refresh – Anagrams
阅读量:7022 次
发布时间:2019-06-28

本文共 988 字,大约阅读时间需要 3 分钟。

Use a hash table to record it. The tricky part is that when the hash value is > 0, it is the index + 1. Otherwise, that string already exists in result list.

 

1 class Solution { 2 public: 3     vector
anagrams(vector
&strs) { 4 vector
result; 5 int len = strs.size(); 6 if (len < 2) return result; 7 unordered_map
mapping; 8 for (int i = 0; i < len; i++) { 9 string tmp = strs[i];10 sort(tmp.begin(), tmp.end());11 if (mapping[tmp]) {12 if (mapping[tmp] > 0) {13 result.push_back(strs[mapping[tmp]-1]);14 mapping[tmp] = -1;15 }16 result.push_back(strs[i]);17 } else {18 mapping[tmp] = i+1;19 }20 }21 return result;22 }23 };

 

转载于:https://www.cnblogs.com/shuashuashua/p/4346158.html

你可能感兴趣的文章
js 图表插件 chartjs 2.4
查看>>
Python之登录
查看>>
SVG.js Mask覆盖和ClipPath裁剪
查看>>
Python多线程之线程创建和终止
查看>>
通过JS语句判断WEB网站的访问端是电脑还是手机
查看>>
(8) iphone 开发 数据传递 : 02 页面切换与数据的反向传递
查看>>
LPC3250 External Memory Controller
查看>>
MySQL内存表的弊端
查看>>
使用SIMILE Timeline 将邮件“事件”可视化
查看>>
SQL:创建某一时间段内的周末日期表以及特殊处理日期表
查看>>
什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?(转)
查看>>
LindAgile.SchedulingTask~设计一个不错的任务调度组件
查看>>
恶搞之手机垃圾信息发送器 手机短信骚扰器
查看>>
mysql replication之binlog-do-db、binlog-ignore-db
查看>>
Date类型和Long类型的相互转换
查看>>
XMPP协议
查看>>
CSS:给 input 中 type="text" 设置CSS样式
查看>>
Softmax函数
查看>>
hdu4462 Scaring the Birds
查看>>
设计中的道理_6
查看>>