Published on

ARTS 第1周

Authors

Algorithm

Two Sum Sulution

Two Sum

package org.nocoder.leetcode.solution;

/**
 * 1.Two Sum 
 * Given an array of integers, return indices of the two numbers such that they add up to a specific target.
 * You may assume that each input would have exactly one solution, and you may not use the same element twice.
 * 
 * Example:
 * Given nums = [2, 7, 11, 15], target = 9,
 * Because nums[0] + nums[1] = 2 + 7 = 9,
 * return [0, 1].
 *
 * @author jason
 * @date 18/7/6.
 */
public class TwoSumSolution {

    public static int[] twoSum1(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if ((nums[i] + nums[j]) == target) {
                    return new int[]{i, j};
                }
            }
        }
        throw new IllegalArgumentException("No solution!");
    }
    
    public static int[] twoSum2(int[] nums, int target){
        Map<Integer, Integer> resultMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int result = target - nums[i];
            if(resultMap.containsKey(result)){
                return new int[]{resultMap.get(result), i};
            }
            resultMap.put(nums[i], i);
        }
        throw new IllegalArgumentException("No solution!");
    }
}


Review

Understanding MySQL Queries with Explain

https://www.exoscale.com/syslog/explaining-mysql-queries/

explain columns:

  • id (query id)
  • select_type (type of statement)
  • table (table referenced)
  • type (join type)
  • possible_keys (which keys could have been used)
  • key (key that was used)
  • key_len (length of used key)
  • ref (columns compared to index)
  • rows (amount of rows searched)
  • Extra (additional information)

The main points for long-term performance summarized:

  • create a sustainable data model that suits your company’s needs
  • choose the right form of database
  • use a software architecture that matches your product
  • go through regular iterations of looking at the structure of your queries and use explain on the more convoluted ones, optimize usage for your chosen database(s), also with regard to database updates and how they could affect you
  • choose the instances that best suit your application and database needs in accordance with performance and bandwidth

Tip

  • Springboot多模块项目,扫描不到另一个module的service,无法使用@Autowired注入。

    • 原因分析:SpringBoot默认在Application启动类的同级package开始往下扫描,另一个module的service包名在启动类包的上一级,所以扫描不到。
    • 解决办法:在springboot启动类上增加@ComponentScan(basePackages = { "com.a.b.c" })指定扫描的package,或者调整SpringBootApplication启动类的位置。
  • Mybatisorg.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

    • 原因分析:在idea下,maven默认到src/main/resources目录下拷贝xml文件,而我把mapper.xml放在了src/main/java的package下,编译后target下找不到mapper.xml文件,故抛出该异常
    • 解决方法:在pom.xml文件的build标签下增加resources。
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
    </resource>
</resources>

Share

Adapter Design Pattern in Java

Adapter Design Pattern in Java

架构师的自我修养

架构师的自我修养