ARTS-WEEK 3

start a blog

Posted by Eric Jin on 2018-08-20

Algorithm

Leetcode 第20题,简单。 决定先按照难易程度开始写leetcode,尽量能够每天或者两天写一道题目。一周记录一道题目到ARTS上面来。

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.

Example 1:
Input: "()"
Output: true

Example 2:
Input: "()[]{}"
Output: true

Example 3:
Input: "(]"
Output: false

Example 4:
Input: "([)]"
Output: false

Example 5:
Input: "{[]}"
Output: true

这题在旁边的related Topics 上面显示的是Stringstack,所以可以猜测到本题的思路应该是从stack入手了。stack的特点是FILO(First in Last Out),先进后出。
这些括号的排列也是对称的,如果是左括号,我们把右括号push()到栈中;是右括号类的,就pop() 出来,如果和当前的右括号是一类的,就继续,如果不是,就说明他们括号暂时不对称,就可以直接返回了。以下是代码实现:

solution
public boolean isValid(String s) {
    Stack<Character> stack = new Stack<>();
    for (Character c : s.toCharArray()){
        switch (c) {
            case  '(':
                stack.push(')');
                break;
            case  '{':
                stack.push('}');
                break;
            case  '[':
                stack.push(']');
                break;
            default:
                if (stack.isEmpty() || stack.pop() != c) {
                    return false;
                }
        }
    }
    return stack.isEmpty();
}
test
@Test
public void isValid() {
    ValidParentheses solution = new ValidParentheses();

    String s1 = "()";
    String s2 = "()[]{}";
    String s3 = "(]";
    String s4 = "([)]";
    String s5 = "{[]}";

    assertEquals(true,solution.isValid(s1));
    assertEquals(true,solution.isValid(s2));
    assertEquals(false,solution.isValid(s3));
    assertEquals(false,solution.isValid(s4));
    assertEquals(true,solution.isValid(s5));
}

Review

Follow these simple rules and you’ll become a Git and GitHub master
这篇文章讲了三点准则最好遵守,这样能够让自己管理代码更加方便,也能更加适应团队的协作。毕竟git 就是为团队而生。

  1. Create a Git repository for every new project
  2. Create a new branch for every new feature
  3. Use Pull Requests to merge code to Master

三点准则,目前能做到的就是第一点了。第二点目前做的不是很好,有的时候自己一个人做项目的时候,直接就在master 分支开始干活了。 三点准则都是为了更好的记录信息,更好的管理代码。记下!

Tip

由于部署项目的时候,一台服务器常常需要开几个tomcat,这种情况下,能把项目独立于tomcat之外,不在webapps里面是最好的。因此学习到了tomcat的三种部署方案。

  1. 直接将编译好的项目放在tomcat的webapps里面,也可以你直接打成WAR包,放入webapps里面,这种情况,tomcat启动的时候,会把它当作自身的项目,然后启动。
  2. 打开tomcat下conf/server.xml,在<Host> </Host>标签之间输入项目配置信息
    <Context path="/myProject" docBase="D:/myProject" reloadable="true" />
    path:浏览器访问时的路径名
    docBase:web项目的WebRoot所在的路径,注意是WebRoot的路径,不是项目的路径。其实也就是编译后的项目。
    reloadble:设定项目有改动时,tomcat是否重新加载该项目。
  3. 进入到 tomcat\conf\Catalina\localhost 目录,新建一个 [projectName].xml 文件,然后在那个新建的 xml 文件中,增加下面配置语句(和上面的是一样的,但是不需要 path 配置,加上也没什么用)
    <Context docBase="D:/myProject" reloadable="true" />

总结
第一种是我们平常常用的,比较普通的部署方法,但是一旦你一个项目需要多个tomcat部署,这个方法就不好管理代码了。
第二种第二种方法直接在 server.xml 文件中配置,但是从 tomcat5.0版本开始后,server.xml 文件作为 tomcat 启动的主要配置文件,一旦 tomcat 启动后,便不会再读取这个文件,因此无法再 tomcat 服务启动后发布 web 项目。
第三种第三种方法是最好的,每个项目分开配置,tomcat 将以\conf\Catalina\localhost 目录下的 xml 文件的文件名作为 web 应用的上下文路径,而不再理会 <Context>中配置的 path 路径,因此在配置的时候,可以不写 path。

最后
附上tomcat 的官网文档,感觉文档还是要常读。

Share

If you’re a developer, you should start blogging — and here’s why
这周分享一篇关于为什么写博客的文章,我也是这几周加入耗子叔的ARTS 活动才开始写博客的。以前也一直有过这个想法,可是主题一直不明确,想要把自己所学的东西记录下来,可是也总是记录在了oneNote 上面,如果要重新整理成博客的话,需要另外写示例代码,需要整理等等,懒惰使我在博客园也仅仅记录了几篇自己最初学习springmybatishibernate的时候的一些错误记录。这次的博客算是有了一个主题了,先从ARTS开始记录,以后深入学习一些东西的时候,也可以像那些大佬一样记录一下自己的学习进程。 这篇文章讲诉了几个作者从写博学习到的东西

  1. Time Management

time management is not about when you do one thing or the other. It’s about having strategies that help you make the best use of the time you have

  1. Design thinking

Design thinking is an iterative approach to problem solving that intentionally seeks out people with different perspectives, knowledge, skills and experience and has them work together to create a practical solution for a real-world problem - Tech Target

  1. Openness to feedback
  2. Communication skills

对于任何事情,都是如此,我们不需要足够好了才开始,我们需要开始去变得更好!

You don’t have to be great to start, but you have to start to be great.

写博客是一个组织语言的过程,就如我们代码重构一样。让我们的思维更加有效率,让知识变得更易搜索和回忆。