代码效率

时间:2016-10-12 18:39:23

标签: java json algorithm

我必须解决一个问题,我的矩阵中填充的自然数从1到n x m(行x列)。 取决于matrix( (1,1), (1,m), (n,1), (n,m) )direction(N,E,S,W)的角落。我必须返回一个数组,数字位于从一个角落到另一个角落的路上。当达到目的时,转弯总是发生。 例如:3x4矩阵,从角落开始:(1,1),方向:south 结果:[1,2,3,4,8,7,6,5,9,10,11,12]

我制作了这段代码,但我认为有一种方法可以提高效率。 任何人都可以帮我提高代码的效率吗?

谢谢!

public static JSONObject transformJSON(JSONObject input)
{
    JSONObject resultObject = input;
    int rows = resultObject.getInt("rows");
    int columns = resultObject.getInt("columns");
    int startRow = resultObject.getInt("startRow");
    int startColumn = resultObject.getInt("startColumn");
    String direction = resultObject.getString("direction");

    JSONArray resultArray = new JSONArray();

    if(startRow == 1 && startColumn == 1)
    {
        if(direction.equals("EAST"))
        {
            for(int i = 0; i < rows; i++)
            {
                if(i%2 == 0)
                    for(int j = 0; j < columns; j++)
                        resultArray.put(i * columns + (j+1));   
                else
                    for(int j = columns * (i+1); j > columns * i; j--)
                        resultArray.put(j);
            }
        }
        else //SOUTH
        {
            for(int i = 0; i < columns; i++)
            {
                if(i%2 == 0)
                    for(int j = 0; j < rows; j++)
                        resultArray.put(j * columns + (i+1));
                else
                    for(int j = 1 + columns * (rows - 1) + i; j >= i + 1; j = j - columns)
                        resultArray.put(j);

            }
        }
    }
    else if(startRow == rows && startColumn == 1)
    {
        boolean even = false;
        if(direction.equals("EAST"))
        {
            for(int i = rows - 1; i >= 0; i--)
            {
                if(even == false)
                {
                    for(int j = 0; j < columns; j++)
                        resultArray.put(i * columns + (j+1));
                    even = true;
                }
                else
                {
                    for(int j = columns * (i+1); j > columns * i; j--)
                        resultArray.put(j);
                    even = false;
                }
            }
        }
        else //NORTH
        {
            for(int i = 0; i < columns; i++)
            {
                if(even == false)
                {
                    for(int j = 1 + (rows - 1) * columns + i; j >= i + 1; j = j - columns)
                        resultArray.put(j);
                    even = true;
                }
                else
                {
                    for(int j = 0; j < rows; j++)
                        resultArray.put((i+1) + j * columns);
                    even = false;
                }

            }
        }
    }
    else if(startRow == 1 && startColumn == columns)
    {
        if(direction.equals("WEST"))
        {
            for(int i = 0; i < rows; i++)
            {
                if(i%2 == 1)
                    for(int j = 0; j < columns; j++)
                        resultArray.put(i * columns + (j+1));   
                else
                    for(int j = columns * (i+1); j > columns * i; j--)
                        resultArray.put(j);
            }
        }
        else //SOUTH
        {
            for(int i = columns-1; i >= 0; i--)
            {
                if(i%2 == 0)
                    for(int j = 0; j < rows; j++)
                        resultArray.put((i+1) + j * columns);
                else
                    for(int j = 1 + (rows - 1) * columns + i; j >= i + 1; j = j - columns)
                        resultArray.put(j);

            }
        }
    }
    else if(startRow == rows && startColumn == columns)
    {
        boolean even = true;
        if(direction.equals("WEST"))
        {
            for(int i = rows - 1; i >= 0; i--)
            {
                if(even == false)
                {
                    for(int j = 0; j < columns; j++)
                        resultArray.put(i * columns + (j+1));
                    even = true;
                }
                else
                {
                    for(int j = columns * (i+1); j > columns * i; j--)
                        resultArray.put(j);
                    even = false;
                }
            }
        }
        else //NORTH
        {
            for(int i = columns-1; i >= 0; i--)
            {
                if(even == true)
                {
                    for(int j = 1 + (rows - 1) * columns + i; j >= i + 1; j = j - columns)
                        resultArray.put(j);
                    even = false;
                }
                else
                {
                    for(int j = 0; j < rows; j++)
                        resultArray.put((i+1) + j * columns);
                    even = true;
                }

            }
        }
    }


    JSONObject transformedJSON = new JSONObject();
    transformedJSON.put("solution", resultArray);
    return transformedJSON;
}

我的测试用例:

public static void main(String[] args) throws Exception
{
    JSONObject test1 = new JSONObject();
    test1.put("rows", 3);
    test1.put("columns", 4);
    test1.put("startRow", 1);
    test1.put("startColumn", 1);
    test1.put("direction", "EAST");

    JSONObject test2 = new JSONObject();
    test2.put("rows", 3);
    test2.put("columns", 6);
    test2.put("startRow", 1);
    test2.put("startColumn", 1);
    test2.put("direction", "SOUTH");

    JSONObject test3 = new JSONObject();
    test3.put("rows", 5);
    test3.put("columns", 4);
    test3.put("startRow", 5);
    test3.put("startColumn", 1);
    test3.put("direction", "EAST");

    JSONObject test4 = new JSONObject();
    test4.put("rows", 5);
    test4.put("columns", 4);
    test4.put("startRow", 5);
    test4.put("startColumn", 1);
    test4.put("direction", "NORTH");

    JSONObject test5 = new JSONObject();
    test5.put("rows", 4);
    test5.put("columns", 5);
    test5.put("startRow", 1);
    test5.put("startColumn", 5);
    test5.put("direction", "WEST");

    JSONObject test6 = new JSONObject();
    test6.put("rows", 4);
    test6.put("columns", 5);
    test6.put("startRow", 1);
    test6.put("startColumn", 5);
    test6.put("direction", "SOUTH");

    JSONObject test7 = new JSONObject();
    test7.put("rows", 4);
    test7.put("columns", 5);
    test7.put("startRow", 4);
    test7.put("startColumn", 5);
    test7.put("direction", "WEST");

    JSONObject test8 = new JSONObject();
    test8.put("rows", 4);
    test8.put("columns", 5);
    test8.put("startRow", 4);
    test8.put("startColumn", 5);
    test8.put("direction", "NORTH");

    System.out.println(transformJSON(test1).toString());
    System.out.println(transformJSON(test2).toString());
    //System.out.println(transformJSON(test3).toString());
    //System.out.println(transformJSON(test4).toString());
    //System.out.println(transformJSON(test5).toString());
    //System.out.println(transformJSON(test6).toString());
    //System.out.println(transformJSON(test7).toString());
    System.out.println(transformJSON(test8).toString());

}

0 个答案:

没有答案