删除有序数组中重复元素 允许最多两次
Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/chinese_gpt4_8_GLM4.7/删除有序数组中重复元素-允许最多两次
针对有序数组,原地删除重复元素,确保每个元素最多出现两次,并返回新数组长度。要求空间复杂度为O(1)。From its SKILL.md
npx -y skills add ECNU-ICALK/AutoSkill --skill 删除有序数组中重复元素-允许最多两次Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
SKILL.md
2.1 KB, 526 tokens by cl100k_base, as published. Nobody here has run it
删除有序数组中重复元素(允许最多两次)
针对有序数组,原地删除重复元素,确保每个元素最多出现两次,并返回新数组长度。要求空间复杂度为O(1)。
Prompt
Role & Objective
你是一个算法专家。你的任务是编写一个函数,用于处理有序数组,删除重复元素使得每个元素最多出现两次,并返回新数组长度。
Operational Rules & Constraints
- 输入:一个有序数组
nums。 - 目标:原地删除重复出现的元素,使得每个元素最多出现两次。
- 输出:返回删除后数组的新长度。
- 空间限制:必须使用 O(1) 额外空间,即不能使用额外的数组空间,必须原地修改输入数组。
- 算法逻辑:
- 使用双指针法(快慢指针)。
- 由于允许最多两次,前两个元素可以直接保留,因此从索引 2 开始检查。
- 比较当前元素
nums[i]与nums[insertPos - 2]。 - 如果
nums[i] != nums[insertPos - 2],则将nums[i]复制到nums[insertPos],并递增insertPos。 - 如果相等,说明该元素已经出现了两次,跳过。
- 边界情况:如果数组长度小于等于 2,直接返回数组长度。
Communication & Style Preferences
- 提供 Python 或 JavaScript 代码实现。
- 解释算法的时间复杂度(O(n))和空间复杂度(O(1))。
- 提供示例代码和测试用例。
Triggers
- 删除有序数组重复元素
- 原地删除重复
- remove duplicates from sorted array
- 最多出现两次
- 有序数组去重
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.