2022.3.17 720. 词典中最长的单词

早上好,我了——刷题PK赛来了,各位加油!

给出一个字符串数组 words 组成的一本英语词典。返回 words 中最长的一个单词,该单词是由 words 词典中其他单词逐步添加一个字母组成。

若其中有多个可行的答案,则返回答案中字典序最小的单词。若无答案,则返回空字符串。

示例 1:

输入:words = [“w”,“wo”,“wor”,“worl”, “world”]
输出:“world”
解释: 单词"world"可由"w", “wo”, “wor”, 和 "worl"逐步添加一个字母组成。
示例 2:

输入:words = [“a”, “banana”, “app”, “appl”, “ap”, “apply”, “apple”]
输出:“apple”
解释:“apply” 和 “apple” 都能由词典中的单词组成。但是 “apple” 的字典序小于 “apply”


题目很简单,对数组中每个单词进行遍历,判断就行,可能效率也还行。或者直接字典树,放字典树代码吧


class Trie {
public:
    Trie() {
        this->children = vector<Trie *>(26, nullptr);
        this->isEnd = false;
    }
    
    bool insert(const string & word) {
        Trie * node = this;
        for (const auto & ch : word) {
            int index = ch - 'a';
            if (node->children[index] == nullptr) {
                node->children[index] = new Trie();
            }
            node = node->children[index];
        }
        node->isEnd = true;
        return true;
    }

    bool search(const string & word) {
        Trie * node = this;
        for (const auto & ch : word) {
            int index = ch - 'a';
            if (node->children[index] == nullptr || !node->children[index]->isEnd) {
                return false;
            }
            node = node->children[index];
        }
        return node != nullptr && node->isEnd;
    }
private:
    vector<Trie *> children;
    bool isEnd;
};

class Solution {
public:
    string longestWord(vector<string>& words) {
        Trie trie;
        for (const auto & word : words) {
            trie.insert(word);
        }
        string longest = "";
        for (const auto & word : words) {
            if (trie.search(word)) {
                if (word.size() > longest.size() || (word.size() == longest.size() && word < longest)) {
                    longest = word;
                }
            }
        }
        return longest;
    }
};


### 在 Visual Studio 2022 中集成或使用有道词典 在 Visual Studio 2022 中直接集成有道词典并非官方支持的功能,但可以通过一些间接的方法实现类似的效果。考虑到已有的冲突报告[^3],建议谨慎处理。 #### 方法一:外部调用有道词典 一种简单的方式是在需要查询单词时手动启动有道词典客户端进行查找。为了提高效率,可以在 Visual Studio 的工具栏上添加自定义按钮来快速打开有道词典: 1. 打开 Visual Studio 2022; 2. 转到 `工具` -> `自定义命令...`; 3. 添加一个新的外接程序命令指向有道词典可执行文件路径; 这样每次点击该按钮就能迅速唤起有道词典窗口完成即时翻译需求。 #### 方法二:利用在线API服务替代方案 如果希望更无缝地融入开发环境内,则可以考虑通过HTTP请求调用第三方提供的免费/付费的在线翻译 API 接口,在代码编辑器旁边构建一个小部件显示翻译结果。这不仅限于有道词典一家的服务提供商,还包括但不限于谷歌翻译、微软 Translator Text API 等多种选择。 对于此目的,可能需要用到Visual Studio Marketplace上的某些扩展包,比如 "REST Client" 或者其他能够发送 HTTP 请求并解析 JSON 响应的库函数。 ```csharp using System.Net.Http; using Newtonsoft.Json.Linq; public async Task<string> TranslateTextAsync(string textToTranslate, string targetLanguageCode) { using (var client = new HttpClient()) { var response = await client.GetAsync($"https://api.example.com/v1/translate?q={textToTranslate}&target={targetLanguageCode}"); if(response.IsSuccessStatusCode){ var jsonResponse = JObject.Parse(await response.Content.ReadAsStringAsync()); return jsonResponse["translatedText"].ToString(); }else{ throw new Exception("Translation failed"); } } } ``` 需要注意的是,上述方法并不能真正意义上“集成”有道词典至IDE内部,而是采取了变通手段满足开发者日常工作中遇到的语言障碍问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值