Algorithm
本周选择的算法题是:Remove Duplicates from Sorted Array II。
规则
Given an integer array nums
sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums
. More formally, if there are k
elements after removing the duplicates, then the first k
elements of nums
should hold the final result. It does not matter what you leave beyond the first k
elements.
Return k
after placing the final result in the first k
slots of nums
.
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
Custom Judge:
The judge will test your solution with the following code:
int[] nums = [...]; // Input array
int[] expectedNums = [...]; // The expected answer with correct length
int k = removeDuplicates(nums); // Calls your implementation
assert k == expectedNums.length;
for (int i = 0; i < k; i++) {
assert nums[i] == expectedNums[i];
}
If all assertions pass, then your solution will be accepted.
Example 1:
Input: nums = [1,1,1,2,2,3]
Output: 5, nums = [1,1,2,2,3,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
Input: nums = [0,0,1,1,1,1,2,3,3]
Output: 7, nums = [0,0,1,1,2,3,3,_,_]
Explanation: Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Constraints:
1 <= nums.length <= 3 * 104
-104 <= nums[i] <= 104
nums
is sorted in non-decreasing order.
Solution
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if len(nums) < 3: return len(nums)
i = 2
for num in nums[2:]:
nums[i] = num
if nums[i] != nums[i-2]:
i += 1
return i
Review
The 6 Principles of Test Automation
作者在这篇文章提到了 6 个测试自动化的原则,分享一下:
- 以改善质量为目标
- 降低引入故障的风险
- 测试工作有助于理解代码
- 必须要容易写
- 必须要容易执行
- 测试套件要保持较低的维护成本
有良好测试行为的系统,是最容易理解的系统,那些认为测试成本很高的人并没有完全意识到质量差的成本,个别而言,错误对产品价值的影响可能难以衡量,如果不加以解决,它们会迅速失控。好在业内有足够多的测试工具和最佳实践来防止这种情况发生,尽管如此,也要理解这些工具诞生的背景、要解决的问题以及背后的理念和原则,从而为开发体验和高质量的产品打好基础。
Tip
确保合适的人在车上,然后多修路,少开车。
Share
保持好心情的 6 件小事
1. 家里放点花
这是一件小事,花可以让空间充满活力,也能让人的心情变得更好。另外它也花不了多少钱,无论是一束还是一小束、三两根,一点点就够了。
ps: 送花到家里之后,第二天的早餐都丰富得多,牛肉面+蛋!
2. 定期逛逛超市
空腹是最大的坏心情催化剂,如果家里能放上一些健康、美味的食物,一来避免冲动购买不健康的食物,二来可以通过在日程里安排固定行程逛逛超市改善心情:
- 现场烘焙,可闻、可尝,美味能放松人的心情
- 努力控制「手推车」也是一件挺有意思的事情
在超市里,感性占主导地位。
3. 给自己留出一个小时
当自己的时间都花在其他人身上时,就像是掉进了被需要的陷阱中,应该尽量找些时间做做自己。比如早起后,用纯净注意力给自己煮一杯咖啡或者看看书,会感到更加脚踏实地。
4. 每天设置小目标
看到一项项事情从待办清单上划掉的感觉很不错,就算是支付账单也能让人感到满足。
5. 静音
把手机或其他电子设备静音一小时,也不会有什么事变得糟糕。在今天,想从世界上 “消失” 就跟想不知道现在几点钟一样难,身边总是会有更种设备提醒你、告诉你要做什么。一天静音一到两小时,聚焦在真正重要的东西上,从世界上 “消失”,会更有 “存在感”。
6. 招待你欣赏的人
无论是工作中的,还是家庭中的,甚至是宠物,和 Ta 散散步、喝杯咖啡,释放出善意,他们会感觉不错,自己也会感觉不错。