Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript 移动 0 元素 #17

Open
sunhengzhe opened this issue Apr 26, 2018 · 23 comments
Open

javascript 移动 0 元素 #17

sunhengzhe opened this issue Apr 26, 2018 · 23 comments

Comments

@sunhengzhe
Copy link
Member

sunhengzhe commented Apr 26, 2018

试写一个方法,将一个数组内所有 0 元素移动到数组的末尾,并保证其他元素的相对次序保持不变,且 0 元素的排列顺序也与出现顺序一致。

例如有如下数组

[3, 0, 4, 6, "0", 0, 13, "0", 78, 0, 14]

应输出

[3, 4, 6, 13, 78, 14, 0, "0", 0, "0", 0]

限制:

  • 不能使用任何数组或对象作为临时变量
  • 不能使用任何 Array.prototype 和 Object.prototype 上的方法
function removeZeros(array) {
    // TODO
}

var input = [7, 2, 3, null, 4, 6, '0', undefined, 13, '0', 78, 0, 0, 19, 14];
removeZeros(input); // [ 7, 2, 3, null, 4, 6, undefined, 13, 78, 19, 14, '0', '0', 0, 0 ]

var input = [0, 1];
removeZeros(input); // [ 1, 0 ]

var input = [2, 1, 4];
removeZeros(input); // [ 2, 1, 4 ]

var input = [0, '0'];
removeZeros(input); // [ 0, '0' ]
@andysongs
Copy link

andysongs commented Apr 26, 2018

const removeZero = function (arr) {
  let count = 0;
  for (let i = 0; i < arr.length - 1; i++) {
    if (isZero(arr[i])) {
      count++
      let tmp = arr[i];
      for (let j = i + 1; j < arr.length; j++) {
        arr[j - 1] = arr[j];
      }
      arr[arr.length - 1] = tmp;
      // console.log(tmp)
    }
    if (i + count === arr.length - 1) break;
  }
  return arr
}

const isZero = function (val) {
  return val === 0 || val === '0'
}

console.log(removeZero(['0', 1, 2, 0, 4]))

@andysongs
Copy link

andysongs commented Apr 26, 2018

修复上一种做法无法识别连续的0的问题

const removeZero = function (arr) {
  let count = 0;
  let i = 0;
  while (i < arr.length) {
    // if (i + count >= arr.length - 1) break;
    if (i + count >= arr.length) break;
    if (isZero(arr[i])) {
      count++
      let tmp = arr[i];
      for (let j = i + 1; j < arr.length; j++) {
        arr[j - 1] = arr[j];
      }
      arr[arr.length - 1] = tmp;
    }
    if (!isZero(arr[i])) i++
  }
  return arr
}

const isZero = function (val) {
  return val === 0 || val === '0'
}

let arr = [7, 2, 3, null, 4, 6, '0', undefined, 13, '0', 78, 0, 0, 19, 14]
console.log(removeZero(arr)) // [7, 2, 3, null, 4, 6, undefined, 13, 78, 19, 14, '0', '0', 0, 0]

@keer3
Copy link
Member

keer3 commented Apr 26, 2018

@andysongs let arr = [0, 0, 0, '0', '0'] 有问题吧🧐

@keer3
Copy link
Member

keer3 commented Apr 26, 2018

const isZero = (number) => number === '0' || number === 0

function removeZeros(array) {
  let length = array.length
  let i = 0
  let zeroCount = 0
  while (i <= length) {
    if (isZero(array[i])) {
      let flag = array[i]
      for (let l = i + 1; l < length; l++) {
        array[l - 1] = array[l]
      }
      array[length - 1] = flag
      zeroCount ++
    } else {
      i ++
    }

    if (i >= length - zeroCount) break
  }
  return array
}

@andysongs
Copy link

@SUNSHUMIN 已修改

@yinkaihui
Copy link

yinkaihui commented Apr 26, 2018

function removeZeros(array) {
  // array 是数组
  const len = array.length
  if (len < 2) {
    return array
  }
  let beginZeroIndex

  for (let i = 0; i < len; i++) {
    if (array[i] === 0 || array[i] === '0') {
      if (beginZeroIndex === undefined) {
        beginZeroIndex = i
      }
    } else {
      if (beginZeroIndex !== undefined && i > beginZeroIndex) {
        let j = i
        let tmp = array[j]
        while (j > beginZeroIndex) {
          array[j] = array[j - 1]
          j--
        }
        array[beginZeroIndex] = tmp
        beginZeroIndex++
      }
    }
  }
  return array  
}

@ThoughtZer
Copy link

ThoughtZer commented Apr 26, 2018

function removeZeros(arr) {
    var temp = 0
    var count = 0
    var i = 0
    for (i = 0; i < arr.length; i++){
      if (arr[i] === 0 || arr[i] === '0'){
        count++
      }
    }
    if (count !== arr.length){
      while (count > 0) {
        for (i = 0; i < arr.length; i++) {
          temp = arr[i]
          if (temp === 0 || temp === '0') { // 遇到0或者'0'把后面的往前推,把0或者'0'放当前次最后一个
            for (var j = i; j < arr.length; j++) {
              if (j + 1 < arr.length) {
                arr[j] = arr[j + 1]
              } else {
                arr[j] = temp
              }
            }
            i = 0
            count--
            if (count <= 0) {
              break
            }
          }
        }
      }
    }
    console.log(arr)
  }

@iLove-Coding
Copy link

iLove-Coding commented Apr 26, 2018

function removeZeros(array) {
            var temp;
            var total = 0;
            for (var i = 0; i < array.length - total; i++) {
           //修正:这里不能用==0做判断,存在隐式类型转换问题,0==[],0=="",0==false均为true
                if (array[i] === 0||array[i]==='0') {
                    temp = array[i];
                    for (var j = i; j < array.length - 1; j++) {
                        array[j] = array[j + 1];
                    }
                    array[array.length - 1] = temp;
                    total++;
                    i--;
                }
            }
            return array;
        }

@keer3
Copy link
Member

keer3 commented Apr 27, 2018

@楼上 == 0 ?

@chenfengjuan
Copy link

function removeZeros(array) {
    let l=array.length;
    let count=0;
    for (let i=0;i<l-count; i++){
        let element=array[i];
        if(element===0||element==='0'){
            count++;
            for(let j=i; j<l-1; j++){
                array[j]=array[j+1];
                array[j+1]=element
            }
        }
        if(array[i]==='0'||array[i]===0){
            i--;
        }
    }
    console.log(array)
}

@miSunLaughing
Copy link

miSunLaughing commented Apr 27, 2018

function removeZeros(array) {
	var zeroNums = 0;
	var index = array.length - 1;
	for(var i = index; i >= 0; i-- ) {
		if (array[i] === 0 || array[i] === '0'){
			if ( i !== index) {
				for (var m = i; m < index - zeroNums; m ++){
					var value = array[m];
		    		        array[m] = array[m + 1];
		    		        array[m + 1] = value;
				}
			}
			zeroNums++;
    	        }
	}
	return array;
}

@Jiasm
Copy link

Jiasm commented Apr 27, 2018

filter一下不就好了么-.-

function removeZeros(array) {
  let targets = [0, '0']
  return [...array.filter(item => !targets.includes(item)), ...array.filter(item => targets.includes(item))]
}

var input = [7, 2, 3, null, 4, 6, '0', undefined, 13, '0', 78, 0, 0, 19, 14]
console.log(removeZeros(input)) // [ 7, 2, 3, null, 4, 6, undefined, 13, 78, 19, 14, '0', '0', 0, 0 ]

var input = [0, 1]
console.log(removeZeros(input)) // [ 1, 0 ]

var input = [2, 1, 4]
console.log(removeZeros(input)) // [ 2, 1, 4 ]

var input = [0, '0']
console.log(removeZeros(input)) // [ 0, '0' ]

@sunhengzhe
Copy link
Member Author

@Jiasm 你一定是没有看题目里的限制。。

@Jiasm
Copy link

Jiasm commented Apr 27, 2018

@sunhengzhe 好BT的游戏规则。。容我听首歌想一下

@iLove-Coding
Copy link

@SUNSHUMIN 改好啦~感谢纠错专家😄

@keer3
Copy link
Member

keer3 commented Apr 27, 2018

@iLove-Coding 🙃我啥都没说

@iLove-Coding
Copy link

@SUNSHUMIN 🤓️无声胜有声

@Jiasm
Copy link

Jiasm commented Apr 27, 2018

添加了一些限制以后的实现-.-

function removeZeros(array) {
  let storage = (tag, before) => function * () {
    if (before) yield* before()
    yield tag
  }
  let normal
  let special
  for (let item of array) item === 0 || item === '0' ? special = storage(item, special) : normal = storage(item, normal)

  let func = (...arg) => arg;

  ((...args) => {
    let ret
    for (let gen of args.filter(_ => _)) {
      func = func.bind(null, ...gen)
    }
  })(normal && normal(), special && special())

  return func()
}

var input = [7, 2, 3, null, 4, 6, '0', undefined, 13, '0', 78, 0, 0, 19, 14]
console.log(removeZeros(input)) // [ 7, 2, 3, null, 4, 6, undefined, 13, 78, 19, 14, '0', '0', 0, 0 ]

var input = [0, 1]
console.log(removeZeros(input)) // [ 1, 0 ]

var input = [2, 1, 4]
console.log(removeZeros(input)) // [ 2, 1, 4 ]

var input = [0, '0']
console.log(removeZeros(input)) // [ 0, '0' ]

@sunhengzhe
Copy link
Member Author

@Jiasm 这个 generator 用的我给满分 💯

@gp0320
Copy link

gp0320 commented May 2, 2018

自己写了一个 依靠冒泡排序 和 三元交互法
`
function sort(arr){

  let index = 0

  for(let i = 0; i<arr.length-index ; i++){

    if(arr[i] === 0 || arr[i] === '0'){
      ++index

      for(let j = i; j< arr.length-1 ; j++){

          let temp = arr[j]
          arr[j] = arr[j+1]
          arr[j+1] = temp
  
      }
    }

    if(arr[i] === 0 || arr[i] === '0'){
      i--
    }
  }

}

`

@Makcy
Copy link

Makcy commented Jun 13, 2018

.length 不就是 Array.prototype内的方法吗。。。所以 综上:没有正确答案?

@sunhengzhe
Copy link
Member Author

@Makcy .length 不是方法。。是属性

@Makcy
Copy link

Makcy commented Jun 13, 2018

@sunhengzhe 噢 打扰了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests