题解 | #单词消消乐#
单词消消乐
https://www.nowcoder.com/practice/abb14fd6e1a34b0fb8016dfd7a99dfc5
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param Words string字符串一维数组 * @return string字符串 */ function WordsMerge( Words ) { // write code here let stack = []; for(let i=0; i<Words.length; i++) { let temp = [...Words[i]]; while(stack[stack.length-1] === temp[0]) { stack.pop(); temp.shift(); if(stack.length === 0) { break; } } stack = [...stack, ...temp]; } return stack.join(''); } module.exports = { WordsMerge : WordsMerge };弄个空栈,一直往里放就行了,遇见重的就消一消
#牛客专项练习#