LeetCode 59 --- Spiral Matrix II

题目链接:
LeetCode 59 — Spiral Matrix II

AC代码:

public class Problem59 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }
    //参考https://discuss.leetcode.com/topic/52420/java-simple-and-clear-easy-understood
    public int[][] generateMatrix(int n) {
            // similar to spiral matrix I,done by myself
        int[][] rs = new int[n][n];
        int top = 0,bottom = n-1,left = 0,right = n-1;
        int num = 1;

        while(left<=right && top <=bottom){
            for(int i=left;i<=right;i++){
                rs[top][i] = num++;
             }
             top++;
             for(int i= top;i<=bottom;i++){
                  rs[i][right] = num++;
              }
              right--;
              for(int i= right;i>=left;i-- ){
                 rs[bottom][i] = num++;
              }
              bottom--;
              for(int i = bottom;i>=top;i--){
                  rs[i][left] = num++;
               }
               left++;
            }
            return rs;
        }

}

你可能感兴趣的:(LeetCode,算法)