活跃农民
- 积分
- 431
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-8-4
- 最后登录
- 1970-1-1
|
本帖最后由 Husky_wang 于 2019-5-9 20:01 编辑
5.9 做题
27. Remove Element. This is a two pointers problem. Almost all char removal problem can use two pointers method. Slow pointer points to the index that all elements left to it should be returned, while fast pointer is the current index. In this case, we move the fast pointer each step and we move the slow pointer only if the fast pointer is pointing to the elements which should be returned. Copy it first to the slow then move the slow.
26. Remove Duplicates from Sorted Array. Still, removing duplicates is char removal problem and for almost all char removal problems we may consider to use two pointers method. We must know when should we move the slow pointer and what should we do when we move the slow pointer. In this case, fast pointer is the current index and if the value of fast is equal to the value of slow, which means we meet the duplicates, we need skip this element using fast pointer. When we find a new element next, we will want to keep it, so we will copy it to which index the slow pointer is pointing. What need to be careful about is whether we need copy first then move slow, or we need move slow first then copy. In this case we move first. It depends on the physical meaning of the slow pointer. Excluding or including.
80. Remove Duplicates from Sorted Array II. This problem require me to keep two elements for each duplicates. How should I deal with this case? May I solve this problem based on the Remove Duplicates from Sorted Array I? What's the difference between these two problem? First, both slow and fast pointers should start with index 2, since in any cases we need return at least two elements. Then, we need to compare value of fast with value of slow-2 (in this case I will return the solution excluding the slow), since there have been already two elements returned.
277. Find the Celebrity. The first step is to find the celebrity candidate. That is, we want to use one pass to find out all elements that violate the rule. So we will use two pointers to compare two index each time and exclude one of them that violates the rule. This is how I compare these two: if left knows right, left is not celebrity, move left++; if left doesn't know right, right is not celebrity, move right--. While loop this process till left == right. Now we find the candidate, which has not yet violated any celebrity rules. But since it has not yet been compared with all other element, we will want to make sure of that. Now we use another one pass to compare all other elements with the candidate. We will need to make sure 1) the celebrity doesn't know any of them; 2) all other elements should know the celebrity. If any of these rule are violated, the candidate is not celebrity and we just return -1.
189. Rotate Array. Actually this is a reverse problem. The tip is to find the rotate pivot, then reverse the first and second subarray. At last reverse the whole array so that we have result. This is just a trick to remember.
|
|