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 ...
随机推荐
- 软件缺陷5C标准
Correct(准确) :每个组成部分的描述准确,不会引起误解 Clear(清晰): 每个组成部分描述清晰,易于理解 Concise(简洁): 只包含必不可少的信息,不包括任何多余的内容 Comple ...
- ASP.NET Core 入门笔记3,使用ASP.NET Core MVC框架构建Web应用
一.ASP.NET Core MVC 输出Hello World,Friend! 1.引入 ASP.NET Core MVC 修改应用启动类(Startup.cs),引入MVC模块并配置默认路由 pu ...
- docker 启动镜像报 WARNING: IPv4 forwarding is disabled. Networking will not work.
centos7 解决办法: # vi /etc/sysctl.conf 添加如下代码: net.ipv4.ip_forward=1 重启network服务 # systemctl restar ...
- 奥比中光Astra Pro的使用(1)
在ubuntu上的使用 首先下载SDK以及OpenNI安装包,下载地址: 解压两个安装包 切换目录到AstraSDK-Linux下的install目录,并输入命令:sudo sh ./install. ...
- MySQL知识篇-nmon监控
说明1:监控MySQL服务器资源不止一种方式,这种nmon监控图形化.历史记录查询笔记方便,便于MySQL优化后,对比其效率不同,资源利用率不同. 说明2:摘抄自https://www.cnblogs ...
- SQL ------ JDBC 删除指定的某条记录
package demo; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; ...
- Linux系列(11):之bash进阶与数据流导向、管线命令
1.万用字符与特殊字符 在bash的环境下有一个特别有用的功能,那就是万用字符!下面展示常见的万用字符: 1.万用字符 除了万用字符还有一些其他的特殊字符,接下来显示特殊字符. 2.特殊字符 2.数据 ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- centos编译安装python3怎么做?
照着我的博客操作 你一定能成功的!因为我就是一步一步的做出来的,虽然只有文档,但是希望你能有耐心!!!! 编译安装难么麻烦,为什么还要编译安装? 那我告诉你想进步就要折腾!你习惯了windows的安装 ...
- 利用requests提交相同名称数据的处理方法
#字典键不能重复 data={ boardid' : boardid, 'divids[ ]' : '0' , 'divids[ ]' : '1' , 'divids[ ]' : '2' , } #这 ...