Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fa6975f

Browse files
other
1 parent 76bd067 commit fa6975f

File tree

2 files changed

+273
-0
lines changed

2 files changed

+273
-0
lines changed

‎cpp/leetcode/68.文本左右对齐.cpp

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/*
2+
* @lc app=leetcode.cn id=68 lang=cpp
3+
*
4+
* [68] 文本左右对齐
5+
*
6+
* https://leetcode.cn/problems/text-justification/description/
7+
*
8+
* algorithms
9+
* Hard (52.22%)
10+
* Likes: 334
11+
* Dislikes: 0
12+
* Total Accepted: 53.9K
13+
* Total Submissions: 103.2K
14+
* Testcase Example: '["This", "is", "an", "example", "of", "text",
15+
* "justification."]\n16'
16+
*
17+
* 给定一个单词数组 words
18+
* 和一个长度 maxWidth ,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。
19+
*
20+
* 你应该使用 "贪心算法"
21+
* 来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 '
22+
* ' 填充,使得每行恰好有 maxWidth 个字符。
23+
*
24+
* 要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。
25+
*
26+
* 文本的最后一行应为左对齐,且单词之间不插入额外的空格。
27+
*
28+
* 注意:
29+
*
30+
*
31+
* 单词是指由非空格字符组成的字符序列。
32+
* 每个单词的长度大于 0,小于等于 maxWidth。
33+
* 输入单词数组 words 至少包含一个单词。
34+
*
35+
*
36+
*
37+
*
38+
* 示例 1:
39+
*
40+
*
41+
* 输入: words = ["This", "is", "an", "example", "of", "text",
42+
* "justification."], maxWidth = 16 输出:
43+
* [
44+
* "This is an",
45+
* "example of text",
46+
* "justification. "
47+
* ]
48+
*
49+
*
50+
* 示例 2:
51+
*
52+
*
53+
* 输入:words = ["What","must","be","acknowledgment","shall","be"], maxWidth =
54+
* 16 输出:
55+
* [
56+
* "What must be",
57+
* "acknowledgment ",
58+
* "shall be "
59+
* ]
60+
* 解释: 注意最后一行的格式应为 "shall be " 而不是 "shall be",
61+
* 因为最后一行应为左对齐,而不是左右两端对齐。
62+
* ⁠ 第二行同样为左对齐,这是因为这行只包含一个单词。
63+
*
64+
*
65+
* 示例 3:
66+
*
67+
*
68+
* 输入:words =
69+
* ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"],maxWidth
70+
* = 20
71+
* 输出:
72+
* [
73+
* "Science is what we",
74+
* ⁠ "understand well",
75+
* "enough to explain to",
76+
* "a computer. Art is",
77+
* "everything else we",
78+
* "do "
79+
* ]
80+
*
81+
*
82+
*
83+
*
84+
* 提示:
85+
*
86+
*
87+
* 1 <= words.length <= 300
88+
* 1 <= words[i].length <= 20
89+
* words[i] 由小写英文字母和符号组成
90+
* 1 <= maxWidth <= 100
91+
* words[i].length <= maxWidth
92+
*
93+
*
94+
*/
95+
96+
#include <string>
97+
#include <vector>
98+
using namespace std;
99+
100+
// @lc code=start
101+
class Solution {
102+
public:
103+
vector<string> fullJustify(vector<string> &words, int maxWidth) {
104+
vector<string> ret;
105+
if (words.empty()) {
106+
return ret;
107+
}
108+
int l = 0, r = 0, n = words.size();
109+
while (true) {
110+
int lineWidth = 0;
111+
while (r < n && lineWidth + r - l + words[r].length() <= maxWidth) {
112+
lineWidth += words[r++].length();
113+
}
114+
// last line
115+
if (r == n) {
116+
string s = join(words, l, r, " ");
117+
ret.emplace_back(s + blank(maxWidth - s.length()));
118+
break;
119+
}
120+
// one word
121+
if (r - l == 1) {
122+
ret.emplace_back(words[l] + blank(maxWidth - words[l].length()));
123+
l++;
124+
continue;
125+
}
126+
// more than one word
127+
int wordNum = r - l;
128+
int spaceNum = maxWidth - lineWidth;
129+
int avgSpace = spaceNum / (wordNum - 1); // segregation num: wordNum - 1
130+
int extraSpace = spaceNum % (wordNum - 1);
131+
string s1 = join(words, l, l + extraSpace + 1, blank(avgSpace + 1));
132+
string s2 = join(words, l + extraSpace + 1, r, blank(avgSpace));
133+
ret.emplace_back(s1 + blank(avgSpace) + s2);
134+
l = r;
135+
}
136+
137+
return ret;
138+
}
139+
140+
private:
141+
string blank(int n) { return string(n, ' '); }
142+
string join(vector<string> &words, int l, int r, string seg) {
143+
string ret(words[l]);
144+
for (size_t i = l + 1; i < r; i++) {
145+
ret += seg + words[i];
146+
}
147+
return ret;
148+
}
149+
};
150+
// @lc code=end

‎cpp/leetcode/76.最小覆盖子串.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* @lc app=leetcode.cn id=76 lang=cpp
3+
*
4+
* [76] 最小覆盖子串
5+
*
6+
* https://leetcode.cn/problems/minimum-window-substring/description/
7+
*
8+
* algorithms
9+
* Hard (45.17%)
10+
* Likes: 2496
11+
* Dislikes: 0
12+
* Total Accepted: 420.4K
13+
* Total Submissions: 930.7K
14+
* Testcase Example: '"ADOBECODEBANC"\n"ABC"'
15+
*
16+
* 给你一个字符串 s 、一个字符串 t 。返回 s 中涵盖 t 所有字符的最小子串。如果 s
17+
* 中不存在涵盖 t 所有字符的子串,则返回空字符串 "" 。
18+
*
19+
*
20+
*
21+
* 注意:
22+
*
23+
*
24+
* 对于 t 中重复字符,我们寻找的子字符串中该字符数量必须不少于 t 中该字符数量。
25+
* 如果 s 中存在这样的子串,我们保证它是唯一的答案。
26+
*
27+
*
28+
*
29+
*
30+
* 示例 1:
31+
*
32+
*
33+
* 输入:s = "ADOBECODEBANC", t = "ABC"
34+
* 输出:"BANC"
35+
* 解释:最小覆盖子串 "BANC" 包含来自字符串 t 的 'A'、'B' 和 'C'。
36+
*
37+
*
38+
* 示例 2:
39+
*
40+
*
41+
* 输入:s = "a", t = "a"
42+
* 输出:"a"
43+
* 解释:整个字符串 s 是最小覆盖子串。
44+
*
45+
*
46+
* 示例 3:
47+
*
48+
*
49+
* 输入: s = "a", t = "aa"
50+
* 输出: ""
51+
* 解释: t 中两个字符 'a' 均应包含在 s 的子串中,
52+
* 因此没有符合条件的子字符串,返回空字符串。
53+
*
54+
*
55+
*
56+
* 提示:
57+
*
58+
*
59+
* ^m == s.length
60+
* ^n == t.length
61+
* 1 <= m, n <= 10^5
62+
* s 和 t 由英文字母组成
63+
*
64+
*
65+
*
66+
* 进阶:你能设计一个在 o(m+n) 时间内解决此问题的算法吗?
67+
*/
68+
69+
#include <iostream>
70+
#include <string>
71+
#include <unordered_map>
72+
using namespace std;
73+
74+
// @lc code=start
75+
class Solution {
76+
public:
77+
// 双指针滑动窗口,右指针向前,覆盖之后左指针向前找最小值
78+
// 给t增加hashmap用作索引,另外再用一个hashmap存当前窗口各字符统计值
79+
string minWindow(string s, string t) {
80+
if (s.empty() || t.empty()) {
81+
return "";
82+
}
83+
int l = 0, r = -1, cnt = 0, m = s.size(), n = t.size();
84+
unordered_map<char, int> index, window;
85+
index.reserve(n);
86+
for (const auto ch : t) {
87+
index[ch]++;
88+
}
89+
int ret_l = -1, ret_len = m + 1; // 多个1,s全长刚好满足时可以满足>r-l+1
90+
// s = "ADOBECODEBANC", t = "ABC"
91+
while (r < m) {
92+
auto itir = index.find(s[++r]);
93+
if (itir != index.end()) {
94+
if (++window[s[r]] <= itir->second) { // 字符未冗余
95+
cnt++;
96+
}
97+
}
98+
while (cnt >= n && l <= r) { // 已覆盖,尝试精简
99+
if (ret_len > r - l + 1) { // 更新返回值
100+
ret_len = r - l + 1;
101+
ret_l = l;
102+
}
103+
104+
auto itil = index.find(s[l]);
105+
if (itil != index.end()) {
106+
if (--window[s[l]] < itil->second) { // 已去掉冗余
107+
cnt--;
108+
}
109+
}
110+
l++;
111+
}
112+
}
113+
return ret_l == -1 ? "" : s.substr(ret_l, ret_len);
114+
}
115+
};
116+
// @lc code=end
117+
118+
int main(int argc, const char **argv) {
119+
Solution s;
120+
auto ret = s.minWindow("abc", "cba");
121+
cout << "ret:" << ret;
122+
return 0;
123+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /