codewars解题笔记 —— 数组的判断

题目:

You get an array of arrays.
If you sort the arrays by their length, you will see, that their length-values are consecutive.
But one array is missing!


You have to write a method, that return the length of the missing array.

Example:
[[1, 2], [4, 5, 1, 1], [1], [5, 6, 7, 8, 9]] --> 3

If the array of arrays is null/nil or empty, the method should return 0.

When an array in the array is null or empty, the method should return 0 too!
There will always be a missing element and its length will be always between the given arrays. 

数组中是长度连续的几个数组,缺失了中间的某一个,找到缺失的那一个的长度是多少,如果传的是空数组,返回0,如果数组中有长度为0的数组,返回0


我的答案:

function getLengthOfMissingArray(arrayOfArrays) {
      let a = null
      if(arrayOfArrays === '' || !arrayOfArrays) {
        a = 0
      }else if(arrayOfArrays.length === 0){
	a = 0
      } else {
	let arr = []
	arrayOfArrays.map(item => {
            if(item){
              arr.push(item.length)
            }else {
              a = 0
            }
	})
	arr.sort(sortNumber)
        console.log(arr)
        if (arr[0] === 0) {
          a = 0
        }else {
          arr.forEach((item, index) => {
  		if(item+1 !== arr[index+1] && arr[index+1]){
  		a = item+1
  	      }  
  	   })
        }
      }
      console.log(a)
      return a 
}
function sortNumber(a,b){
  return a - b
}


票数最高的答案

function getLengthOfMissingArray(arrayOfArrays) {
  const lengths = (arrayOfArrays || [])
    .map(a => a ? a.length : 0)
    .sort((a, b) => a - b)
  
  if (lengths.includes(0)) {
    return 0
  }

  for (let i = 0; i < lengths.length - 1; i++) {
    if (lengths[i] + 1 !== lengths[i + 1]) {
      return lengths[i] + 1
    }
  }

  return 0
}

思考:

1、清晰的解题思路很重要

2、多用链式结构,更简洁高效

3、记住一些常用的代码,比如这个例子中的排序


arr.sort((a, b) => a-b)

10,5,40,25,1000,1  =>  1,5,10,25,40,1000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值