Published on

ARTS 第8周

Authors

Algorithm

package org.nocoder.leetcode.solution;

/**
 * https://leetcode.com/problems/string-to-integer-atoi/description/
 * <p>
 * 8. String to Integer (atoi)
 * <p>
 * Implement atoi which converts a string to an integer.
 * <p>
 * The function first discards as many whitespace characters as necessary until the first non-whitespace character is found.
 * Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as
 * possible, and interprets them as a numerical value.
 * <p>
 * The string can contain additional characters after those that form the integral number, which are ignored and have no
 * effect on the behavior of this function.
 * <p>
 * If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists
 * because either str is empty or it contains only whitespace characters, no conversion is performed.
 * <p>
 * If no valid conversion could be performed, a zero value is returned.
 * <p>
 * Note:
 * <p>
 * Only the space character ' ' is considered as whitespace character.
 * Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range:
 * [−2 31,  2 31 − 1]. If the numerical value is out of the range of representable values, INT_MAX (2 31 − 1) or
 * INT_MIN (−2 31) is returned.
 * Example 1:
 * <p>
 * Input: "42"
 * Output: 42
 * Example 2:
 * <p>
 * Input: "   -42"
 * Output: -42
 * Explanation: The first non-whitespace character is '-', which is the minus sign.
 * Then take as many numerical digits as possible, which gets 42.
 * Example 3:
 * <p>
 * Input: "4193 with words"
 * Output: 4193
 * Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
 * Example 4:
 * <p>
 * Input: "words and 987"
 * Output: 0
 * Explanation: The first non-whitespace character is 'w', which is not a numerical
 * digit or a +/- sign. Therefore no valid conversion could be performed.
 * Example 5:
 * <p>
 * Input: "-91283472332"
 * Output: -2147483648
 * Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
 * Thefore INT_MIN (−231) is returned.
 *
 * @author jason
 * @date 18/8/26.
 */
public class StringToInteger {

    public static int myAtoi(String str) {
        if (str == null || "".equals(str = str.trim())) {
            return 0;
        }

        // 字母开头
        if (str.matches("^[a-zA-Z]\\w{0,31}$")) {
            return 0;
        }

        int i = 0;
        char flag = '+';
        // 判断正负
        if (str.charAt(i) == '-'){
            flag = '-';
            i++;
        } else if (str.charAt(i) == '+') {
            flag = '+';
            i++;
        }

        double result = 0;
        //计算对应的整数
        while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
            result = result * 10 + (str.charAt(i) - '0');
            i++;
        }

        if (flag == '-') {
            result = -result;
        }

        // 越界处理
        if (result > Integer.MAX_VALUE) {
            return Integer.MAX_VALUE;
        } else if (result < Integer.MIN_VALUE) {
            return Integer.MIN_VALUE;
        }

        return (int) result;
    }

    public static void main(String[] args) {
        System.out.println(myAtoi("123abc"));
        System.out.println(myAtoi("  "));
        System.out.println(myAtoi(null));
        System.out.println(myAtoi("hello123"));
        System.out.println(myAtoi("9999999999999999999999999"));
        System.out.println(myAtoi("-9999999999999999999999999"));
        System.out.println(myAtoi("1000"));
        System.out.println(myAtoi("0001"));
        System.out.println(myAtoi("-888"));
        System.out.println(myAtoi("   9999    2222"));
    }
}

Review

https://docs.docker.com/network/host/ http://cizixs.com/2016/06/12/docker-network-modes-explained

Tip

docker container 使用主机网络

背景

多个docker容器分布在不同的宿主机上,并且都要通过 eureka 进行服务调用,跨主机调用时,如果使用的是docker 容器内部网络,那么在调用不同宿主机上的服务时,就会出现找不到服务的现象,通过使用主机网络,解决了这个问题

在docker run 后追加长命令 --net=host 即可使docker container 使用宿主机网络

Share

Observer Pattern

观察者模式也称为发布订阅模式,当一个对象的状态发生变化时,通知其他对象,其他对象根据变化做相应的操作。例如,book表的数量内容发生变化后,相应的数据统计表要根据book数量进行增量统计,这时候book表对应的对象就是被观察者,而相关的统计表对应的实体对象就是观察者。

被观察者(发布者)

package org.nocoder.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 被观察者(发布者)
 */
public class Subject {

    /**
     * 观察者
     */
    private List<Observer> observers = new ArrayList<Observer>();

    private int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        // 状态变更通知已订阅的观察者
        notifyAllObservers();
    }

    /**
     * 添加订阅
     * @param observer
     */
    public void addObserver(Observer observer){
        observers.add(observer);
    }

    /**
     * 通知已订阅的观察者
     */
    public void notifyAllObservers(){
        for (Observer observer : observers) {
            observer.update();
        }
    }
}

观察者抽象类

package org.nocoder.observer;

/**
 * 观察者抽象类
 */
public abstract class Observer {

    protected Subject subject;

    public abstract void update();
}

BinaryObserver

package org.nocoder.observer;

public class BinaryObserver extends Observer {

    public BinaryObserver(Subject subject){
        this.subject = subject;
        this.subject.addObserver(this);
    }

    @Override
    public void update() {
        System.out.println(this.getClass().getName() + ": " + Integer.toBinaryString(subject.getState()));
    }
}

HexaObserver

package org.nocoder.observer;

public class HexaObserver extends Observer {

    public HexaObserver(Subject subject){
        this.subject = subject;
        this.subject.addObserver(this);
    }

    @Override
    public void update() {
        System.out.println(this.getClass().getName() + ": " + Integer.toHexString(subject.getState()));
    }
}

OctalObserver

package org.nocoder.observer;

public class OctalObserver extends Observer {

    public OctalObserver(Subject subject){
        this.subject = subject;
        this.subject.addObserver(this);
    }

    @Override
    public void update() {
        System.out.println(this.getClass().getName() + ": " + Integer.toOctalString(subject.getState()));
    }
}
package org.nocoder.observer;

public class Demo {
    public static void main(String[] args) {
        // 被观察者
        Subject subject = new Subject();

        // 观察者
        new BinaryObserver(subject);
        new OctalObserver(subject);
        new HexaObserver(subject);

        // 触发事件通知已经订阅的观察者
        subject.setState(15);

        // 触发事件通知已经订阅的观察者
        subject.setState(100);

        // 观察者消费订阅,执行 update() 方法
    }
}