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

字符串的排列【其他解法】 #17

Open
Silencesnow opened this issue Mar 4, 2020 · 0 comments
Open

字符串的排列【其他解法】 #17

Silencesnow opened this issue Mar 4, 2020 · 0 comments

Comments

@Silencesnow
Copy link

function Permutation(str) {
    const queue = str.split('');
    queue.sort();
    return PermutationCore(queue);
}


function PermutationCore(queue) {
    let result = [];
    if (queue.length === 1) {
        return queue;
    }
    queue.forEach((item, index) => {
        const newArr = queue.slice(0);
        newArr.splice(index, 1);
        const newArray = PermutationCore(newArr).map(i => (item + i));
        result = result.concat(newArray);
    });

    return result;
}


console.log(Permutation('abc')); // ["abc", "acb", "bac", "bca", "cab", "cba"]
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