代码拉取完成,页面将自动刷新
/*
* @lc app=leetcode.cn id=131 lang=java
*
* [131] 分割回文串
*/
// @lc code=start
class Solution {
public List<List<String>> partition(String s) {
List<List<String>> res = new ArrayList();
List<String> list = new ArrayList();
backTrack(s, res, list, 0);
return res;
}
private void backTrack(String s, List<List<String>> res, List<String> list, int index) {
if (index == s.length()) {
res.add(new ArrayList(list));
return;
}
for (int i = index; i < s.length(); i++) {
if (!valid(s, index, i)) {
continue;
}
list.add(s.substring(index, i + 1));
backTrack(s, res, list, i + 1);
list.remove(list.size() - 1);
}
}
private boolean valid(String s, int left, int right) {
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}
// @lc code=end
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。