Spiral and Zigzag
【LeetCode】
虽然感觉spiral matrix 两道题和 zigzag conversion 那道题没有太多联系,但是,毕竟都是相当于数学上的找规律题目。
这种优雅的题目就应该用下面这种优雅的代码写法。
054 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].
*/ class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<>();
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return res;
int beginX = 0, endX = matrix[0].length - 1;
int beginY = 0, endY = matrix.length - 1;
while(true){
//from left to right
for(int i = beginX; i <= endX; i++) res.add(matrix[beginY][i]);
if(++beginY > endY) break;
//from top to bottom
for(int i = beginY; i <= endY; i++) res.add(matrix[i][endX]);
if(beginX > --endX) break;
//from right to left
for(int i = endX; i >= beginX; i--) res.add(matrix[endY][i]);
if(beginY > --endY) break;
//from bottom to top
for(int i = endY; i >= beginY; i--) res.add(matrix[i][beginX]);
if(++beginX > endX) break;
}
return res;
}
}
059 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 ]
] */ class Solution {
public int[][] generateMatrix(int n) {
int[][] res = new int[n][n];
int beginX = 0, endX = n - 1;
int beginY = 0, endY = n - 1;
int flag = 1;
while(true){
for(int i = beginX; i <= endX; i++) res[beginY][i] = flag++;
if(++beginY > endY) break;
for(int i = beginY; i <= endY; i++) res[i][endX] = flag++;
if(beginX > --endX) break;
for(int i = endX; i >= beginX; i--) res[endY][i] = flag++;
if(beginY > --endY) break;
for(int i = endY; i >= beginY; i--) res[i][beginX] = flag++;
if(++beginX > endX) break;
}
return res;
}
}
006 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: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows: string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
*/ class Solution {
public:
string convert(string s, int numRows) {
if(numRows <= || s.length() < numRows)
return s;
string res;
for(int i = ;i < numRows;++i)
{
for(int j = i;j < s.length();j += * (numRows - ))
{
res += s[j];
if(i > && i < numRows - )
{
if(j + * (numRows - - i) < s.length())
res += s[j + * (numRows - - i)];
}
}
}
return res;
}
};
Spiral and Zigzag的更多相关文章
- Pramp mock interview (4th practice): Matrix Spiral Print
March 16, 2016 Problem statement:Given a 2D array (matrix) named M, print all items of M in a spiral ...
- [LeetCode] Zigzag Iterator 之字形迭代器
Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...
- [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- [LeetCode] ZigZag Converesion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【leetcode】ZigZag Conversion
题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- 整数压缩编码 ZigZag
在分析Avro源码时,发现Avro为了对int.long类型数据压缩,采用Protocol Buffers的ZigZag编码(Thrift也采用了ZigZag来压缩整数). 1. 补码编码 为了便于后 ...
- No.006:ZigZag Conversion
问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
随机推荐
- pandas中groupby的参数:as_index
参考:https://blog.csdn.net/cjsyr6wt/article/details/78200444?locationNum=11&fps=1 以下是pandas官方的解释: ...
- 【FFMPEG】使用FFMPEG+H264实现RTP传输数据
开发环境:WINDOWS7 32bitMINGWeclipse juno cdt1.首先你要编译好FFMPEG,a) 方法一:可以去官网下载源码,用MINGW编译(编译时记得支持H264,当然,事先得 ...
- Angular ViewChild & ViewChildren
基础 ViewChild ViewChild 装饰器用于获取模板视图中的元素或直接调用其组件中的方法.它支持 Type 类型或 string 类型的选择器,同时支持设置 read 查询条件,以获取不同 ...
- python手撸桌面计算器
网上有一些许多关于计算器的源码,但我似乎不太care 一直寻思着自己手撸一个才有意思,于是这就开始了 实现功能: 1.基本的两个数 +-x÷ 运算以及取反,百分之,平方等 2.支持连续运算 3.暂不支 ...
- C语言Ⅰ博客作业07
这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/9933 我在这个课程的目 ...
- Linux 防火墙设置常用指令
查看防火墙状态命令: service firewalld status systemctl status firewalld 结果: 其中: enabled:开机启动(开机不启动是disabled ...
- Hadoop_常用命令(hdfs上)
Hadoop_常用命令(hdfs上) hadoop fs所有文件系统都可以使用 hdfs dfs仅针对于hdfs文件系统 - 1 - 查看所有目录(文件夹)及文件 hdfs dfs -ls / - ...
- 并不对劲的复健训练-p3674
题目大意 给出序列$ a_1,...,a_n $ ( $ n\leq10^5,a\leq 10^5 $ ),有\(m\) ( \(m\leq 10^5\))个以下三类询问: (1)给出\(l,r,k\ ...
- Spring实战(十)Spring AOP应用——为方法引入新功能、为对象引入新方法
切面最基本的元素是通知和切点,切点用于准确定位应该在什么地方应用切面的通知. 1.Spring借助AspectJ的切点表达式语言来定义Spring切面 在Spring中,要使用AspectJ的切点表达 ...
- C#委托的定义 以及使用方式详解,更简单的理解委托。
委托的声明及定义: 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法,可以避免在程序中大量使用If-Else(Switch)语句,同时使得 ...