恒美微站 Logo 恒美微站
  • 首页
  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心
  • 联系我们

194 · 寻找单词(前缀和二分法)

  • 首页
  • 资讯中心
  • /
  • 194 · 寻找单词(前缀和二分法)

相关资讯

Unity多平台开发实战:从代码架构到iOS崩溃排查 2026/9/8 14:21:59
用Tcl/Tk构建FPGA仿真文件获取交互界面 2026/9/8 14:16:59
QtCharts 5.12.3 Release编译产物:解决Qt模块缺失与集成问题 2026/9/8 14:16:59

最新资讯

超大文件分片上传与断点续传:WebUploader深度改造实践
2步拿到全球Offer|我的AI海外求职工作流
揭秘!外贸人工SEO优化的独特渠道大公开
Claude Code深度评测:安装配置、接入第三方模型与实战边界解析
STM32+MPU6050数据滤波实战:从原理到代码的完整降噪方案
ARM MTE内存标签扩展:从原理到工程实践的硬件内存安全方案

今日推荐

Redis缓存与离线预计算在大数据处理中的实战应用
Android 12热启动闪屏排查:从冷热启动差异到官方SplashScreen避坑指南
加密资产价值投资:原理、方法与实战策略

本周热门

超人会飞不算本事:系统稳定依赖清晰规则与边界设计
超人VS蜘蛛侠:拆解超级IP的影响力与传播方法论
基于CNN的调制信号识别:MATLAB实现时频图分类实战

本月精选

自研推理加速器Redwood:两周内实现PyTorch模型高效部署的实战教程
V4L2摄像头采集实战:从camera_client.rar到出图全流程解析
从“谁发明了钢琴键”到知识问答智能体:RAG与记忆工程实践

194 · 寻找单词(前缀和二分法)

发布时间:2026/9/8 14:21:59
194 · 寻找单词(前缀和二分法) 链接LintCode 炼码题解双指针方法class Solution { public: /** * param str: the string * param dict: the dictionary * return: return words which are subsequences of the string */ vectorstring findWords(string str, vectorstring dict) { // write your code here. std::vectorstd::string result; for (auto q : dict) { if (valid(str, q)) { result.push_back(q); } } return result; } private: // 双指针方法 bool valid1(const std::string p, const std::string q) { if (p.size() 0) { return false; } int i 0; int j 0; while (i p.size() j q.size()) { if (p[i] q[j]) { i; j; } else { i; } } if (j q.size()) { return true; } return false; } };-二分法求解时间复杂度O(dict中所有字符的长度*log(str字符串长度))--将str字符对应的位置存在到ch2pos的hash表中--遍历dict中所有的元素与str进行对比--对比str和dict元素使用双指针进行对比--dict中对应字符在hash对应字符串中查找当前位置的地方class Solution { public: /** * param str: the string * param dict: the dictionary * return: return words which are subsequences of the string */ vectorstring findWords(string str, vectorstring dict) { // write your code here. std::unordered_mapchar, std::vectorint ch2pos; for (int i 0; i str.size(); i) { // 存储每个字符对应的位置 ch2pos[str[i]].push_back(i); } std::vectorstd::string result; // 判断是否为对应的单词 for (auto word : dict) { if (valid(ch2pos, str, word)) { // 追加结果 result.push_back(word); } } return result; } private: int find_left_index(const std::vectorint pos, int cur_pos) { int left 0; int right pos.size()-1; while (left 1 right) { int mid left (right-left)/2; if (pos[mid] cur_pos) { left mid; } else if (pos[mid] cur_pos) { right mid; } else { left mid; } } // 求得 cur_pos的位置 if (pos[left] cur_pos) { return pos[left]; } if (pos[right] cur_pos) { return pos[right]; } return -1; } bool valid(const std::unordered_mapchar, std::vectorint ch2pos, const std::string p, const std::string q) { if (p.size() 0) { return false; } int i 0; int j 0; while (i p.size() j q.size()) { if (p[i] q[j]) { i; j; } else { auto ite ch2pos.find(q[j]); // 如果当前字符不存在 if (ite ch2pos.end()) { return false; } // 查找当前位置该字符存在的位置 int pos find_left_index(ite-second, i); // 不存在该单词位置 if (pos -1) { return false; } /*if (p[pos] ! q[j]) { return false; }*/ // 更新移动下一个位置 i pos1; j; } } // 对比的单词到达末尾 if (j q.size()) { return true; } return false; } };class Solution { public: /** * param str: the string * param dict: the dictionary * return: return words which are subsequences of the string */ vectorstring findWords(string str, vectorstring dict) { // write your code here. std::vectorstd::string result; if (str.size() 0) { return result; } // table[i][j]当前这个字符i位置开始后面最接近的下字符j的位置是那个下标 std::vectorstd::vectorint table(str.size()1, std::vectorint(26, -1)); for (int i str.size() - 1; i 0; --i) { for (int j 0; j 26; j) { table[i][j] table[i1][j]; if (str[i] - a j) { // 如果是他自己就是它当前的位置 table[i][j] i; } } } for (auto q : dict) { if (valid(table, str, q)) { result.push_back(q); } } return result; } private: bool valid(const std::vectorstd::vectorint table, const std::string p, const std::string q) { if (p.size() 0) { return false; } int i 0; int j 0; while (i p.size() j q.size()) { /*if (p[i] q[j]) { i; j; } else {*/ // 获得字符q[j]的位置 int pos table[i][q[j]-a]; // 不存在这个位置 if (pos -1) { return false; } // 继续往后面查找 i pos 1; j; //} } if (j q.size()) { return true; } return false; } };class Solution { public: /** * param str: the string * param dict: the dictionary * return: return words which are subsequences of the string */ vectorstring findWords(string str, vectorstring dict) { // write your code here. int len str.size(); if (len 0) { return {}; } int dict_len dict.size(); if (dict_len 0) { return {}; } vectorvectorint nexts build(str); vectorstring result; for (auto str : dict) { if (is_same(nexts, len, str)) { result.push_back(str); } } return result; } bool is_same(vectorvectorint nexts, int len, const std::string str) { int i 0; int j 0; while (i len j str.size()) { int index str[j] - a; if (nexts[i][index] len) { //return false; break; } i nexts[i][index] 1; j; } // i在走道超过了可能j还没有走到最后 if (j str.size()) { return true; } return false; } vectorvectorint build(const std::string str) { int len str.size(); if (len 0) { return {}; } vectorvectorint nexts(len, vectorint(26, len)); for (int i str.size()-1; i 0; --i) { if (i ! str.size()-1) { nexts[i] nexts[i1]; } nexts[i][str[i]-a] i; /*for (char ch a; ch z; ch) { if (str[i] ch) { nexts[i][ch-a] i; } }*/ } return nexts; } };

关于恒美微站

恒美微站专注于为个体商户、工作室提供极简自助建站服务,让每个人都能轻松拥有专业网站。

快速链接

  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心

服务项目

  • 可视化建站
  • 拖拽编辑
  • 主题定制
  • SEO 优化
  • 网站托管

联系方式

  • 📍 地址:北京市朝阳区建国路 88 号
  • 📞 电话:400-888-8888
  • ✉️ 邮箱:info@hmyw.cn
  • 🕐 时间:周一至周日 9:00-18:00

© 2024 恒美微站 hmyw.cn 版权所有 | 京 ICP 备 12345678 号