leetcode 序列重排,[leetcode] 140. 單詞拆分 II

 2023-11-18 阅读 29 评论 0

摘要:class Solution {int n;vector<string>res;unordered_map<int,set<int>>hash;string s;void dfs(int start, string temp){if(start == n){res.push_back(temp);return ;}for(auto end : hash[start]){if(start == 0){//前面不要有空格dfs

在這里插入圖片描述
在這里插入圖片描述

class Solution {int n;vector<string>res;unordered_map<int,set<int>>hash;string s;void dfs(int start, string temp){if(start == n){res.push_back(temp);return ;}for(auto end : hash[start]){if(start == 0){//前面不要有空格dfs(end, s.substr(start, end - start));}else{dfs(end, temp +" " + s.substr(start, end - start));}}}
public:vector<string> wordBreak(string s, vector<string>& wordDict) {this->n = s.size();this->s = s;unordered_set<string>m_set(wordDict.begin(), wordDict.end());vector<bool>dp(s.size() + 1, false);dp[s.size()] = true;for(int i = s.size(); i >= 1; i--){if(dp[i] == true){for(int j = 0; j < i; j++){if(m_set.count(s.substr(j, i - j)) == 1){hash[j].insert(i);dp[j] = true;}}}}dfs(0, "");return res;}
};

在這里插入圖片描述

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/178874.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息