You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constremoveDuplicate=function(nums){// show you codereturnnums;}letarr=[1,2,2,3];letdupArray=removeDuplicate(arr)console.log(dupArray,dupArray===arr);// [1,2,3] true
Activity
CodeRookie262 commentedon Jan 8, 2021
暴力破解版
CodeRookie262 commentedon Feb 1, 2021
双指针法(快慢指针)
简化版
var removeDuplicate = function(nums){ if(!nums || nums.length === 0) return nums; let i = 0; for(var k = 1;k < nums.length;k++){ if(nums[k] !== nums[k - 1]) nums[++i] = nums[k]; } - nums.length = i; + nums.length = i + 1; return nums; }CodeRookie262 commentedon Feb 1, 2021
计数法
yokuray commentedon Feb 1, 2021
学废了学废了,但是我不会告诉你简化版有小问题🙈
yokuray commentedon Feb 1, 2021
复杂的我不懂,来个简单的(~ ̄▽ ̄)~
CodeRookie262 commentedon Feb 1, 2021
发现了哈哈,你说的应该是
nums.length = i,这个需要更改为nums.length = i + 1;CodeRookie262 commentedon Feb 1, 2021
@Mirai39 👍👍
这代码写的可以,反向遍历切掉重复的元素,解决了指向切割照成数组崩塌的问题,也是一种解决方法,Nice~
不过读起来有点费劲😁😁
CodeRookie262 commentedon Feb 1, 2021
@Mirai39 👍👍
这代码写的可以,反向遍历切掉重复的元素,解决了指向切割照成数组崩塌的问题,也是一种解决方法,Nice~
不过我感觉我读起来有点费劲┭┮﹏┭┮
yokuray commentedon Feb 1, 2021
哈哈哈~~我的错我的错,谢罪<( _ _ )>
CodeRookie262 commentedon Feb 1, 2021
不会呀,这个方法我真的没有想到,多亏你让我学到了新的解法 o( ̄▽ ̄)d good
yokuray commentedon Feb 1, 2021
握爪~~