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 4 AngularJS ng src directive
- Part 82 to 85 Talking about Generic queue, stack collection class
Part 82 Generic queue collection class Part 83 Generic stack collection class Part 84 Real tim ...
- linux中重置服务器的mysql用户密码
本文章前提条件是自己经把mysql登录密码给忘记了,这个时间我们解决方法有很多,重新安装mysql数据库一切重来,另一种是通过下面文章重新设置root密码,下面我们一起来看看方法二吧. 最 近 ...
- 10款经典的web前端特效的预览及源码
1.CSS3响应式导航菜单 今天我给大家介绍一下如何使用纯CSS来实现的一个响应式导航菜单,我们使用的是HTML5+CSS3技术,当浏览器窗口变小或者使用手机浏览器访问的时候,原本横条菜单会收缩成一个 ...
- POD数据了解
Plain old data (普通旧的数据); POD 是Plain Old Data的簡寫,是指一些系統的int, char, float.指標.array之類的資料型別,這應該蠻好想像的,就是C ...
- 使用DriverManager获取数据库连接的一个小改进
由于使用DriverManager获取数据库连接时,由于DriverManager实现类中有一段静态代码块,可以直接注册驱动,且可以同时管理多个驱动程序 所以当换数据库连接时需要指定不同的数据库,那么 ...
- zedboard OPENCV移植
1:系统环境搭建 要准备好交叉编译环境 见http://blog.csdn.net/xiabodan/article/details/22717175 2:下载cmake CMake是一个跨平台的安装 ...
- Linux下的vi编辑器与gcc工具的使用
最近在网上找了些视频,自学了一点Linux环境下,C编程的方法. 注 vi与vim是有区别的,vim打开的源码文件其中的关键字是有颜色的. vi编辑器有3种模式,命令行模式,插入模式,底行模式. 如果 ...
- 开始ubuntu 14.04 的装X模式---终端模式下中文输入,听歌,上irc 开启framebuffer看电影 截图
先上图吧 卡卡的全是在tty1 下的操作,看电影,听歌,截图 ,看图 ,上irc 等等,相当适合在小白面前装屁! 需要安装的软件: 为了能正常显示中文:安装fbterm sudo apt-get i ...
- 使用tortoise git管理gitolite版本库
gitolite-admin是用于管理git版本库的版本库,将其从服务器上clone下来. 使用tortoise git clone的时候需要指定私钥,私钥的格式是ppk的,需要使用putty的PUT ...