@@ -0,0 +1,1184 @@
+package com.freetymekiyan.algorithms.other;%0A%0A/**%0A * Given a matrix of 1's and 0's, return the number of ones in a random rectangle.%0A * %3Cp%3E%0A * Company: Facebook%0A */%0Apublic class GetNumberOfOnes %7B%0A%0A    private final int%5B%5D%5B%5D nums;%0A%0A    /**%0A     * Save cumulative sum.%0A     * The sum is the number of ones.%0A     * A rectangle can be represented by 4 other rectangles.%0A     */%0A    public GetNumberOfOnes(int%5B%5D%5B%5D matrix) %7B%0A        if (matrix == null %7C%7C matrix.length == 0 %7C%7C matrix%5B0%5D.length == 0) %7B%0A            throw new IllegalArgumentException(%22Input matrix should have at least 1 row and 1 column.%22);%0A        %7D%0A        int m = matrix.length;%0A        int n = matrix%5B0%5D.length;%0A        nums = new int%5Bm%5D%5Bn%5D;%0A        int num = 0;%0A        for (int r = 0; r %3C m; r++) %7B%0A            for (int c = 0; c %3C n; c++) %7B%0A                if (matrix%5Br%5D%5Bc%5D == 1) %7B%0A                    num++;%0A                %7D%0A                nums%5Br%5D%5Bc%5D = num;%0A            %7D%0A        %7D%0A    %7D%0A%0A    public int getNumberOfOnes(int%5B%5D topLeft, int%5B%5D bottomRight) %7B%0A        return nums%5BbottomRight%5B0%5D%5D%5BbottomRight%5B1%5D%5D - nums%5BbottomRight%5B0%5D%5D%5BtopLeft%5B1%5D%5D - nums%5BtopLeft%5B0%5D%5D%5BbottomRight%5B1%5D%5D + nums%5BtopLeft%5B0%5D%5D%5BtopLeft%5B1%5D%5D;%0A    %7D%0A%7D

add Get Number Of Ones