参加了网易内推的笔试,分享一下自己对三道编程题的理解。(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);