示例#1
0
        public void RunProblem()
        {
            var t = new NumMatrix(new int[][]
            {
                new int[] { 3, 0, 1, 4, 2 },
                new int[] { 5, 6, 3, 2, 1 },
                new int[] { 1, 2, 0, 1, 5 },
                new int[] { 4, 1, 0, 1, 7 },
                new int[] { 1, 0, 3, 0, 5 }
            });

            var temp = t.SumRegion(2, 1, 4, 3);

            if (temp != 8)
            {
                throw new Exception();
            }

            temp = t.SumRegion(1, 1, 2, 2);
            if (temp != 11)
            {
                throw new Exception();
            }

            temp = t.SumRegion(1, 2, 2, 4);
            if (temp != 12)
            {
                throw new Exception();
            }

            t = new NumMatrix(new int[][] { });
        }
示例#2
0
 public void Run()
 {
     var str      = "[[3,0,1,4,2],[5,6,3,2,1],[1,2,0,1,5],[4,1,0,1,7],[1,0,3,0,5]]";
     var m        = JsonSerializer.Deserialize <int[][]>(str);
     var instance = new NumMatrix(m);
     var s        = instance.SumRegion(2, 1, 4, 3);
 }
        public void NumMatrixTests()
        {
            /**
             * Your NumMatrix object will be instantiated and called as such:
             * NumMatrix obj = new NumMatrix(matrix);
             * int param_1 = obj.SumRegion(row1,col1,row2,col2);
             *
             * Given matrix = [
             * [3, 0, 1, 4, 2],
             * [5, 6, 3, 2, 1],
             * [1, 2, 0, 1, 5],
             * [4, 1, 0, 1, 7],
             * [1, 0, 3, 0, 5]
             * ]
             *
             * sumRegion(2, 1, 4, 3) -> 8
             * sumRegion(1, 1, 2, 2) -> 11
             * sumRegion(1, 2, 2, 4) -> 12
             */
            int[][] array2D = new int[][]
            {
                new int[] { 3, 0, 1, 4, 2 },
                new int[] { 5, 6, 3, 2, 1 },
                new int[] { 1, 2, 0, 1, 5 },
                new int[] { 4, 1, 0, 1, 7 },
                new int[] { 1, 0, 3, 0, 5 },
            };
            NumMatrix obj = new NumMatrix(array2D);

            int param_1 = obj.SumRegion(2, 1, 4, 3);

            param_1 = obj.SumRegion(1, 1, 2, 2);
            param_1 = obj.SumRegion(1, 2, 2, 4);
        }
        public void TestMethod3()
        {
            NumMatrix nm       = new NumMatrix(matrix);
            int       actual   = nm.SumRange(1, 2, 2, 4);
            int       expected = 12;

            Assert.Equal(expected, actual);
        }
        public void TestMethod1()
        {
            NumMatrix nm       = new NumMatrix(matrix);
            int       actual   = nm.SumRange(2, 1, 4, 3);
            int       expected = 8;

            Assert.Equal(expected, actual);
        }
示例#6
0
        public void Test1(int row1, int col1, int row2, int col2, int expected)
        {
            // Arrange
            var numMatrix = new NumMatrix(new[] { new[] { 3, 0, 1, 4, 2 }, new[] { 5, 6, 3, 2, 1 }, new[] { 1, 2, 0, 1, 5 }, new[] { 4, 1, 0, 1, 7 }, new[] { 1, 0, 3, 0, 5 } });

            // Act
            var value = numMatrix.SumRegion(row1, col1, row2, col2);

            // Assert
            Assert.AreEqual(expected, value);
        }
示例#7
0
 static void Main(string[] args)
 {
     int[,] matrix = new int[4, 5] {
     {3, 0, 1, 4, 2},
     {5, 6, 3, 2, 1},
     {1, 2, 0, 1, 5},
     {4, 1, 0, 1, 7},
     };
     NumMatrix m = new NumMatrix(matrix);
     Console.WriteLine(m.SumRegion(1, 1, 2, 2));
     Console.WriteLine(matrix.GetLength(0));
 }
示例#8
0
        public static void Run()
        {
            var st = new Solution304();

            Console.WriteLine("Start: {0}", DateTime.Now);

            NumMatrix numMatrix = new NumMatrix(new int[,] { { 3, 0, 1, 4, 2 }, { 5, 6, 3, 2, 1 }, { 1, 2, 0, 1, 5 }, { 4, 1, 0, 1, 7 }, { 1, 0, 3, 0, 5 } });
            Console.WriteLine(numMatrix.SumRegion(2, 1, 4, 3));
            Console.WriteLine(numMatrix.SumRegion(1, 1, 2, 2));
            Console.WriteLine(numMatrix.SumRegion(1, 2, 2, 4));

            Console.WriteLine("End: {0}", DateTime.Now);
        }
示例#9
0
        static void Main(string[] args)
        {
            int[,] matrix = new int[4, 5] {
                { 3, 0, 1, 4, 2 },
                { 5, 6, 3, 2, 1 },
                { 1, 2, 0, 1, 5 },
                { 4, 1, 0, 1, 7 },
            };
            NumMatrix m = new NumMatrix(matrix);

            Console.WriteLine(m.SumRegion(1, 1, 2, 2));
            Console.WriteLine(matrix.GetLength(0));
        }
        public void SetUp()
        {
            var matrix = new int[][]
            {
                new[] { 3, 0, 1, 4, 2 },
                new[] { 5, 6, 3, 2, 1 },
                new[] { 1, 2, 0, 1, 5 },
                new[] { 4, 1, 0, 1, 7 },
                new[] { 1, 0, 3, 0, 5 },
                new[] { 2, 1, 4, 3 },
                new[] { 1, 1, 2, 2 },
                new[] { 1, 2, 2, 4 }
            };

            _numMatrix = new NumMatrix(matrix);
        }
        public void NumMatrix()
        {
            //sumRegion(2, 1, 4, 3) -> 8
            //sumRegion(1, 1, 2, 2)-> 11
            //sumRegion(1, 2, 2, 4)-> 12
            NumMatrix obj = new NumMatrix(matrix);

            foreach (RangeSumQuery2DTestData testData in TestDataList)
            {
                int sum = obj.SumRegion(testData.InputArray[0], testData.InputArray[1], testData.InputArray[2],
                                        testData.InputArray[3]);

                Console.WriteLine("Test output: " + sum);

                Assert.AreEqual(testData.OutputInt, sum);
            }
        }
    private void NumMatrix_Main(string[] ope, string[] para)
    {
        if (ope.Length != para.Length)
        {
            return;
        }
        if (ope.Length <= 0 || para.Length <= 0)
        {
            return;
        }

        string[] matrix_str = para[0].Replace("[[", "").Replace("]]", "").Split("],[", StringSplitOptions.None);
        int[][]  matrix     = new int[matrix_str.Length][];
        for (int n = 0; n < matrix.Length; n++)
        {
            matrix[n] = set_array_int(matrix_str[n].Split(','));
        }

        NumMatrix nm = new NumMatrix(matrix);

        for (int i = 0; i < ope.Length; i++)
        {
            if (ope[i] == "NumMatrix")
            {
                Console.WriteLine("NumMatrix()");
            }
            else if (ope[i] == "sumRegion")
            {
                string[] flds = para[i].Split(',');
                int      row1 = int.Parse(flds[0]);
                int      col1 = int.Parse(flds[1]);
                int      row2 = int.Parse(flds[2]);
                int      col2 = int.Parse(flds[3]);
                int      sum  = nm.SumRegion(row1, col1, row2, col2);
                Console.WriteLine("sumRegion(" + row1.ToString()
                                  + ", " + col1.ToString()
                                  + ", " + row2.ToString()
                                  + ", " + col2.ToString()
                                  + ") = " + sum.ToString());
            }
        }
    }
示例#13
0
        public void Test1()
        {
            var grid1 = new int[3][]
            {
                new int[3] {
                    1, 1, 1
                },
                new int[3] {
                    1, 1, 1
                },
                new int[3] {
                    1, 1, 1
                },
            };

            var matrix = new NumMatrix(grid1);

            matrix.SumRegion(0, 0, 2, 2).Should().Be(9);
            matrix.SumRegion(1, 1, 2, 2).Should().Be(4);
        }
        public void RangeSum2DSuccess()
        {
            int[][] input = new int[][]
            {
                new int[] { 3, 0, 1, 4, 2 },
                new int[] { 5, 6, 3, 2, 1 },
                new int[] { 1, 2, 0, 1, 5 },
                new int[] { 4, 1, 0, 1, 7 },
                new int[] { 1, 0, 3, 0, 5 }
            };


            int result;

            result = new NumMatrix(input).SumRegion(2, 1, 4, 3);
            Assert.AreEqual(result, 8);

            result = new NumMatrix(input).SumRegion(1, 1, 2, 2);
            Assert.AreEqual(result, 11);

            result = new NumMatrix(input).SumRegion(1, 2, 2, 4);
            Assert.AreEqual(result, 12);
        }
示例#15
0
 public Matrix(T[,] existingArray)
 {
     this.NumMatrix = existingArray;
     this.Rows      = NumMatrix.GetLength(0);
     this.Cols      = NumMatrix.GetLength(1);
 }