diff options
author | Jack Sangdahl <[email protected]> | 2024-08-07 20:27:59 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-08-07 20:27:59 -0600 |
commit | e5e4de987494fdb9a5fd62aa9ee39d6d6d16b6fa (patch) | |
tree | 4e64af50fdd17b9ceffef4211a5463a9b1213482 | |
parent | e8d76d8e669d833541f1b6a2923fb80fca2e8407 (diff) | |
download | leetcode-e5e4de987494fdb9a5fd62aa9ee39d6d6d16b6fa.tar.gz leetcode-e5e4de987494fdb9a5fd62aa9ee39d6d6d16b6fa.zip |
885. spiral matrix 3
-rw-r--r-- | spiral_matrix3.c | 66 | ||||
-rw-r--r-- | spiral_matrix3.py | 38 |
2 files changed, 104 insertions, 0 deletions
diff --git a/spiral_matrix3.c b/spiral_matrix3.c new file mode 100644 index 0000000..b0e1a08 --- /dev/null +++ b/spiral_matrix3.c @@ -0,0 +1,66 @@ +// 885. Spiral Matrix III +#include "leetcode.h" + +/* + * you start at the cell '(rStart, cStart)' of an 'rows * cols' grid facing + * east. the northwest corner is at the first row and column in the grid, and + * the southeast corner is at the last row and column. you will walk in a + * clockwise spiral shape to visit every position in this gid. whenever you move + * outside the grid's boundary, we continue our walk outside the grid (but may + * return to the gid boundary later). eventually we reach all 'rows * cols' + * spaces of the grid. return an array of coordinates representing the positions + * of the grid in the order visited. + */ + +int **spiralMatrixIII(int rows, int cols, int rStart, int cStart, + int *returnSize, int **returnColumnSizes) { + *returnSize = rows * cols; + *returnColumnSizes = (int *)malloc((*returnSize) * sizeof(int)); + int **ans = (int **)malloc((*returnSize) * sizeof(int *)); + for (int i = 0; i < (*returnSize); i++) { + ans[i] = (int *)calloc(2, sizeof(int)); + (*returnColumnSizes)[i] = 2; + } + ans[0][0] = rStart; + ans[0][1] = cStart; + int up = 1, right = 1, cnt_rl = 0, cnt_ud = 0, idx = 1; + while (idx < (*returnSize)) { + for (int shift = 1; shift <= right; shift++) { + cStart = !(cnt_rl % 2) ? cStart + 1 : cStart - 1; + if (rStart < rows && cStart < cols && rStart > -1 && cStart > -1) { + ans[idx][0] = rStart; + ans[idx][1] = cStart; + idx++; + } + } + cnt_rl++, right++; + for (int shift = 1; shift <= up; shift++) { + rStart = !(cnt_ud % 2) ? rStart + 1 : rStart - 1; + if (rStart < rows && cStart < cols && rStart > -1 && cStart > -1) { + ans[idx][0] = rStart; + ans[idx][1] = cStart; + } + } + cnt_ud++, up++; + } + return ans; +} + +int main() { + int rs1, rs2, **rcs1, **rcs2; + int **sm31 = spiralMatrixIII(1, 4, 0, 0, &rs1, rcs1); + int **sm32 = spiralMatrixIII(5, 6, 1, 4, &rs1, rcs2); + for (int i = 0; i < rs1; i++) + for (int j = 0; j < rcs1[i][j]; j++) + printf("%d ", sm31[i][j]); + printf("\n"); + for (int i = 0; i < rs2; i++) + for (int j = 0; j < rcs2[i][j]; j++) + printf("%d ", sm32[i][j]); + printf("\n"); + for (int i = 0; i < rs1; i++) + free(sm31[i]); + for (int i = 0; i < rs2; i++) + free(sm32[i]); + free(sm31), free(sm32); +} diff --git a/spiral_matrix3.py b/spiral_matrix3.py new file mode 100644 index 0000000..1f0af25 --- /dev/null +++ b/spiral_matrix3.py @@ -0,0 +1,38 @@ +# 885. Spiral Matrix III + +""" +you start at the cell '(rStart, cStart)' of an 'rows cols' grid facing +east. the northwest corner is at the first row and column in the grid, and +the southeast corner is at the last row and column. you will walk in a +clockwise spiral shape to visit every position in this gid. whenever you move +outside the grid's boundary, we continue our walk outside the grid (but may +return to the gid boundary later). eventually we reach all 'rows cols' +spaces of the grid. return an array of coordinates representing the positions +of the grid in the order visited. +""" + + +class Solution(object): + def spiralMatrixIII(self, rows, cols, rStart, cStart): + """ + :type rows: int + :type cols: int + :type rStart: int + :type cStart: int + :rtype: List[List[int]] + """ + ans = [] + dx, dy, n = 0, 1, 0 + while len(ans) < rows * cols: + for i in range(n / 2 + 1): + if 0 <= x < rows and 0 <= y < cols: + ans.append([x, y]) + x, y = x + dx, y + dy + dx, dy, n = dy, -dx, n + 1 + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.spiralMatrixIII(rows=1, cols=4, rStart=0, cStart=0)) + print(obj.spiralMatrixIII(rows=5, cols=6, rStart=1, cStart=4)) |