function findLastEmptyIndex(arr) {
// 检查数组是否为空或第一个元素不为空
if (!arr || arr.length === 0 || arr[0] !== '') {
return -1;
}
let lastIndex = 0;
// 遍历数组,找到连续空字符串的最后一个索引
for (let i = 1; i < arr.length; i++) {
if (arr[i] === '') {
lastIndex = i;
} else {
// 遇到非空字符串,立即终止循环
break;
}
}
return lastIndex;
}
// 测试用例
console.log(findLastEmptyIndex(['', '', '', 'a', 'b', ''])); // 2
console.log(findLastEmptyIndex(['', 'hello', '', ''])); // 0
console.log(findLastEmptyIndex(['', '', ''])); // 2
console.log(findLastEmptyIndex(['a', '', ''])); // -1
console.log(findLastEmptyIndex([])); // -1
console.log(findLastEmptyIndex([''])); // 0
console.log(findLastEmptyIndex(['', 'x'])); // 0