b***e 发帖数: 1419 | 1 /*
Find all substring palindomes. No dupe.
Vanilla JS. Run it with node.js or in browser console.
*/
function reverse(s) {
var o = '';
for (var i = s.length - 1; i >= 0; i--)
o += s[i];
return o;
};
var p = function(s) {
var prevLookup = {};
var nextLookup = {};
for (var i = 0; i < s.length; i++) {
var c = s.charAt(i);
if (!prevLookup[c]) {
prevLookup[c] = [];
nextLookup[c] = [];
}
}
for (var i = 0; i <... 阅读全帖 |
|
b***e 发帖数: 1419 | 2 /*
Find all substring palindomes. No dupe.
Vanilla JS. Run it with node.js or in browser console.
*/
function reverse(s) {
var o = '';
for (var i = s.length - 1; i >= 0; i--)
o += s[i];
return o;
};
var p = function(s) {
var prevLookup = {};
var nextLookup = {};
for (var i = 0; i < s.length; i++) {
var c = s.charAt(i);
if (!prevLookup[c]) {
prevLookup[c] = [];
nextLookup[c] = [];
}
}
for (var i = 0; i <... 阅读全帖 |
|
b***e 发帖数: 1419 | 3 看这段就够了:
// start code
var _rec = function(left, right, accum) {
console.log(accum + reverse(accum));
if (left > right) return;
for (var c in prevLookup) {
var leftMostPos = nextLookup[c][left];
var rightMostPos = prevLookup[c][right];
if (leftMostPos != null && leftMostPos <= right) {
console.log(accum + c + reverse(accum));
if (rightMostPos > leftMostPos) {
_rec(leftMostPos + 1, r... 阅读全帖 |
|
b***e 发帖数: 1419 | 4 看这段就够了:
// start code
var _rec = function(left, right, accum) {
console.log(accum + reverse(accum));
if (left > right) return;
for (var c in prevLookup) {
var leftMostPos = nextLookup[c][left];
var rightMostPos = prevLookup[c][right];
if (leftMostPos != null && leftMostPos <= right) {
console.log(accum + c + reverse(accum));
if (rightMostPos > leftMostPos) {
_rec(leftMostPos + 1, r... 阅读全帖 |
|