GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — 分割回文串-131 · Issue #67 · sl1673495/leetcode-javascript Open Labels Description给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。 返回 s 所有可能的分割方案。 示例: 来源:力扣(LeetCode) 思路递归全排列尝试所有的下标做起点,所有的下标作为终点,递归暴力判断。 /** * @param {string} s * @return {string[][]} */ let partition = function (s) { let n = s.length let ret = [] let find = function (start, prev) { // 最少分割一个字符 最多分割到末尾前一位 for (let i = 1; i <= n; i++) { let end = start + i let cur = s.substring(start, end) if (cur) { let res = prev.concat(cur) if (isPalindrome(cur)) { if (end === n) { ret.push(res) } else { find(start + i, res) } } } } } find(0, []) return ret } function isPalindrome(s) { if (!s) { return false } let i = 0 let j = s.length - 1 while (i < j) { let head = s[i] let tail = s[j] if (head !== tail) { return false } else { i++ j-- } } return true } ActivityMetadataMetadataAssigneesLabelsProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |