We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
原题链接
先明确题意,题目要求我们原地操作。
在极端情况下,nums 数组中没有元素等于 val,那么左右指针各遍历了一次数组。
const removeElement = function(nums, val) { const len = nums.length let left = 0 for (let right = 0; right < len; right++) { if (nums[right] !== val) { nums[left] = nums[right] left++ } } return left }
这种方法在极端情况下,左右指针加起来只遍历了数组一次。
const removeElement = function(nums, val) { let left = 0 let right = nums.length while (left < right) { if (nums[left] === val) { nums[left] = nums[right - 1] right-- } else { left++ } } return left }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接
双指针
先明确题意,题目要求我们原地操作。
在极端情况下,nums 数组中没有元素等于 val,那么左右指针各遍历了一次数组。
双指针夹逼
这种方法在极端情况下,左右指针加起来只遍历了数组一次。
The text was updated successfully, but these errors were encountered: