ARTS 第11周 零拷贝

Algorithm

本周完成的算法题: Jump Game

题目要求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

Example 1:

Input: [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
Example 2:

Input: [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum
             jump length is 0, which makes it impossible to reach the last index.

这个题的解可以使用贪心算法来解决。但是具体细节与Jump GameII略有不同。对于实例中给出的第二种情况,当我们到达index 3以后,再也无法往后继续跳,所以我们要在循环条件中增加i <= nextMaxIndex

1
2
3
4
5
6
7
8
9
10
11
12
public boolean canJump(int[] nums) {
    int nextMaxIndex = 0;
    //注意到条件 i <= nextMaxIndex
    for (int i = 0; i <= nums.length - 1 && i <= nextMaxIndex; i++) {

        if(nextMaxIndex < (i + nums[i])){
            nextMaxIndex = (i + nums[i]);
        }
        nextMaxIndex = Math.max(nextMaxIndex, i + nums[i]);
    }
    return nextMaxIndex >= nums.length - 1;
}

Review

本周Review 10 Bad Habits of Unsuccessful People

成功没有捷径、成功各不相同。但是不成功却大体是类似的,所以我们要做好的将那些导致我们不成功的做法、习惯成生活中剔除,避免不成功Just try to avoid being unsuccessful

10个坏习惯:

  1. Always being distracted(经常分心,无法集中注意力)
  2. Only talking the talk(光说不做)
  3. Spending time with the wrong people
  4. Always focusing on the negative
  5. Procrastinating (拖延)
  6. Not listening to others
  7. Giving in to laziness
  8. Not being curious (没有好奇心)
  9. Not being nice
  10. Giving up

Tip

零拷贝(Zero Copy)

零拷贝

参考资料:

零拷贝介绍 Linux 中的零拷贝技术,第 1 部分 Linux 中的零拷贝技术,第 2 部分 零拷贝底层实现原理 It’s all about buffers: zero-copy, mmap and Java NIO

Share

使用 Akka 的 Actor 模型和领域驱动设计构建反应式系统