Skip to content

Commit eeeccf2

Browse files
nebulaedxubozhe
and
xubozhe
authoredJul 7, 2022
feature: add 1 solution (#8)
* update * update * feature: add 1 solution * feature: add 1 solution and update 1 solution * fix: update README.md * fix: update README.md * feature: add 3 solutions * feature: add 1 solution Co-authored-by: xubozhe <xubozhe@bytedance.com>
1 parent 4bb4910 commit eeeccf2

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed
 

‎.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
CMakeLists.txt
3+
cmake-build-debug
4+
.DS_Store

‎Code/131.分割回文串.cpp

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<string>
4+
#include<array>
5+
using namespace std;
6+
7+
// 我的解法:DFS, 时间 100 ms 84.51%,空间 73.8 MB 77.08%
8+
class Solution {
9+
private:
10+
vector<vector<string>> ret;
11+
12+
bool isPalind(const string& s) {
13+
size_t left = 0, right = s.size() - 1;
14+
while (left < right) {
15+
if (s[left] == s[right]) {
16+
++left;
17+
--right;
18+
} else return false;
19+
}
20+
return true;
21+
}
22+
23+
void dfs(const string& s, size_t pos, vector<string>& curr) {
24+
if (pos == s.size()) {
25+
ret.emplace_back(curr);
26+
return;
27+
}
28+
string part;
29+
for (size_t end = pos + 1; end <= s.size(); ++end) {
30+
part = s.substr(pos, end - pos);
31+
if (isPalind(part)) {
32+
curr.emplace_back(part);
33+
dfs(s, end, curr);
34+
curr.pop_back();
35+
}
36+
}
37+
}
38+
public:
39+
vector<vector<string>> partition(string s) {
40+
vector<string> curr;
41+
dfs(s, 0, curr);
42+
return ret;
43+
}
44+
};
45+
46+
// 我的解法:DFS,时间 116 ms 53.62%,空间 73.6 MB 90.35%
47+
class Solution {
48+
private:
49+
vector<vector<string>> ret;
50+
51+
bool isPalind(const string& s) {
52+
size_t left = 0, right = s.size() - 1;
53+
while (left < right) {
54+
if (s[left] == s[right]) {
55+
++left;
56+
--right;
57+
} else return false;
58+
}
59+
return true;
60+
}
61+
62+
void dfs(const string& s, size_t pos, array<size_t, 16>& curr, size_t c_pos) {
63+
if (pos == s.size()) {
64+
size_t len = 0;
65+
for (size_t i = 0; i < 16; ++i) {
66+
if (curr[i] == 0) {
67+
len = i;
68+
break;
69+
}
70+
}
71+
if (len == 0) len = 16;
72+
vector<string> currRet(len);
73+
size_t prev = 0;
74+
for (size_t i = 0; i < len; ++i) {
75+
currRet[i] = move(s.substr(prev, curr[i] - prev));
76+
prev = curr[i];
77+
}
78+
ret.push_back(move(currRet));
79+
return;
80+
}
81+
for (size_t end = pos + 1; end <= s.size(); ++end) {
82+
if (isPalind(s.substr(pos, end - pos))) {
83+
curr[c_pos] = end;
84+
dfs(s, end, curr, c_pos + 1);
85+
curr[c_pos] = 0;
86+
}
87+
}
88+
}
89+
public:
90+
vector<vector<string>> partition(string s) {
91+
array<size_t, 16> curr{};
92+
dfs(s, 0, curr, 0);
93+
return ret;
94+
}
95+
};
96+
97+
int main() {
98+
string input{"aab"};
99+
100+
Solution sol;
101+
102+
vector<vector<string>> ret = sol.partition(input);
103+
104+
for (auto& var1: ret) {
105+
cout << "[";
106+
for (auto& var2: var1) {
107+
cout << var2 << ",";
108+
}
109+
cout << "],";
110+
}
111+
112+
return 0;
113+
}

0 commit comments

Comments
 (0)
Please sign in to comment.