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 ...
随机推荐
- 闯越自动签到demo版补充说明
demo代码:https://www.cnblogs.com/canmeng/p/11000548.html 定位出错是由于cookie 我重新登录账号过,cookies的值就变了 当时没注意cook ...
- C++多态实例
#include <iostream> #include <string> using namespace std; //class 实现 class Employee { s ...
- Linux命令之---diff
命令介绍 diff命令可以酌行比较纯文本文件内的内容,并输出文件的差异. 命令格式 diff [option] [file1] [file2] 举例子 1)比较俩文本文件 [root@king ~]# ...
- Python协程详解(一)
yield有两个意思,一个是生产,一个是退让,对于Python生成器的yield来说,这两个含义都成立.yield这个关键字,既可以在生成器中产生一个值,传输给调用方,同时也可以从调用方那获取一个值, ...
- Django 六——自定义标签、图片验证码、发送邮件、评论树、组合搜索
1.自定义标签 2.图片验证码 3.生成邮箱验证码.发送邮件 4.评论树实现 5.组合搜索(Q) 1.自定义标签 配置: a.在app中新建文件夹 templatetags,里面新建 xx.py文 ...
- VC下如何调用控制台命令以及其他可执行文件
开始的时候想写一个基于MFC的Wifi开关控制程序,可是不知道VC中如何调用控制台命令,经过网上的学习,发现其实挺挺简单也挺好用.于是制作了一个简单的基于MFC个人助理小软件,可以点击按钮实现Wifi ...
- IOS开发学习笔记038-autolayout 自动布局 界面实现
在storyboard/xib文件中实现自动布局 autolayout 1.注意事项 autolayout和frame属性是有冲突的,所以如果准备使用autolayout,就不要再代码中对控件的fra ...
- algorithm 头文件
非修改性序列操作(12个) 循环 对序列中的每个元素执行某操作 for_each() 查找 在序列中找出某个值的第一次出现的位置 find() 在序列中找出符合某谓词的第一个元素 find_if() ...
- Summary—【base】(JavaScript)
1.认识Js 1.1 Js就是一门运行在客户端浏览器的脚本编程语言 1.2 组成 ECMAScript:Js的语法标准 DOM:JS操作网页 ...
- hibernate级联查询映射的两种方式
Hibernate主要支持两种查询方式:HQL查询和Criteria查询.前者应用较为广发,后者也只是调用封装好的接口. 现在有一个问题,就是实现多表连接查询,且查询结果集不与任何一个实体类对应,怎么 ...