开发者

Traverse String Clockwise using 1D Array only

开发者 https://www.devze.com 2023-04-02 09:00 出处:网络
I have implemented a function TraverseStringClockwise which takes a comma separated string of integers, and a width and a height, and returns a new string by walking in a clockwise order using 2D Char

I have implemented a function TraverseStringClockwise which takes a comma separated string of integers, and a width and a height, and returns a new string by walking in a clockwise order using 2D Char Array. I am trying to do the same using 1D array, but I am running into issues.

Example:

Str=”1,2,3,4,5,6,7,8,9,10,11,12”;Width=3;Height=4;Returnstr=”1,2,3,6,9,12,11,10,7,4,5,8”

Any pointers / help ?

here's the code

public class TraverseStringClockwise {

// Build 2-dimensional matrix representing h * w
public static String[][] buildMatrix(String[] s, int width, int height)
{
    String[][] matrix = new String[height][width];
    int charPos = 0;
    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            matrix[i][j] = s[charPos++];
        }
    }
    return matrix;
}

public static String traverseStringClockwise(String s, int width, int height)
{
    // invalid if width or height are zero or there aren't enough elems in String to fill the matrix
    if(s == null || width == 0 || height == 0 || (s.split(",").length != width * height) )
    {
        return null;
    }
    String[][] matrix = buildMatr开发者_高级运维ix(s.split(","), width, height); // O(n) where n = w*h
    Cursor cursor = new Cursor(width, height);
    StringBuilder sb = new StringBuilder();
    while(!cursor.isWalkComplete()) // // O(n) where n = w*h
    {
        cursor.walk();
        sb.append(matrix[cursor.colPos][cursor.rowPos]);
        if(!cursor.isWalkComplete())
        {
            sb.append(",");
        }
    }
    return (sb.length() > 1) ? sb.toString() : null;
}


/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    String input = "1,2,3,4,5,6,7,8,9,10,11,12";
    int width = 3, height = 4;
    String[][] block = buildMatrix(input.split(","), 3, 4);
    System.out.println("INPUT = " + input);
    System.out.println("OUTPUT = " + walkStringClockwise(input, width, height));
}

}


If you already have the code to walk the array in clockwise order in a 2D array, you can use a standard trick to convert this code to using just a 1D array by linearizing the 2D array into a 1D array. One way to do this is to store the 2D array as a 1D array in row-major order. For example, given this 2D array:

 1  2  3
 4  5  6
 7  8  9
10 11 12

You would encode it as the 1D array

 1 2 3 4 5 6 7 8 9 10 11 12

That is, you lay out all of the elements of the first row, then all the elements of the second row, then all the elements of the third row, etc.

The advantage of this approach is that if you are given an index (row, col) into the original 2D array, you can find the matching position in the 1D array efficiently. To see how to do this, note that each horizontal step you take in the original array corresponds to a horizontal step in the 1D array, while each vertical step you take in the originl array corresponds to skipping over one row of elements in the 1D array. Overall, th formula is that the element at (row, col) in the 2D array can be found at position row * width + col in the 1D array.

Given this, you can try rewriting your code to just use a 1D array, replacing all instances where you were using the 2D array with corresponding code to access the proper element in the 1D array as described above.

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消