当前位置: 首页 > news >正文

C++ | Leetcode C++题解之第140题单词拆分II

题目:

题解:

class Solution {
private:unordered_map<int, vector<string>> ans;unordered_set<string> wordSet;public:vector<string> wordBreak(string s, vector<string>& wordDict) {wordSet = unordered_set(wordDict.begin(), wordDict.end());backtrack(s, 0);return ans[0];}void backtrack(const string& s, int index) {if (!ans.count(index)) {if (index == s.size()) {ans[index] = {""};return;}ans[index] = {};for (int i = index + 1; i <= s.size(); ++i) {string word = s.substr(index, i - index);if (wordSet.count(word)) {backtrack(s, i);for (const string& succ: ans[i]) {ans[index].push_back(succ.empty() ? word : word + " " + succ);}}}}}
};
http://www.lryc.cn/news/370281.html

相关文章:

  • JVM (四)GC过程
  • Web前端自学初中:探索与突破
  • 音视频开发26 FFmpeg 时间问题整理
  • 设计模式之MVC模式
  • u盘数据要在哪台电脑上恢复?u盘数据恢复后保存在哪里
  • Selenium 定位编辑框有span
  • mac 安装HomeBrew
  • Windows C++: 剪切板内容获取
  • 1panel运维面板
  • 人工智能GPT-4o?
  • python之语法糖
  • 纷享销客海外合规观点与方案:个人隐私数据保护与数据出入境
  • 公司面试题总结(四)
  • 守护山川,联通希望 —— 北斗户外应急呼叫柱:野外安全的守护神
  • 使用canvas制作一个无人机旋转特效
  • 感受风的速度~2024COSP上海国际户外展为您的骑行之旅锦上添花
  • 冲12分top刊|乳腺癌单细胞细分亚型的正确打开方式
  • 1-Maven-settings配置
  • 使用服务器搭建个人图床
  • 探索 doc 和 docx 文件格式的区别
  • kettle学习(利用jsonPath定位,json文件转换)
  • 选到哪里看命
  • MyBatis插件机制介绍与原理
  • [每日一练]利用.str.len()布尔值查询实现字数需求
  • Post Microsoft Build and AI Day 北京开发者日
  • Qt-Advanced-Docking-System的学习
  • STM32定时器输出pwm的几种模式
  • 5 分支结构程序-5.1 关系运算符和表达式
  • 提升易用性,OceanBase生态管控产品的“从小到大”
  • Golang——gRPC认证