两种方法求到一个N*M矩阵某个点的最小长度

package com.ybl.test.suanfa.java;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/** * Created by H1871 on 2018/7/19. */
public class Day2 {
    public static  int aa(int [][] a2) {
        int row = a2.length;//行
        int col = a2[0].length;
        for (int h = 1; h < row; h++) {
            a2[0][h] = a2[h - 1][0] + a2[h][0];
        }
        for (int k = 1; k < col; k++) {
            a2[0][k] = a2[0][k-1] + a2[0][k];
        }

        for (int l = 1; l < row; l++) {
            for (int g = 1; g < col; g++) {
                a2[l][g] = Math.min(a2[l - 1][g], a2[l][g-1])+a2[l][g];

            }
        }
        return a2[row-1][col-1];
    }
    public static void main(String[] args) {

        int[][] a2 = {
                {1, 3, 5, 9},
                {8, 1, 3, 4},
                {5, 0, 6, 1},
                {8, 8, 4, 0}
        };
        int row = a2.length;//行
        int col = a2[0].length;
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < a2.length; i++) {
            int a = 0;
            for (int j = 0; j < col; j++) {
                a = a2[i][j] + a;
            }
            list.add(a);
        }
        System.out.println(Collections.min(list));
        System.out.println("=========================");

        System.out.println(aa(a2));
    }



}
点赞