參加了網易內推的筆試,分享一下自己對三道程式設計題的理解。(ps:第二題有點投機取巧了)
題目順序為:彩色磚塊,交錯的01串,小易獨立的生活
var line = `ABAB`
var tokens = line.split(``)
var a = {};
var num = 0;
for(var i = 0;i<tokens.length;i++){
if(!a[tokens[i]]){
a[tokens[i]] = 1;
num +=1;
}
}
if(num ==2){
console.log(2);
}
else if(num ==1){
console.log(1);
}
else{
console.log(0);
}
………………………………………………………………………………
var arr = [1,1,1,1,0,1,1,1,1];
var arr3 = [];
var arr2 = [];
var arr1 = [];
// var max = Number(arr[0]);
for (var i = 0; i < arr.length; i++) {
if (Number(arr[i]) == Number(arr[i+1])) {
arr1.push(arr[i]);
arr2.push(arr1);
arr1 = [];
}
else{
arr1.push(arr[i]);
}
}
for(var i =0;i<arr2.length;i++){
arr3[i] = arr2[i].length;
}
arr3.sort(function(a,b){
return b-a;
});
console.log(arr3[0]);
……………………………………………………………………..
var arr = [16,5,100,10];
var day = 0;
for(var i = 1;i<=arr[1];i++){
arr[2] = arr[2] - arr[0];
if(arr[2]<0){
//console.log(day)
}else{
day++
}
}
while(arr[2]>=0){
arr[2] = arr[2] -arr[3] -arr[0];
if(arr[2]>=0){
day++
}
}
console.log(day);