Coursera Algorithms Week1 Homework Percolation

这篇博客介绍了Coursera算法课程第一周的Percolation作业,涉及问题规范分析、Percolation数据类型、Monte Carlo模拟、运行时间和内存使用分析。博主强调了API设计的重要性,并分享了PMD代码规范,如使用final修饰private属性,遵循变量命名规则,以及避免使用制表符等。
摘要由CSDN通过智能技术生成

这个问题的文档让我学到很多,一个完整的问题规范,自己写程序也要这样分析。
MODEL
PROBLEM
边界 throw exception 情况
(PERCOLATION DATA TYPE)
(MONTE CARLOSIMULATION)
ANALYSIS OF RUNNING TIME AND MEMORY USAGE

最后的检测中也学到很多东西。
1.API一定不能少,这也是工作里最最重要的一条。
2.PMD也要重视:

  • private属性要不要final
  • 变量需lowCase加camelCase
  • 缩进里别用制表符
  • 逗号前面不要空格
//Percolation.java
import edu.princeton.cs.algs4.WeightedQuickUnionUF;

/**
 * Builds a N*N sized WeightedQuickUnionUF grid to mock create a simple percolation system. Initially all
 * nodes in the grid are blocked and must be opened. The grid is considered to percolate when there is a
 * connection from an open node on the top row to an open node on the bottom row.
 *
 * Whether a node is open or not is kept in an array. All connections are done through a WeightedQuickUnionUF object.
 *
 * We have a second WeightedQuickUnionUF object for checking fullness so as to not run into the backwash issue.
 */
public class Percolation {
   
    private WeightedQuickUnionUF grid;
    private WeightedQuickUnionUF full;
    private int N;
    private int top;
    private int bottom;
    private boolean[] openNodes;

    /**
     * Initialises an N * N WeightedQuickUnionUF object plus two extra nodes for the virtual top and virtual bottom
     * nodes. Creates an internal boolean array to keep track of whether a node is considered open or not.
     *
     * Also initialises a second N * N WeightedQuickUnionUF object plus one extra node as a second collection to check
     * for fullness and avoid the backwash issue.
     *
     * @param N The dimensions of the grid
     */
    public Percolation(int N) {
   
        if (N <= 0) {
   
            throw new java.lang.IllegalArgumentException("Woah, N must be greater than zero");
        }

        grid = new WeightedQuickUnionUF(N * N + 2);
        full = new WeightedQuickUnionUF(N * N + 1);

        this.N = N;

        top = getSingleArrayIdx(N, N) + 1;
        bottom = getSingleArrayIdx(N, N) + 2;

        openNodes = new boolean[N * N];
    }

    /**
     * Converts an index for a 0-based array from two grid coordinates which are 1-based. First checks to see if the
     * coordinates are out of bounds.
     *
     * @param i Node row
     * @param j Node column
     * @return
     */
    private int getSingleArrayIdx(int i, int j) {
   
        doOutOfBoundsCheck(i, j);

        return (N * (i - 1) + j) - 1;
    }

    /**
     * Checks to see if two given coordinates are valid. I.e - a coodinate is valid if it is greater than 0
     * and smaller than the dimensions of the parent grid
     *
     * @param i Node row
     * @param j Node column
     * @return
     */
    private boolean isValid(int i, int j) {
   
        return i > 0
                && j > 0
                && i <= N
                && j <= N;
    }

    /**
     * Throws an error if the given coordinates are valid (the valid state comes from the `isValid` function
     * @param i Node row
     * @param j Node column
     */
    private void doOutOfBoundsCheck(int i, int j) {
   
        if (!isValid(i, j)) {
   
            throw new IndexOutOfBoundsException("Boo! Values are out of bounds");
        }
    }

    /**
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值