zoukankan      html  css  js  c++  java
  • Careercup

    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面试题了。原谅我的吐槽,因为耐着性子做了那么多难度不小的假面试题,实在感觉很窝火,但我仍然相信这些题目能达到练习目的的。

    代码:

     1 // http://www.careercup.com/question?id=5732809947742208
     2 #include <cstring>
     3 #include <string>
     4 #include <unordered_set>
     5 using namespace std;
     6 
     7 class Solution {
     8 public:
     9     int longestWordInDictionary(unordered_set<string> &dictionary, string letters) {
    10         int i;
    11         int max_len;
    12         int word_len;
    13         
    14         memset(c1, 0, MAXLEN * sizeof(int));
    15         word_len = (int)letters.length();
    16         for (i = 0; i < word_len; ++i) {
    17             ++c1[letters[i]];
    18         }
    19         
    20         max_len = 0;
    21         unordered_set<string>::iterator it;
    22         for (it = dictionary.begin(); it != dictionary.end(); ++it) {
    23             word_len = (int)(*it).length();
    24             if (word_len > (int)letters.length()) {
    25                 continue;
    26             }
    27             
    28             memcpy(c2, c1, MAXLEN * sizeof(int));
    29             for (i = 0; i < word_len; ++i) {
    30                 if (c2[(*it)[i]] == 0) {
    31                     break;
    32                 }
    33                 --c2[(*it)[i]];
    34             }
    35             if (i == word_len) {
    36                 max_len = word_len > max_len ? word_len : max_len;
    37             }
    38         }
    39         
    40         return max_len;
    41     };
    42 private:
    43     const int MAXLEN
    44     int c1[MAXLEN];
    45     int c2[MAXLEN];
    46 };
  • 相关阅读:
    反射详解六
    反射详解五
    反射详解四
    反射详解三
    反射详解二
    mysql行转列
    mysql增删改查
    视图的使用
    js过滤
    错误集
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3705726.html
Copyright © 2011-2022 走看看