对这个矩阵挑战有任何想法或解决方案吗? [英] Any Idea or solution to this matrix challenge?

查看:47
本文介绍了对这个矩阵挑战有任何想法或解决方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,正在练习算法,我只是想知道如何解决这个螺旋矩阵挑战:

Hi I'm newbie practicing algorithms, I was just wondering how to solve this spiral matrix Challenge:

让函数MatrixSpiral(strArr)读取存储在strArr中的字符串数组,该字符串表示2D N矩阵,并且您的程序应在按顺时针螺旋顺序打印元素后返回它们.您应该以字符串形式返回新形成的元素列表,其字符串之间用逗号分隔.例如:输入:

Have the function MatrixSpiral(strArr) read the array of strings stored in strArr which will represent a 2D N matrix, and your program should return the elements after printing them in a clockwise, spiral order. You should return the newly formed list of elements as a string with the numbers separated by commas. For example: input:

["[4, 5, 6, 5]",   
 "[1, 1, 2, 2]",  
 "[5, 4, 2, 9]"]   

输出:

"4,5,6,5,2,9,2,4,5,1,1,2"

我以前做过简单的矩阵螺旋运算,但是不知道如何解决这样的问题.

I have done simple matrix spiral before, but don't know how to solve one like this.

这不是一个简单的矩阵螺旋.我尝试使用此代码,但输出方式却不同

This is not a simple matrix spiral. I tried with this code, but output is way different

输入是字符串数组"的数组(请参见双引号),输出应该是数字用逗号分隔的字符串.

const spiralOrder = (matrix) => {

if(!matrix.length || !matrix[0].length){
        return [];
}
//Use 4 pointes to create wall around square
let rowBegin = 0,
    rowEnd = matrix.length - 1,
    colBegin = 0,
    colEnd = matrix[0].length - 1;

let result = [];
while(rowBegin <= rowEnd && colBegin <= colEnd){

    //move right
    for(let i= colBegin; i<= colEnd; i++){
            result.push(matrix[rowBegin][i]);
    }
    rowBegin++; // mark row as traversed after moving right

    //move down
    for(let i=rowBegin; i<= rowEnd; i++){
            result.push(matrix[i][colEnd]);
    }
    colEnd--; //mark column as traversed after moving down

    //move left
    if(rowBegin <= rowEnd){
            for(let i=colEnd; i >= colBegin; i--){
                    result.push(matrix[rowEnd][i]); 
            }
    }
    rowEnd--; //mark end row as traversed after moving left

    //move up
    if(colBegin <= colEnd){ 
            for(let i=rowEnd; i >= rowBegin; i--){
                    result.push(matrix[i][colBegin]);
            }
    }
    colBegin++; //mark begining column as traversed after moving up
}

return result;
};

spiralOrder([[4, 5, 6, 5], [1, 1, 2, 2], [5, 4, 2, 9]])

Output: [ '[',
  '4',
  ',',
  ' ',
  '5',
  ',',
  ' ',
  '6',
  ',',
  ' ',
  '5',
  ']',
  ']',
  ']',
  '9',
  ' ',
  ',',
  '2',
  ' ',
  ',',
  '4',
  ' ',
  ',',
  '5',
  '[',
  '[',
  '1',
  ',',
  ' ',
  '1',
  ',',
  ' ',
  '2',
  ',',
  ' ',
  '2' ]

可以请您分享任何解决方案吗?

Could you please share any solution?

推荐答案

您可以采用包含四个方向和一个索引对(i/j)的方法,以及四个用于限制循环的变量upperlower限制,以及leftright限制.

You could take an approach with four directions and an index pair (i/j), as well as four more variables for limiting the loops with upper and lower bound, as well as left and right limits.

获取限制后,将检查限制是递增还是递减.如果该限制不在所需范围之内,则循环结束.

After taking a limit, the limit is checked an incremented or decremented. If the limit is not inside of the wanted range, the loops ends.

最后,将剩下的项目添加到结果集中.

At the end, the left over item is added to the result set.

function getSpiral(data) {
    var array = data.map(j => JSON.parse(j)),
        upper = 0,
        lower = array.length - 1,
        left = 0,
        right = array[0].length - 1,
        i = upper,
        j = left,
        result = [];

    while (true) {
        if (upper++ > lower) break;

        for (; j < right; j++) result.push(array[i][j]);
        if (right-- < left) break;

        for (; i < lower; i++) result.push(array[i][j]);
        if (lower-- < upper) break;

        for (; j > left; j--) result.push(array[i][j]);
        if (left++ > right) break;

        for (; i > upper; i--) result.push(array[i][j]);
    }
    result.push(array[i][j]);

    return result.join(',');
}

console.log(getSpiral(['[4, 5, 6, 5]', '[1, 1, 2, 2]', '[5, 4, 2, 9]']));
console.log(getSpiral(['[1, 2, 3, 4, 5]', '[6, 7, 8, 9, 10]', '[11, 12, 13, 14, 15]', '[16, 17, 18, 19, 20]']));

这篇关于对这个矩阵挑战有任何想法或解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆