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

[LeetCode]139.单词拆分(C++)

1.代码

class Solution {
public:bool wordBreak(string s, vector<string>& wordDict) {int length = s.size();bool *count1 = new bool[length+1];fill(count1, count1 + length + 1, false);unordered_map<string, bool> hashTable;count1[0] = true;for(int i = 0;i < wordDict.size();i ++){hashTable[wordDict[i]] = true;}for(int i = 1;i <= length;i ++){for(int j = 0;j < i;j ++){if(count1[j] && hashTable.count(s.substr(j,i-j))>0){count1[i] = true;break;}}}return count1[length];}
};

2.思路

用了动态规划,用一个count1数组记录字符串的前i个字母是否能拆分成单词,状态转移方程是count1[i] = count1[j] &&hashTable.count(s.substr(j,i-j))>0。


http://www.mrgr.cn/news/13583.html

相关文章:

  • 使用ffmpeg+node-media-server实现从rtsp服务器拉流再推送至rtmp服务器,实现http+flv进行web播放
  • 【Go语言成长之路】泛型入门
  • 【Linux】软硬链接和动静态库
  • 安美数字酒店宽带运营系统-任意文件读取
  • Python编码系列—Python CI/CD 实战:构建高效的自动化流程
  • Ai产品经理的探索:技能、机遇与未来展望
  • Django 框架中values和values_list的区别
  • 冒泡排序
  • uniapp-Vue项目如何实现国际化,实现多语言切换,拒绝多套开发,一步到位,看这篇就够
  • .NET 中的字符流、字节流和缓冲流
  • java 使用 jakarta.mail 发送邮件
  • Linux下TCP编程
  • error:0308010C:digital envelope routines::unsupported【超详细图解】
  • 数据结构(邓俊辉)学习笔记】串 09——BM_BC算法:以终为始
  • 基于大数据的电信诈骗行为可视化系统含预测研究【lightGBM,XGBoost,随机森林】
  • 景芯SoC A72实战反馈
  • 深度强化学习算法(三)(附带MATLAB程序)
  • SpringBoot Bean初始化顺序
  • springboot+vue+mybatis计算机房屋服务平台+PPT+论文+讲解+售后
  • 【C语言】深入理解指针(四)qsort函数的实现