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 };
时间: 2024-11-20 22:36:04

Spiral and Zigzag的相关文章

【数组】Spiral Matrix II

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 思路: 本质上和上一题是一样的,这里我们要用数字螺旋的去填充矩阵.同理,我们也是逐个环

Spiral Matrix(LintCode)

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 难得的一次AC! 虽然感觉题

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

LeetCode—*Spiral Matrix问题,主要是用到了方向矩阵,很创意

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 这是一个螺旋排序的问题 这里遇到一个比较巧妙的

【Leet Code】ZigZag Conversion

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) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSII

Binary Tree Zigzag Level Order Traversal

原题: 题目解析:这个问题的实质是要我们按成访问二叉树的结点,并返回每层访问的结果,这里要求走Z字,其实就是一行正向一行反向. /* the kernel idea is visit a binary search tree in level and the additional work we have to label the end of one level. */ vector<vector<int> > zigzagLevelOrder(TreeNode *root) {

[Leetcode] spiral matrix 螺旋矩阵

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return[1,2,3,6,9,8,7,4,5]. 题意:以螺旋的方式,顺时针访问数组. 思路:按照遍

103. Binary Tree Zigzag Level Order Traversal (Tree, Queue; BFS)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree [3,9,20,null,null,15,7], 3   / \  9  20    /  \