← 返回 google 的题目列表Replace first duplicate with sum and remove others in-place
类型:online_judge
google
Given a sorted array nums, for example [1, 2, 2, 3], replace the first occurrence of duplicate numbers with their sum and remove subsequent duplicate numbers to obtain a new array [1, 4, 3]. The algorithm should have a time complexity of O(n) and a space complexity of O(1).
Implement the def replace_and_remove_duplicates(nums: List[int]) -> List[int] function.
Input: nums = [1, 2, 2, 3]
Output: [1, 4, 3]
Assume that nums is sorted and contains zero or more duplicates.
Input Example:
[1, 2, 2, 3, 3, 4, 4, 4, 5]
Output Example:
[1, 4, 6, 5]
Example
Input
[1, 2, 2, 3]