LeetCode – Refresh – Anagrams

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<string> anagrams(vector<string> &strs) {
 4         vector<string> result;
 5         int len = strs.size();
 6         if (len < 2) return result;
 7         unordered_map<string, int> 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 };
时间: 2024-10-16 17:43:41

LeetCode – Refresh – Anagrams的相关文章

LeetCode - Refresh - Pascal&#39;s Triangle II

Exact same as I. 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 if (rowIndex < 0) return vector<int> (); 5 vector<int> result(1, 1); 6 for (int i = 1; i <= rowIndex; i++) { 7 for (int j = result.size()-1; j >

LeetCode - Refresh - Pascal&#39;s Triangle

This is simple. Just everything use current[j] += current[j-1]. But leave the first one to be "1". Then add another "1" to end. 1 class Solution { 2 public: 3 vector<vector<int> > generate(int numRows) { 4 vector<vector&

[LeetCode][Java] Anagrams

题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题意: 给定一个字符串数组,返回所有的易位构词组合. 注意:所有的输入都是小写. 算法分析: 易位构词其实也很好理解,就是两个单词所包含的字符和数量都是一样的,只是顺序不同 对字符串中各字母进行排序,那么互为重排列的字符串就会相等. 按照上述思路,用一个map纪

Java for LeetCode 049 Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 解题思路:首先要理解,什么是anagrams,ie.“tea”.“tae”.“aet”,然后就十分好做了,new一个hashmap,使用一个排过序的String作为key,重复了就往里面添加元素,这里出现一个小插曲,就是排序的时候不要用PriorityQueue,因为P

leetcode 49. Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. [Solution] FOR ANY string st IN strs, 1) sort st 2) hash st, since st is sorted, all anagrams will be hash to one value. 1 vector<strin

【leetcode】Anagrams (middle)

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. anagrams 的意思是两个词用相同的字母组成  比如 “dog" "god" 思路: 把单词排序 如 dog 按字母排序变为 dgo 用unordered_map<string, int> 记录排序后序列第一次出现时,字符串在输入st

LeetCode 049 Anagrams

题目要求:Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 分析: 参考网址:http://www.cnblogs.com/easonliu/p/3643595.html 代码如下: class Solution { public: vector<string> anagrams(vector<

【leetcode】Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. Anagrams:即字母个数和字母都相同,但是字母顺序不相同的词 e.g. "tea","and","ate","eat","dan".   return &qu

LeetCode – Refresh – Maximum Gap

Sorting solution O(nlogn): 1 class Solution { 2 public: 3 int maximumGap(vector<int> &num) { 4 int len = num.size(), result = 0; 5 if (len < 2) return 0; 6 sort(num.begin(), num.end()); 7 for (int i = 0; i < len-1; i++){ 8 result = max(res