Spiral and Zigzag

【LeetCode】 

虽然感觉spiral matrix 两道题和 zigzag conversion 那道题没有太多联系,但是,毕竟都是相当于数学上的找规律题目。

这种优雅的题目就应该用下面这种优雅的代码写法。

054 Spiral Matrix

 1 /* Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
 2 
 3 For example,
 4 Given the following matrix:
 5 
 6 [
 7  [ 1, 2, 3 ],
 8  [ 4, 5, 6 ],
 9  [ 7, 8, 9 ]
10 ]
11 You should return [1,2,3,6,9,8,7,4,5].
12  */
13  
14  
15 class Solution {
16     public List<Integer> spiralOrder(int[][] matrix) {
17         List<Integer> res = new ArrayList<>();
18         if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return res;
19         int beginX = 0, endX = matrix[0].length - 1;
20         int beginY = 0, endY = matrix.length - 1;
21         while(true){
22             //from left to right
23             for(int i = beginX; i <= endX; i++) res.add(matrix[beginY][i]);
24             if(++beginY > endY) break;
25             //from top to bottom
26             for(int i = beginY; i <= endY; i++) res.add(matrix[i][endX]);
27             if(beginX > --endX) break;
28             //from right to left
29             for(int i = endX; i >= beginX; i--) res.add(matrix[endY][i]);
30             if(beginY > --endY) break;
31             //from bottom to top
32             for(int i = endY; i >= beginY; i--) res.add(matrix[i][beginX]);
33             if(++beginX > endX) break;
34         }
35         return res;
36     }
37 }

059 Spiral Matrix II

 1 /* Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.
 2 
 3 For example,
 4 Given n = 3,
 5 
 6 You should return the following matrix:
 7 [
 8  [ 1, 2, 3 ],
 9  [ 8, 9, 4 ],
10  [ 7, 6, 5 ]
11 ] */
12 
13 
14 class Solution {
15     public int[][] generateMatrix(int n) {
16         int[][] res = new int[n][n];
17         int beginX = 0, endX = n - 1;
18         int beginY = 0, endY = n - 1;
19         int flag = 1;
20         while(true){
21             for(int i = beginX; i <= endX; i++) res[beginY][i] = flag++;
22             if(++beginY > endY) break;
23             for(int i = beginY; i <= endY; i++) res[i][endX] = flag++;
24             if(beginX > --endX) break;
25             for(int i = endX; i >= beginX; i--) res[endY][i] = flag++;
26             if(beginY > --endY) break;
27             for(int i = endY; i >= beginY; i--) res[i][beginX] = flag++;
28             if(++beginX > endX) break;
29         }
30         return res;       
31     }
32 }

006 Zigzag Conversion

 1 /* The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
 2 
 3 P   A   H   N
 4 A P L S I I G
 5 Y   I   R
 6 And then read line by line: "PAHNAPLSIIGYIR"
 7 Write the code that will take a string and make this conversion given a number of rows:
 8 
 9 string convert(string text, int nRows);
10 convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
11  */
12 
13 class Solution {
14 public:
15     string convert(string s, int numRows) {
16         if(numRows <= 1 || s.length() < numRows) 
17             return s;
18         string res;
19         for(int i = 0;i < numRows;++i)
20         {
21             for(int j = i;j < s.length();j += 2 * (numRows - 1))
22             {
23                 res += s[j];
24                 if(i > 0 && i < numRows - 1)
25                 {
26                     if(j + 2 * (numRows - 1 - i) < s.length())
27                         res += s[j + 2 * (numRows - 1 - i)];
28                 }
29             }
30         }
31         return res;
32     }
33 };
原文地址:https://www.cnblogs.com/niuxichuan/p/7471045.html