console
function lamps(a) {
a[0] = a[0] == 0 ? 0 : 1;
let count1 = _handle(a.join('').split(''));
a[0] = a[0] == 0 ? 1 : 0;
let count2 = _handle(a.join('').split(''))+1;
return count1 > count2 ? count2 : count1;
function _handle(arr) {
let count = 0;
document.getElementById('div').innerHTML += '<p>开头是 '+arr[0]+' 的原字符串:'+arr.join('')+'</p><p>***开始***</p>';
while(true) {
if (arr.join('').match(/0(?=0)|1(?=1)/)) {
let temp = arr.join('').match(/0(?=0)|1(?=1)/)[0];
let index = arr.join('').match(/0(?=0)|1(?=1)/)['index'];
temp == 0 ? arr[index+1] = 1 : arr[index+1] = 0;
document.getElementById('div').innerHTML += '<p>'+arr.join('')+'</p>';
count++;
} else {
document.getElementById('div').innerHTML += '<p>***结束***</p><p>开头是 1 的新字符串:'+arr.join('')+'</p>';
break;
}
}
document.getElementById('div').innerHTML += '<p>'+'进行次数:'+count+' 次'+'</p><br/>';
return --count;
}
}
lamps([1,1,0,1,1,0,0,0,0,1,0]);
<div id="div"></div>
p {
margin: 0;
padding: 0;
}