Careercup - Google面试题 - 5732809947742208
2014-05-03 22:10
原题:
Given a dictionary, and a list of letters ( or consider as a string), find the longest word that only uses letters from the string. [I didn't meet this question, what's the best solution?]
题目:给定一个字符串,和一个字典。从字典中找出一个最长的词,并且这个词只使用字符串里的字母。
解法:此人说这题不是自己遇到的,不是自己的面试题你贴它干嘛,难不成又是从别处copy来的?Careercup由于这些复制粘贴党的存在基本已经没有参考价值了。就算是复制粘贴,能否把题目描述清楚!什么叫只用字符串里的字母,能多吗?能少吗?必须刚刚好吗?由于题目信息不足,我姑且认为只要一个单词的字母完全被参照字符串包含,就是满足条件了。在Google题目区我选取了30题来练手,这其中至少有一半是假的。有个发帖者叫“Guy”,如果我有幸见到他本人,我会亲自揍他一顿。因为我明知他发布的一堆Google面试题都是抄来的,还是耐着性子做完了。面试题本身都是好题,但把别人的题目抄来自己发帖就不好了。Careercup这个网站上有很多代码功底很强的人,偶尔能看到他们发言都有茅塞顿开的感觉。但很遗憾,也有很多代码风格极差却经常活跃发言,还大量发布面试题的人。这样的人能够发布大量面试题,只有可能是他们又在geek-for-geek或者glassdoor之类的网站上看见别的题了。他们连on-site之前的电面都不可能通过的,更不用说on-site面试题了。原谅我的吐槽,因为耐着性子做了那么多难度不小的假面试题,实在感觉很窝火,但我仍然相信这些题目能达到练习目的的。
代码:
// http://www.careercup.com/question?id=5732809947742208
#include <cstring>
#include <string>
#include <unordered_set>
using namespace std; class Solution {
public:
int longestWordInDictionary(unordered_set<string> &dictionary, string letters) {
int i;
int max_len;
int word_len; memset(c1, , MAXLEN * sizeof(int));
word_len = (int)letters.length();
for (i = ; i < word_len; ++i) {
++c1[letters[i]];
} max_len = ;
unordered_set<string>::iterator it;
for (it = dictionary.begin(); it != dictionary.end(); ++it) {
word_len = (int)(*it).length();
if (word_len > (int)letters.length()) {
continue;
} memcpy(c2, c1, MAXLEN * sizeof(int));
for (i = ; i < word_len; ++i) {
if (c2[(*it)[i]] == ) {
break;
}
--c2[(*it)[i]];
}
if (i == word_len) {
max_len = word_len > max_len ? word_len : max_len;
}
} return max_len;
};
private:
const int MAXLEN
int c1[MAXLEN];
int c2[MAXLEN];
};
Careercup - Google面试题 - 5732809947742208的更多相关文章
- Careercup - Google面试题 - 5085331422445568
2014-05-08 23:45 题目链接 原题: How would you use Dijkstra's algorithm to solve travel salesman problem, w ...
- Careercup - Google面试题 - 4847954317803520
2014-05-08 21:33 题目链接 原题: largest number that an int variable can fit given a memory of certain size ...
- Careercup - Google面试题 - 6332750214725632
2014-05-06 10:18 题目链接 原题: Given a ,) (,) (,), (,) should be returned. Some suggest to use Interval T ...
- Careercup - Google面试题 - 5634470967246848
2014-05-06 07:11 题目链接 原题: Find a shortest path ,) to (N,N), assume is destination, use memorization ...
- Careercup - Google面试题 - 5680330589601792
2014-05-08 23:18 题目链接 原题: If you have data coming in rapid succession what is the best way of dealin ...
- Careercup - Google面试题 - 5424071030341632
2014-05-08 22:55 题目链接 原题: Given a list of strings. Produce a list of the longest common suffixes. If ...
- Careercup - Google面试题 - 5377673471721472
2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...
- Careercup - Google面试题 - 6331648220069888
2014-05-08 22:27 题目链接 原题: What's the tracking algorithm of nearest location to some friends that are ...
- Careercup - Google面试题 - 5692127791022080
2014-05-08 22:09 题目链接 原题: Implement a class to create timer object in OOP 题目:用OOP思想设计一个计时器类. 解法:我根据自 ...
随机推荐
- Part 8 Coalesce function in sql server
- JavaScript之返回顶部
为了弄这个Hexo,今天又是坐在电脑面前待了一天( ⊙ o ⊙ ),老是出问题,在百度上也试验了很多方法,还是没弄好,诶...身心疲惫甚是乏累啊~~~ 算了,这个Hexo先不弄,还是安分点吧,在Hex ...
- 关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究
关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究 测试代码:http://git.oschina.net/Xiyue/TabBarItem_TEST 简 ...
- 网站的配置文件XML读写
网站的配置信息一般都写入到XML中,以下是简单的对xml的读写操作,仅供参考. 读操作: XmlDocument xmlDoc = new XmlDocument(); XmlReaderSettin ...
- 8款功能强大的最新HTML5特效实例
1.HTML5 Canvas画板画图工具 可定义笔刷和画布 今天要分享的这款HTML5 Canvas画图工具就可以简单实现网络画图的功能,我们可以自定义笔刷的类型.粗细.颜色,也可以定义画布的大小和背 ...
- Mybatis-Generator插件自动生成Dao、Model、Mapping相关文件
最近做项目,mapping 有点多而且容易写错,于是试着用了Mybatis-Generator 插件自动生成 dao, domain mapping 文件.感觉还挺好用.把相关配置分享,一边以后做项 ...
- CICS&&XA
CICS (Customer Information Control System) 是IBM 公司的强大主机交易服务器.集成平台,在全球C.C++.COBOL等交易中间件市场上占有绝大多数客户.CI ...
- 工厂方法(Factory Method)模式
一.工厂方法(Factory Method)模式 工厂方法(FactoryMethod)模式是类的创建模式,其用意是定义一个创建产品对象的工厂接口,将实际创建工作推迟的子类中. 工厂方法模式是简单工厂 ...
- 【风马一族_xml】xml编程
xml编程:利用java程序支增删改查(CRUD)XML中的数据 解析思想: dom解析 sax解析 基于这两种解析思想市面上就有了很多的解析api sun jaxp (比较弱)既有dom方式也有sa ...
- 防止双击选中html中文字
在开发过程中很常用的会给<span></span>等内联元素增加一个onlick事件,但是经常发生的一件事情就是点击的时候,选中了span中的字体:倒是不影响主逻辑,但是很难受 ...