leetcode --- js版本程序员

leetcode-Easy-第7期-数组类型-Remove Du

2019-03-05  本文已影响14人  石头说钱

题目:

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

const removeDuplicaties = (arrs) => {
  const len = arrs.length
  for(let i = 0; i < len; i++ ){
    if(arrs[i] !== arrs[i + 1]){
      arrs.push(arrs[i])
    }
  }
  return arrs.splice(0,len).length
}
const removeDuplicaties = (arrs) => {
  const noRepeat = new Set(arrs) //没创建一个对象就要消耗内存空间
  return noRepeat.length
}

上一篇 下一篇

猜你喜欢

热点阅读