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 ...
随机推荐
- CS对于dll文件的引用
1.对于.net或者com型的组件,直接添加引用即可. 2.对于标准的dll文件,利用DLLImport这个函数即可,如果要使用函数的别名,那么应该加入EntryPoint. 示例:一般可以封装成一个 ...
- Cocoa Touch的3种类的交流方式delegate/target/notification
1. Delegate 在Cocoa Touch类对象运行的周期中,某一个时间点它会去调用一些指定类的指定函数来完成他自身所要完成的功能.这个”指定的类”,就称为这个类的委托类.”指定函数”则是一些在 ...
- View的setOnClickListener的添加方法
1)第一种,也是最长见的添加方法(一下都以Button为例) 1 Button btn = (Button) findViewById(R.id.myButton);2 btn .setOnClick ...
- Swiper之滑块2
对比之前Swiper滑块1来说,我们添加一下背景颜色来看看: <!DOCTYPE html> <html> <head> <meta http-equiv=& ...
- 用实体框架搭建MVC程序框架(全部)
第一步:1.新建项目 2.新建domain类库 3.新建Data类库 4.为上面的1.2.3添加实体框架nuget包.(可以右键管理nuget包来查找entityframework,当然也可以通过程序 ...
- VSS汉化后出现问题及解决方法
1.vss汉化后在获取项目时不递归获取? 解决方法:工具-->选项-->常规-->勾选 项目递归时有效(R) 2.查看差异,可视化窗口未出现分隔? 解决方法:差异选项-->格式 ...
- 20171107--SQL变量,运算符,存储过程
create database shujuku03 go use shujuku03 go create table jiaoshi--创建jiaoshi表-- ( code int primary ...
- JAVA多线程通信
JAVA多线程通信 package com.frank.thread; /** * author:pengyan * date:Jun 16, 2011 * file:ProducerAndCusto ...
- nyoj_t218(Dinner)
描述 Little A is one member of ACM team. He had just won the gold in World Final. To celebrate, he dec ...
- FTP 数字代码的意义
110 重新启动标记应答. 120 服务在多久时间内ready. 125 数据链路埠开启,准备传送. 150 文件状态正常,开启数据连接端口. 200 命令执行成功. 202 命令执行失败. 211 ...