【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的更多相关文章

  1. 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 ...

  2. [LeetCode] Zigzag Iterator 之字形迭代器

    Given two 1d vectors, implement an iterator to return their elements alternately. For example, given ...

  3. [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 ...

  4. [LeetCode] Spiral Matrix II 螺旋矩阵之二

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  5. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  6. [LeetCode] ZigZag Converesion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  7. 【leetcode】ZigZag Conversion

    题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  8. 整数压缩编码 ZigZag

    在分析Avro源码时,发现Avro为了对int.long类型数据压缩,采用Protocol Buffers的ZigZag编码(Thrift也采用了ZigZag来压缩整数). 1. 补码编码 为了便于后 ...

  9. No.006:ZigZag Conversion

    问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

随机推荐

  1. docker笔记(2)——docker镜像操作

    操作环境:mac OS 10.14.6 docker版本:10.03.1 终端:iterm2 3.3 时间:2019年8月 docker 镜像,是运行容器的模板,通过pull操作会向指定仓库获取镜像, ...

  2. itchat相关资料

    https://itchat.readthedocs.io/zh/latest/ https://www.v2ex.com/t/306804 http://blog.csdn.net/th_num/a ...

  3. win10下查看进程,杀死进程

    参考链接:https://blog.csdn.net/qq_36819098/article/details/80262482

  4. [bzoj4026]dC Loves Number Theory_主席树_质因数分解_欧拉函数

    dC Loves Number Theory 题目大意:dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯竭的水题资源. 给定一个长度为 n的正整数序列A,有q次询问 ...

  5. 微信小程序开放数据解密 AES-128-CBC 解密(C#版本)

    最近在开发小程序,需要跟微信服务端交互,微信敏感数据都有加密返回,需要在服务端接收进行解密后再返回给客户端小程序,今天就通过C# 进行数据的解密,官方下载下来是Node.C++.php等,就是没有C# ...

  6. Linux-echo:打印彩色输出

    脚本可以使用转义序列在终端中生成彩色文本 文本颜色是由对应的色彩码来描述的.其中包括: 重置=0,黑色=30,红色=31,绿色=32, 黄色=33,蓝色=34,洋红=35,青色=36,白色=37. 要 ...

  7. 小白学习django第一站-环境配置

    Django简单来说就是用Python开发的一个免费开源的Web框架 使用Django,使你能够以最小的代价构建和维护高质量的Web应用. 开搞!!! 工具准备: linux(ubuntu) + py ...

  8. C++字符转换等常用方法

    下面是C++中字符编码格式转换(包括ANSI转换为UTF8,UTF8转换为ANSI,ANSI转换为unicode,unicode转换为ANSI,UTF8转换为unicode,unicode转换为UTF ...

  9. 《深入理解 Java 虚拟机》学习笔记 -- 内存区域

    <深入理解 Java 虚拟机>学习笔记 -- 内存区域 运行时数据区域 主要分为 6 部分: 程序计数器 虚拟机栈 本地方法栈 Java 堆 方法区 如图所示: 1. 程序计数器(线程私有 ...

  10. Spring 的 AOP 概述和底层实现

    Spring 的 AOP 概述和底层实现 1. 什么是 AOP AOP (Aspect Oriented Programing),即面向切面编程 AOP 采取横向抽取机制,取代了传统纵向继承体系重复性 ...