Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

分割回文串 #105

Open
louzhedong opened this issue Dec 12, 2018 · 0 comments
Open

分割回文串 #105

louzhedong opened this issue Dec 12, 2018 · 0 comments

Comments

@louzhedong
Copy link
Owner

习题

出处 LeetCode 算法第131题

给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。

返回 s 所有可能的分割方案。

示例:

输入: "aab"
输出:
[
  ["aa","b"],
  ["a","a","b"]
]

思路

采用DFS的方式解答

习题

function copy(array) {
  var res = [];
  var length = array.length;
  for (var i = 0; i < length; i  ) {
    res.push(array[i]);
  }
  return res;
}
function DFS(res, temp, s) {
  if (!s) {
    res.push(copy(temp));
    return;
  }
  for (var i = 1; i <= s.length; i  ) {
    var current = s.slice(0, i);
    if (current.split('').reverse().join('') == current) {
      temp.push(current);
      DFS(res, temp, s.slice(i));
      temp.pop();
    }
  }
}
var partition = function (s) {
  var res = [];
  var temp = [];
  DFS(res, temp, s);
  return res;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant