-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path131.cpp
More file actions
46 lines (41 loc) · 1.14 KB
/
131.cpp
File metadata and controls
46 lines (41 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//
// 131.cpp
// LeetCode
//
// Created by 张佐玮 on 15/7/16.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Palindrome Partitioning
//
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<string>> partition(string s) {
vector<string> current;
vector<vector<string>> result;
partitionRecursive(current, result, s, 0, s.size()-1);
return result;
}
void partitionRecursive (vector<string> ¤t, vector<vector<string>> &result, string s, long start, long end) {
if (start > end) {
result.push_back(current);
}
for (long i = start; i <= end; i++) {
if (isPalidrome(s, start, i)) {
current.push_back(s.substr(start, i-start+1));
partitionRecursive(current, result, s, i+1, end);
current.pop_back();
}
}
}
bool isPalidrome(string &s, long start, long end) {
while (start < end) {
if (s[start++] != s[end--]) {
return false;
}
}
return true;
}
};