Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59
原题:
Group Anagrams
input = ["star, astr, car, rac, st"]
output = [["star, astr"],["car","rac"],["st"]);
题目:给定一堆字符串,设法把anagram都排在一起。
解法:自定义一个comparator,互为anagram的两个字符串在排好序以后是相同的。根据这个规则可以写出个效率不怎么高,但是代码很短的算法。
代码:
// http://www.careercup.com/question?id=5733320654585856
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std; string ss1, ss2;
bool comparator(const string &s1, const string &s2)
{
ss1 = s1;
ss2 = s2; sort(ss1.begin(), ss1.end());
sort(ss2.begin(), ss2.end()); return ss1 < ss2;
} int main()
{
int i;
int n;
vector<string> v; while (cin >> n && n > ) {
v.resize(n);
for (i = ; i < n; ++i) {
cin >> v[i];
}
sort(v.begin(), v.end(), comparator);
cout << "{" << endl;
for (i = ; i < (int)v.size(); ++i) {
cout << " " << v[i] << endl;
}
cout << "}" << endl;
v.clear();
} return ;
}
Careercup - Facebook面试题 - 5733320654585856的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
- Careercup - Facebook面试题 - 5188884744896512
2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...
随机推荐
- django 学习-16 Django会话Cookie
1.django.admin.py startproject cs3 cd cs3 django.admin.py startapp blog 2. vim urls.py url ...
- 收集一些常用的php正则表达式
1. 平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: 2. "^\d+$" //非负整数(正整数 + 0) 3. "^[0 ...
- IE6的position:fixed
手头一个项目中,要实现把一个浮层控制在浏览器窗口右下角,用”position:fixed”来控制最合适不过了. 但万恶的IE6不支持这个属性,之前采用过的方法有:将滚动条转移到body上,使用绝对定位 ...
- OpenFiler安装与基本配置
一. 安装篇 1. 插入安装盘 2. 选择键盘输出 3. 对硬盘进行分区 4. 删除所有数据并重新分区 5. 配置IP地址等信息 ...
- 用LINQ在集合中查询特定对象
这里是原文出处: 简单的概括LINQ LINQ是Language-Integrated Query的缩写,是C# 3.0和VB 9.0中新加入的语言特性,可以在编程时使用内置的查询语言进行基于集合的操 ...
- UIAlertView[警告框] [代理协议型]UIActionSheet [表单视图][代理协议型]
//// ViewController.h// UIAlertViewAndUIActionSheet//// Created by hehe on 15/9/21.// Copyright ...
- C语言知识总结(5)
预处理指令 C语言提供的预处理指令主要有:宏定义.文件包含.条件编译 宏定义 不带参数的宏定义 1>一般形式 #define 宏名 字符串 比如#define A 10 2>作用 它的作用 ...
- UML类图几种关系的总结[转]
原文地址:http://www.open-open.com/lib/view/open1328059700311.html 在UML类图中,常见的有以下几种关系: 泛化(Generalization) ...
- HTML5标准终于来了,看什么书学习最好??????
最近看了一本书<HTML5网页开发实例详解>,是大众点评的攻城狮写的,觉得很有收获,看样子目前大多数的国内网页都支持HTML5了,全栈工程师是不是必须得会HTML5? 有兴趣的可以讨论呀, ...
- 《CSS3秘笈》备忘录
第一部分 1. 类名称区分大小写:.special和.SPECIAL不一样 2. :focus 是通过单击或跳格集中在某个地方 3. ::selection 没有单冒号,被选中的文本[ 但是在I ...