Algorithm
本周选择的算法题是:Remove Element
规则如下:
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example 1:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2,
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
Note that the order of those five elements can be arbitrary.
It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
Solution
我实现的方案:
Runtime:24 ms,快过 99.39%。
Memory:12.7 MB,低于 100%。
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
j = 0
for i in range(len(nums)):
if nums[i] != val:
nums[j] = nums[i]
j += 1
return j
前后双指针也可以。
Review
苹果的 UIKit 一向反应比较慢。直到 iOS 13,UISearchBar
终于暴露了它的 textField,同时还提供了对 token 的支持。
在以往的版本中,我们需要自己插入 button 一类的控件,然后控制 textField 的输入区域来达到相同的目的,也并不复杂。
Tip
和 Django、nginx 有关的配置过程:
Setting up Django and your web server with uWSGI and nginx
Setting up a Nginx web server on macOS
Nginx: 413 – Request Entity Too Large Error and Solution
Nginx 的停止、检查、重新加载配置的指令:
nginx -s stop
nginx -t
nginx -s reload
Share
本周分享:
技术总监的反思录,我是如何失去团队掌控的? 一篇深刻的总结
Faster way to download and install Xcode 通过从 Apple Developer Portal 下载,并通过 xip 解压安装 Xcode。这样可以方便的在 CI/CD Workflow 中自动化完成指定版本的安装、升级、打包、测试等工作。