第 10 周 ARTS

一. Algorithm

本周做了一道数组相关的题目: 26. Remove Duplicates from Sorted Array

Given a sorted array nums, remove the duplicates in-place such that each element appear only once 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.

要求给定一个有序数组,要求计算出所有元素的未重复个数,并将所有元素无重复的加到数组最前面。同时给定了一个前提,空间复杂度为 O(1) 即数组只能就地操作,不能创建新的数组。

既然不能用新的数组或者容器进行过滤,基本思路就是遍历数组元素,每个元素与前一个元素比较,若不相等则表示该元素第一次出现,将数组对应位置上的元素值设置为当前元素即可。代码如下:


public class Solution {
    public static int removeDuplicates(int[] nums) {

        int numsLength = nums.length;

        if (numsLength == 0 || numsLength == 1) {
            return numsLength;
        }

        int result = 1;

        for (int i = 1; i < numsLength; i++) {
            if (nums[i] != nums[i - 1]) {
                nums[result ++] = nums[i];
            }
        }
        return result;
    }
}

比较简单的一到数组相关的题目,最终空间复杂度为 O(1),时间复杂度为 O(n).另外最近也看了极客时间出的专栏 《数据结构与算法之美》数组相关的一节。感觉这个专栏最大的好处是给限定了一个范围,之前耗子哥给出的练级攻略是终极解决方案,但提到的几本书都是大部头并且有一定的难度,因此也需要长久的坚持与努力,因为日常生活工作中可能拿不出那么多的时间,因此根据这个专栏提到的十个数据结构、十个算法以及相关的实战讲解,先将最普遍的、基础的数据结构、算法以及分析方法学会在去啃那些大部头可能会更加的高效,也更容易坚持下去。

二. Review

本周读了耗子叔专栏中推荐的一篇文章 Instagram Engineering’s 3 rules to a scalable cloud application architecture

简单讲解了 Instagram 的三条架构法则:

使用经过验证的稳定可靠的技术

作者提到 Mike 关注了 Facebook, Netflix, Twitter 等公司使用的技术,学习比较后发现基于微服务的架构更加的适合团队,通过该架构使得团队具有更好的灵活性。

不要重复造轮子

作者提到技术是帮助我们实现想要的功能的工具,而不是一个需要时刻照看的 Baby。当已经有现成的技术存在时,我们应该利用其优势去构建想要的产品和功能。

保持简洁

作者简单说了 Mike 在不断的对抗复杂性,通过完成一个个简单的小的需求并最后完成了整个系统全球所需的决策。

作者简单描述了 Instagram 的三个原则,但是对于细节没有过多的介绍。不清楚 Mike 有没有相关的演讲视频或者文章,读其本人的更加详细的讲解启发应该会更大吧。

三. Tip

分享一个维护 LogStash 配置文件的小技巧吧,开始在维护 LogStash 的配置文件时,其结构如下:

input {

}

filter {

}

output {
}

随着输入的增多,往往伴随的不同的输出和不同的过滤逻辑,最终可能导致配置文件越来越大变得越来越难以维护。看配置文件的参数解释

$ ./logstash --help
Usage:
    bin/logstash [OPTIONS]

Options:

    -f, --path.config CONFIG_PATH Load the logstash config from a specific file
                                  or directory.  If a directory is given, all
                                  files in that directory will be concatenated
                                  in lexicographical order and then parsed as a
                                  single config file. You can also specify
                                  wildcards (globs) and any matched files will
                                  be loaded in the order described above.

可以看到其可以配置为一个目录,如果是目录,其会将目录下的所有文件按照字典顺序拼接为一个配置文件。这样我们可以很方便的将不同用途的配置放到不同的文件中,方便管理。

四. Share

国庆期间一直在外面,没有什么深入的思考和分享,唯一最大的感受就是在这种人山人海的节假日出行,远不如在安静的咖啡馆泡着读书、敲代码来的充实有趣。分享一下 Python 之禅吧,

在 Python 终端输入 import this即可出现。

(ELK) ➜  arts  git:(master) ✗ ipython
Python 3.6.1 (v3.6.1:69c0db5050, Mar 21 2017, 01:21:04)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
发布了46 篇原创文章 · 获赞 21 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/Ahri_J/article/details/105405272