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. django_数据库操作—增、删、改、查

    增加 增加数据有两种方法 1> sava >>> from datetime import date >>> book = BookInfo( btitle= ...

  2. A1095 Cars on Campus (30)(30 分)

    A1095 Cars on Campus (30)(30 分) Zhejiang University has 6 campuses and a lot of gates. From each gat ...

  3. main方法中sleep

    sleep(long mil); 你应该 加 休眠的时间才行 一.Thread.sleep(1000); 二.让你的class extends Thread来继承 sleep方法

  4. ActiveMQ初步学习

    本文主要参考张丰哲大神的简书文章,链接 https://www.jianshu.com/p/ecdc6eab554c JMS,即Java Message Service,通过面向消息中间件(MOM:M ...

  5. JWT实现token认证

    演变 1.1 有状态服务 在原来的单体系统中,有状态服务,即服务端需要记录每次会话的客户端信息,从而识别客户端身份,根据用户身份进行请求的处理,session+cookie.在如今的大多数分布式系统中 ...

  6. 一个iOS程序员眼中的跨域问题

    摘要: 跨域问题是web开发领域一个常见的问题,相信每个web开发者都遇到"跨域"的问题 最近公司的iOS开发任务比较少,所以自己最近开始了Web开发的任务,在用H5做了很多页面, ...

  7. rsync 使用小记

    工作中遇到了有关rsync使用的问题,在这里记录下供有同样需求的人参考一下 先说下环境 服务端配置 pid file = /rsyncdata/rsyncd.pid port = 873 addres ...

  8. 习题:Wormhole(思路题)

    tyvj1763 描述 一维的世界就是一个数轴.这个世界的狭小我们几乎无法想象.在这个数轴上,有N个点.从左到右依次标记为点1到N.第i个点的坐标为Xi.经过漫长时间的物理变化和化学变化,这个一维世界 ...

  9. 【bzoj1520】[POI2006]Szk-Schools 费用流

    题目描述 输入 输出 如果有可行解, 输出最小代价,否则输出NIE. 样例输入 5 1 1 2 3 1 1 5 1 3 2 5 5 4 1 5 10 3 3 3 1 样例输出 9 题解 费用流 设xi ...

  10. 关于jena-fuseki SPARQL query版本问题的解决方案

    在做“Apache jena SPARQL endpoint及推理”时,遇到了不少问题,主要原因是jena-fuseki版本更新了.以下对问题解决方案做下笔记.想了解更多,请查阅底部参考文献. Que ...