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 ...
随机推荐
- 论UT阶段重要性
测试与开发这对立的命运啊 如果是对测试从业者心存鄙视的朋友啊,请关掉此页,带着偏见不好看的~ 人生就像一个旅途,每个人看到风景不一样,世界观.人生观.价值观也就不同.不要试着去改变别人,因为你的观点在 ...
- idea标签页多行显示+设置标签页上限
idea标签页多行显示+设置标签页上限 Setting--Editor--General--Editor Tabs
- 算法巩固的第一天-java冒泡排序算法
自媒体萌新一枚,不对的地方各路大神可以指点指点!个人理解: 冒泡排序算法<插入排序算法<快速排序算法 /** * 冒泡排序算法 * @author sj * */ public class ...
- Git 的配置
一般配置用户层面配置 Git 的配置从上到下分三层 system/global/local,使用三个不同参数进行设置,每个层次的配置存储在不同的位置, 1)./etc/gitconfig 文件:包含了 ...
- Javascript学习笔记四——操作表单
Javascript学习笔记 大多网页比如腾讯,百度云之类的需要登陆,用户输入账号密码就可以登陆,那么浏览器是如何获取用户的输入的呢?今天就记录一下操作表单. 操作表单与操作DOM是差不多的,表单本身 ...
- vue 事件中的 .native
vue组件添加事件@click.native native是什么? .native - 监听组件根元素的原生事件. 主要是给自定义的组件添加原生事件. 官网的解释: 你可能想在某个组件的根元素上监听一 ...
- django进阶版1
目录 字段中choice参数 MTV与MVC模型 AJAX(*********) Ajax普通请求 Ajax传json格式化数据 Ajax传文件 序列化组件 Ajax+sweetalert 字段中ch ...
- T100——取得系统参数值,如关帐日期
CALL cl_get_para(g_enterprise,g_site,'S-MFG-0031') RETURNING l_para_data 用此方法获取关帐日期
- c++11 跨平台多线程demo和qt 静态链接(std::thread有join函数,设置 QMAKE_LFLAGS = -static)
#include <stdio.h>#include <stdlib.h> #include <chrono> // std::chrono::seconds#in ...
- join函数详解
定义:join() 方法用于把数组中的所有元素放入一个字符串. 语法 : ArrayObject.join(separator) separator 可选.指定要使用的分隔符.如果省略该参数,则使 ...