https://oj.leetcode.com/problems/anagrams/

在一个vector<string>中,找到所有经过顺序变换,可以变成一样的 string.

首先,对每个 string 排序,这样它的顺序就是 abcd 相当于做了一个统一。

然后,对vector排序,这样,如果有重复的,则必相邻,相当于找重复的那步从 n*n,简化到 nlogn.

但是,在这个过程中,需要知道原来字符串的样子,所以,使用了额外空间。

class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> ans;
if(strs.size() == || strs.size() == )
return ans;
//sort every string
vector<string> str_copy = strs;
for(int i = ;i<strs.size();i++)
sort(str_copy[i].begin(),str_copy[i].end()); //sort the whole vector
vector<string> str_copy_copy = str_copy;
sort(str_copy_copy.begin(),str_copy_copy.end()); for(int i = ; i<strs.size();i++)
{
if(str_copy_copy[i] == str_copy_copy[i-])
{
//find all strings equal to it, and record its original string
for(int j = ;j<str_copy.size();j++)
{
if(str_copy_copy[i] == str_copy[j])
ans.push_back(strs[j]);
}
}
//to avoid duplicated compare and search, skip the same ones
while(i<strs.size())
if(str_copy_copy[i] == str_copy_copy[i-])
i++;
else
break;
}
return ans; }
};

LeetCode OJ--Anagrams **的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  5. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  7. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  8. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  10. LeetCode OJ 49. Group Anagrams

    题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...

随机推荐

  1. 2 Model层-模型成员

    1 类的属性 objects:是Manager类型的对象,用于与数据库进行交互 当定义模型类时没有指定管理器,则Django会为模型类提供一个名为objects的管理器 支持明确指定模型类的管理器 c ...

  2. 【Word Ladder II】cpp

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  3. Unable to execute dex: Multiple dex files define 问题

    今天在run公司的android project时候,报这个错误. 1. Clean Project, 重启Eclipse 没有解决. 2. 看到别人遇到的相同错误,解决方法如下: http://bl ...

  4. nyoj 题目20 吝啬的国度

    吝啬的国度 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市, ...

  5. 201621123034 《Java程序设计》第2周学习总结

    1. 本周学习总结 本周学习了基本数据类型.包装类,自动装箱与自动拆箱.数组.ArrayList.包装类可以更加方便的转换基本数据类型,而其存放的是对象的引用,而非对象本身,在对其内容进行比较时,要使 ...

  6. jQuery操作DOM基础 - 元素属性的查看与设置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 人类基因(human)

    题目描述 上了大学之后,小W和小Z一起报了一门三宝课,在实践课上遇到了一些问题. 一条染色体上有nn对基因,每种基因都可以用一个数ai来表示,作为标号. 现在,在其中一条染色单体上某基因发生了突变. ...

  8. 基于深度摄像头的障碍物检测(realsense+opencv)

    前几天老大给了个任务,让我帮slam组写一个基于深度摄像头的障碍物检测,捣鼓了两天弄出来了,效果还不错,就在这里记一下了. 代码的核心思路是首先通过二值化,将一米之外的安全距离置零不考虑,然后通过开运 ...

  9. kubernetes-dashboard

    1.导入kubernetes-dashboard 镜像 [root@node1 DNS]# docker load < kube-dashboard.tar 6bc90c4dba69: Load ...

  10. CSS Modules使用方法

    css modules调研 css模块化解决方案 抛弃css (Radium,jsxstyle,react-style) 利用js来管理样式依赖(css Modules) css模块化面临的问题 全局 ...