-
Notifications
You must be signed in to change notification settings - Fork 0
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
Labels
Comments
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])) |
修复上一种做法无法识别连续的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] |
@andysongs let arr = [0, 0, 0, '0', '0'] 有问题吧🧐 |
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
} |
@SUNSHUMIN 已修改 |
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
} |
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)
} |
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;
} |
@楼上 == 0 ? |
|
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;
} |
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' ] |
@Jiasm 你一定是没有看题目里的限制。。 |
@sunhengzhe 好BT的游戏规则。。容我听首歌想一下 |
@SUNSHUMIN 改好啦~感谢纠错专家😄 |
@iLove-Coding 🙃我啥都没说 |
@SUNSHUMIN 🤓️无声胜有声 |
添加了一些限制以后的实现-.- 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' ] |
@Jiasm 这个 generator 用的我给满分 💯 |
自己写了一个 依靠冒泡排序 和 三元交互法
` |
.length 不就是 Array.prototype内的方法吗。。。所以 综上:没有正确答案? |
@Makcy .length 不是方法。。是属性 |
@sunhengzhe 噢 打扰了 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
试写一个方法,将一个数组内所有 0 元素移动到数组的末尾,并保证其他元素的相对次序保持不变,且 0 元素的排列顺序也与出现顺序一致。
例如有如下数组
[3, 0, 4, 6, "0", 0, 13, "0", 78, 0, 14]
应输出
[3, 4, 6, 13, 78, 14, 0, "0", 0, "0", 0]
限制:
The text was updated successfully, but these errors were encountered: