LeetCode OJ--Anagrams **
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 **的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- 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 ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode OJ 49. Group Anagrams
题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...
随机推荐
- 致敬wusir懒孩子自有懒孩子的生存之道之二
https://www.cnblogs.com/wupeiqi/ https://www.cnblogs.com/Eva-J/ https://www.cnblogs.com/wupeiqi/p/90 ...
- [Python]有关pygame库中的flip和update的区别
pygame.display.flip()和pygame.display.update()的用法上的区别: 资料一. 资料二. (资料最后更新时间:2019年1月9日)
- CSAPP 缓冲区溢出试验
缓冲区溢出试验是CSAPP课后试验之一,目的是: 更好的理解什么是缓冲区溢出 如何攻击带有缓冲区溢出漏洞的程序 如何编写出更加安全的代码 了解并理解编译器和操作系统为了让程序更加安全而提供的几种特性 ...
- Codeforces Round #460 (Div. 2)-A. Supermarket
A. Supermarket time limit per test2 seconds memory limit per test256 megabytes Problem Description W ...
- 动态规划:HDU-1398-Square Coins(母函数模板)
解题心得: 1.其实此题有两种做法,动态规划,母函数.个人更喜欢使用动态规划来做,也可以直接套母函数的模板 Square Coins Time Limit: 2000/1000 MS (Java/Ot ...
- go经典练习题涉及流程控制-字符串-struct-map的数据类型的处理
one:求1到100之间的质数 package main import ( "fmt" ) func isPrime(n int) bool { var flag = true f ...
- Spring MVC学习总结
Spring MVC学习总结 Spring MVC学习路(一) 下载配置文件 Spring MVC学习路(二) 设置配置文件 Spring MVC学习路(三) 编写第一个demo Spring MVC ...
- Autofac Mvc5 Nuget
Autofac 3.5.2 Install-Package Autofac -Version 3.5.2 Autofac ASP.NET MVC 5 Integration 3.3.3 Install ...
- CentOS7 haproxy+keepalived实现高可用集群搭建
一.搭建环境 CentOS7 64位 Keepalived 1.3.5 Haproxy 1.5.18 后端负载主机:192.168.166.21 192.168.166.22 两台节点上安装rabbi ...
- outlook同步异常
新装的系统,备份了outlook,还原后发现,outlook还在不停的同步服务端邮件,设置规则,禁止接收今天之前的邮件,但是outloock还是在同步,只是不接收而已,这样导致了莫名其妙的异常错误,o ...