这是四则或许对你有些许帮助的信息:

1、👉 最近我发现了一个每日都会推送最新校招资讯的《校招日程》文档,其中包括往届补录应届实习校招信息,比如各种大厂、国企、银行、事业编等信息都会定期更新,帮忙扩散一下。

2、😍 免费分享阿秀个人学习计算机以来收集到的免费学习资源,点此白嫖;也记录一下自己以前买过的不错的计算机书籍、网络专栏和垃圾付费专栏

3、🚀如果你想在校招中顺利拿到更好的offer,阿秀建议你多看看前人踩过的坑留下的经验,事实上你现在遇到的大多数问题你的学长学姐师兄师姐基本都已经遇到过了。

4、🔥 欢迎准备计算机校招的小伙伴加入我的学习圈子,一个人踽踽独行不如一群人报团取暖,圈子里沉淀了很多过去21/22/23届学长学姐的经验和总结,好好跟着走下去的,最后基本都可以拿到不错的offer!此外,每周都会进行精华总结和分享!如果你需要《阿秀的学习笔记》网站中📚︎校招八股文相关知识点的PDF版本的话,可以点此下载

# 140. 单词拆分 II

力扣原题链接(点我直达) (opens new window)

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。

说明:

  • 分隔时可以重复使用字典中的单词。
  • 你可以假设字典中没有重复的单词。

示例 1:

输入:
s = "catsanddog"
wordDict = ["cat", "cats", "and", "sand", "dog"]
输出:
[
  "cats and dog",
  "cat sand dog"
]
1
2
3
4
5
6
7
8

示例 2:

输入:
s = "pineapplepenapple"
wordDict = ["apple", "pen", "applepen", "pine", "pineapple"]
输出:
[
  "pine apple pen apple",
  "pineapple pen apple",
  "pine applepen apple"
]
解释: 注意你可以重复使用字典中的单词。
1
2
3
4
5
6
7
8
9
10

示例 3:

输入:
s = "catsandog"
wordDict = ["cats", "dog", "sand", "and", "cat"]
输出:
[]
1
2
3
4
5

# 1、这是真的不会,看了答案也不会

//回溯+剪枝,利用一个map保留键值映射,想相当于加入剪枝操作,可以对之前计算过的避免重复计算,进而加速计算过程。
vector<string> wordBreakCore(unordered_map<string, vector<string> >& m, vector<string>& wordDict, string s) {
	if (m.count(s)>0) return m[s];
	if (s.empty()) return { "" };
	vector<string> res;
	for (auto &word : wordDict) {
		if (s.substr(0, word.size()) != word) continue;
		vector<string> tmp = wordBreakCore(m, wordDict, s.substr(word.size()));
		for (auto& itm : tmp) {
			res.push_back(word + (itm.empty() ? "" : " " + itm));
		}
	}
	m[s] = res;
	return res;
}

vector<string> wordBreak(string s, vector<string>& wordDict) {
	unordered_map<string, vector<string> > m;
	return wordBreakCore(m, wordDict, s);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21