详细见:leetcode.com/problems/spiral-matrix
Java Solution: github
package leetcode;
import java.util.LinkedList;
import java.util.List;
public class P054_SpiralMatrix {
public static void main(String[] args) {
System.out.println(new Solution().spiralOrder(new int[][]{
{1},
{1},
{1},
{1}
}));
}
/*
* 最重要的是代码简洁,否则就是没用
* 1 ms
* 2.73%
*/
static class Solution {
List<Integer> ans = new LinkedList<Integer>();
public List<Integer> spiralOrder(int[][] matrix) {
int row = 0, col = 0, eni = 0;
if (matrix == null || (row = matrix.length) == 0
|| (col = matrix[0].length) == 0)
return ans;
eni = Math.min(row, col) >> 1;
for (int i = 0; i != eni; i ++)
fourEdge(i, row - 1 - i, col - 1 - i, matrix);
if ((Math.min(row, col) & 0x1) == 1) {
int rowi = Math.min(row, col) >> 1;
int staj = rowi;
int endj = Math.max(row, col) - rowi - 1;
if (row < col)
for (int j = staj; j <= endj; j ++)
ans.add(matrix[rowi][j]);
else
for (int i = staj; i <= endj; i ++)
ans.add(matrix[i][rowi]);
}
return ans;
}
private void fourEdge(int i, int j, int k, int[][] matrix) {
int t = 0;
for (t = i; t < k; t ++)
ans.add(matrix[i][t]);
for (t = i; t < j; t ++)
ans.add(matrix[t][k]);
for (t = k; t > i; t --)
ans.add(matrix[j][t]);
for (t = j; t > i; t --)
ans.add(matrix[t][i]);
}
}
}
C Solution: github
/*
url: leetcode.com/problems/spiral-matrix/
AC 3ms 0.00%
*/
#include <stdio.h>
#include <stdlib.h>
void shell_add(int* ans, int* ai, int** m, int x1, int y1, int x2, int y2) {
int x = 0, y = 0;
for (y = y1; y < y2; y ++)
ans[(*ai) ++] = m[x1][y];
for (x = x1; x < x2; x ++)
ans[(*ai) ++] = m[x][y2];
for (y = y2; y > y1; y --)
ans[(*ai) ++] = m[x2][y];
for (x = x2; x > x1; x --)
ans[(*ai) ++] = m[x][y1];
}
int* spiralOrder(int** m, int rn, int cn) {
int* ans = (int*) malloc(sizeof(int)*(rn*cn));
int ai = 0;
int x1 = 0, y1 = 0, x2 = rn - 1, y2 = cn - 1;
while (x1 < x2 && y1 < y2) {
shell_add(ans, &ai, m, x1, y1, x2, y2);
x1 ++;
y1 ++;
x2 --;
y2 --;
}
if (x1 == x2) {
while(y1 <= y2)
ans[ai ++] = m[x1][y1++];
} else if (y1 == y2) {
while (x1 <= x2)
ans[ai ++] = m[x1++][y1];
}
printf("ai is %d\r\n", ai);
return ans;
}
int main() {
int rn = 7, cn = 3;
int** m = (int**) malloc(sizeof(int*)*rn);
int m0[] = {1, 2, 3};
int m1[] = {4, 5, 6};
int m2[] = {7, 8, 9};
int m3[] = {10, 11, 12};
int m4[] = {13, 14, 15};
int m5[] = {16, 17, 18};
int m6[] = {19, 20, 21};
int* ans = NULL;
int i = 0;
m[0] = m0;
m[1] = m1;
m[2] = m2;
m[3] = m3;
m[4] = m4;
m[5] = m5;
m[6] = m6;
ans = spiralOrder(m, rn, cn);
for (i = 0; i < rn*cn; i ++)
printf("%d ", ans[i]);
printf("\r\n");
free(m);
}
Python Solution: github
#coding=utf-8
'''
url: leetcode.com/problems/spiral-matrix
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年4月10日
@details: Solution: 45ms 46.70%
'''
class Solution(object):
def shell_add(self, a, ai, i1, j1, i2, j2, m):
for k in range(j1, j2):
a[ai[0]] = m[i1][k]
ai[0] += 1
for k in range(i1 , i2):
a[ai[0]] = m[k][j2]
ai[0] += 1
for k in range(j2, j1, -1):
a[ai[0]] = m[i2][k]
ai[0] += 1
for k in range(i2, i1, -1):
a[ai[0]] = m[k][j1]
ai[0] += 1
def spiralOrder(self, m):
"""
:type m: List[List[int]]
:rtype: List[int]
"""
mi = 0 if m == None else len(m)
if mi == 0: return []
mj = 0 if m[0] == None else len(m[0])
if mj == 0: return []
i1, j1, i2, j2, ai, a = 0,0,mi-1,mj-1,[0],[0]*(mi*mj)
while i1 < i2 and j1 < j2:
self.shell_add(a, ai, i1, j1, i2, j2, m)
i1, i2, j1, j2 = i1+1,i2-1,j1+1,j2-1
if i1 == i2:
for k in range(j1, j2+1):
a[ai[0]] = m[i1][k]
ai[0] += 1
elif j1 == j2:
for k in range(i1, i2+1):
a[ai[0]] = m[k][j1]
ai[0] += 1
return a