Published on

ARTS 第5周

Authors

Algorithm

Longest Palindromic Substring

package org.nocoder.leetcode.solution;

/**
 * 5. Longest Palindromic Substring
 * <p>
 * Given a string s, find the longest palindromic substring in s.
 * <p>
 * You may assume that the maximum length of s is 1000.
 * <p>
 * Example 1:
 * Input: "babad"
 * Output: "bab"
 * Note: "aba" is also a valid answer.
 * <p>
 * Example 2:
 * Input: "cbbd"
 * Output: "bb"
 *
 * @author jason
 * @date 18/8/4.
 */
public class LongestPalindromicSubstring {
    public static String longestPalindrome(String s) {
        if (s == null || s.length() == 0) {
            return s;
        }
        boolean[][] palindrome = new boolean[s.length()][s.length()];
        String result = "";
        int maxLength = 0;
        for (int i = s.length() - 1; i >= 0; i--) {
            for (int j = i; j < s.length(); j++) {
                if ((s.charAt(i) == s.charAt(j)) && (j - i <= 2 || palindrome[i + 1][j - 1])) {
                    palindrome[i][j] = true;
                    if (maxLength < j - i + 1) {
                        maxLength = j - i + 1;
                        result = s.substring(i, j + 1);
                    }
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(LongestPalindromicSubstring.longestPalindrome("a"));
        System.out.println(LongestPalindromicSubstring.longestPalindrome("aa"));
        System.out.println(LongestPalindromicSubstring.longestPalindrome("aaa"));
        System.out.println(LongestPalindromicSubstring.longestPalindrome("cbbd"));
        System.out.println(LongestPalindromicSubstring.longestPalindrome("babad"));
    }
}

Review

Demystifying Dynamic Programming

https://medium.freecodecamp.org/demystifying-dynamic-programming-3efafb8d4296

Demystifying Dynamic Programming

How to construct & code dynamic programming algorithms

Dynamic Programming Defined

Dynamic programming amounts to breaking down an optimization problem into simpler sub-problems, and storing the solution to each sub-problem so that each sub-problem is only solved once.

Sub-problems on Sub-problems on Sub-problems

Sub-problems are smaller versions of the original problem. In fact, sub-problems often look like a reworded version of the original problem. If formulated correctly, sub-problems build on each other in order to obtain the solution to the original problem.

Dynamic Programming Process

Step 1: Identify the sub-problem in words. Step 2: Write out the sub-problem as a recurring mathematical decision. Step 3: Solve the original problem using Steps 1 and 2. Step 4: Determine the dimensions of the memoization array and the direction in which it should be filled. Step 5: Code it!

Tip

Docker spring boot 项目使用位于容器外(宿主机)的配置文件

springboot 允许我们把配置文件放置在与jar文件同路径的config目录下,结合docker的数据卷挂载,可以把容器内的config目录映射到宿主机的指定目录下,从而实现容器内的项目使用宿主机目录下的配置文件。

pom.xml dockerfile 插件配置

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>dockerfile-maven-plugin</artifactId>
    <version>1.4.3</version>
    <configuration>
        <repository>192.168.28.121:9090/my-service</repository>
        <tag>${project.version}</tag>
        <useMavenSettingsForAuth>true</useMavenSettingsForAuth>
        <buildArgs>
            <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
        </buildArgs>
    </configuration>
</plugin>

在项目根目录下编写 Dockerfile 文件,命名为 Dockerfile

FROM frolvlad/alpine-oraclejdk8:latest
ARG JAR_FILE
ADD ${JAR_FILE} /usr/local/my-service/app.jar
RUN mkdir /usr/local/my-service/config
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/usr/local/my-service/app.jar", "--spring.config.location=/usr/local/my-service/config/application.properties"]

运行docker容器,使用 -v 参数挂载数据卷,指定容器外配置文件的目录,例如:

docker run -it -p 8080:8080 -v /Users/jason/my-service/config/:/usr/local/my-service/config/ 192.168.28.121:9090/my-service:1.0

上面的例子中 -v /Users/jason/my-service/config/:/usr/local/my-service/config/ 冒号左边的路径代表的是宿主机,冒号右边的路径代表的是docker 容器内的路径

Share

Jenkins Pipeline script

Node:一个Node就是一个Jenkins节点,可以是Master,也可以是Slave,是Pipeline中具体Step的运行环境。

Stage:一个Pipeline有多个Stage组成,每个Stage包含一组Step。注意一个Stage可以跨多个Node执行,即Stage实际上是Step的逻辑分组。

Step:是最基本的运行单元,可以是创建一个目录、从代码库中checkout代码、执行一个shell命令、构建Docker镜像、将服务发布到Kubernetes集群中。Step由Jenkins和Jenkins各种插件提供。

将node、stage、step的Groovy DSL写在一个Jenkinsfile文件中,Jenkinsfile会被放到代码库的根目录下。下面是一个Jenkinsfile的Helloworld的例子:

node {
   stage('prepare') {
       echo 'prepare step1'
       echo 'prepare step2'
   }
   stage('build') {
       echo 'build step1'
       echo 'build step2'
   }
   stage('deploy') {
       echo 'deploy step1'
       echo 'deploy step2'
   }
}

下面是一个实际使用的例子,从gitlab检出代码,使用maven命令打包,构建docker镜像并推送到镜像仓库,然后使用ssh连接到测试服务器运行docker容器

node {
    stage('Checkout From Gitlab') {
        git credentialsId: 'gitlab-passwd', url: 'http://192.168.28.216/yuntu/eureka-server.git', branch: 'dev'
    }

    stage('Maven Install'){
        def mvnHome = tool name: 'maven3', type: 'maven'
        def mvnCMD = "${mvnHome}/bin/mvn"
        sh "${mvnCMD} clean install"
    }


    stage('Build And Push Docker Image'){
        def mvnHome = tool name: 'maven3', type: 'maven'
        def mvnCMD = "${mvnHome}/bin/mvn"
        sh "${mvnCMD} dockerfile:build"
        sh "${mvnCMD} dockerfile:push"
    }

    stage('Run Docker Container On Test Server'){
        def dockerLogin = 'docker login -u admin -p admin123 192.168.28.121:9090'
        def dockerPull = 'docker  pull 192.168.28.121:9090/eureka-server:1.0'
        def dockerStop = 'docker stop eureka-server'
        def dockerRm = 'docker rm eureka-server'
        def dockerRun = 'docker run -p 1112:1111 --name eureka-server -d 192.168.28.121:9090/eureka-server:1.0'
        sshagent(['jason-local-vm']) {
            sh "ssh -o StrictHostKeyChecking=no jason@192.168.28.121 ${dockerLogin}"
            sh "ssh -o StrictHostKeyChecking=no jason@192.168.28.121 ${dockerPull}"
            sh "ssh -o StrictHostKeyChecking=no jason@192.168.28.121 ${dockerStop}"
            sh "ssh -o StrictHostKeyChecking=no jason@192.168.28.121 ${dockerRm}"
            sh "ssh -o StrictHostKeyChecking=no jason@192.168.28.121 ${dockerRun}"
        }
    }
}