一:求岛屿大小
问题
输入:一个二维数组,每一个元素为0或者1
输出:最多有多少个1是连续的
连续的定义:上下左右相邻
样例输入:
const testInput = [
[1, 0, 0, 1, 0],
[1, 0, 1],
[0, 0, 1, 0, 1],
[1, 0, 1, 0, 1],
[1, 0, 1, 1],
];
样例输出:
5
样例输出说明:
答案
function findMax(data) {
let max = 0;
data.forEach((arr,x)=>{
arr.forEach((item,y)=>{
if(data[x][y] === 1) {
max = Math.max(max, addNum(data,x,y))
console.log(max)
}
})
})
return max
}
addNum = function(grid,x,y) {
if(x>=0 && x< grid.length && y>=0 && y<grid[x].length && grid[x][y] === 1) {
grid[x][y] = 0
return 1 + addNum(grid, x-1, y) + addNum(grid, x+1, y) + addNum(grid, x, y-1) + addNum(grid, x , y+1)
} else {
return 0
}
}
let result = findMax(testInput)
console.log(result)
二
问题
const pathList = [
'/a/b',
'/a/2/c',
'/d'
]
const root = {}
construct(pathList , root )
console.log(root)
function construct (pathList, root) {
}
/*
{
"a": {
"2": {
"c": {}
},
"b": {}
},
"d": {}
}
*/
三
问题
const items = [
['a', 'b', 'c', 'd', 'e'],
['1', '2', '3', '4', '5'],
['I', 'II', 'III', 'IV', 'V', 'VI', 'VII']
]
const config = [3, 1, 2]
console.log(ranking(items, config))
function ranking (items, config) {
}
/*
['a', 'b', 'c', '1', 'I', 'II', 'd', 'e', '2', 'III', 'IV', '3', 'V', 'VI', '4', 'VII', '5']
*/
四
industry_list = [
{
"parent_ind" : "女装",
"name" : "连衣裙"
},
{
"name": "女装"
},
{
"parent_ind" : "女装",
"name" : "半身裙"
},
{
"parent_ind" : "女装",
"name" : "A字裙"
},
{
"name": "数码"
},
{
"parent_ind" : "数码",
"name": "电脑配件"
},
{
"parent_ind" : "电脑配件",
"name": "内存"
},
]
为了取用方便,我们希望可以将其转换为树状格式,例如:
{
"数码": {
"电脑配件": {
"内存" : {}
}
},
"女装" : {
"连衣裙": {},
"半身裙": {},
"A字裙": {}
}
}
实现一个方法完成这个转换
function convert_format(data)
function convert_format(data) {
if(Array.isArray(data)) {
var result = data.filter(item=>{
return !item.hasOwnProperty('parent_ind')
}).map(item=>{
// console.log(item)
let temp = {}
temp[item.name] = {}
return temp
})
data.map(item=>{
// console.log(item)
if(item.hasOwnProperty('parent_ind')){
console.log(item)
for(let i = 0; i < result.length; i++) {
for (let p in result[i]) {
if(item.parent_ind === p) {
result[i][p][item.name] = {}
}
}
}
}
})
console.log(result)
return result
}else {
return []
}
}