【LeetCode-面试算法经典-Java实现】【006-ZigZag Conversion(Z字型转换)】
来源:互联网 发布:surfacepro4 续航优化 编辑:程序博客网 时间:2024/05/21 11:22
【006-ZigZag Conversion(Z字型转换)】
【LeetCode-面试算法经典-Java实现】【所有题目目录索引】
原题
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
APLSIIG
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”.
题目大意
输入一个字符串和指定的行数,将字符以Z字型输出。
解题思路
计算出字符的最大列数,根据列数和行数创建一个一维数组,再计算每个字符中一维数组中的位置,再对一维数组中的字符进行紧凑操作,返回结果。
代码实现
public class Solution { public String convert(String s, int nRows) { if (s == null || s.length() <= nRows || nRows == 1) { return s; } int index = s.length(); int rowLength = 0; // 计算行的长度,包括最后换行字符 int slash = nRows - 2; // 一个斜线除去首尾所占用的行数 while (index > 0) { // 竖形的一列 index -= nRows; rowLength++; // 斜着的列数 for (int i = 0; i < slash && index > 0; i++) { rowLength++; index--; } } char[] result = new char[nRows * rowLength]; // 保存结果的数组,最后一列用于保存换行符 for (int i = 0; i < result.length; i++) { // 初始化为空格 result[i] = ' '; } int curColumn = 0; // 当前处理的行数 index = 0; while (index < s.length()) { // 处理竖线 for (int i = 0; i < nRows && index < s.length(); i++) { result[rowLength * i + curColumn] = s.charAt(index); index++; } curColumn++; // 处理斜线 for (int i = nRows - 2; i > 0 && index < s.length(); i--) { result[rowLength * i + curColumn] = s.charAt(index); curColumn++; index++; } }// System.out.println(new String(result)); // 对字符数组进行紧凑操作 index = 0; while (index < s.length() && result[index] != ' ') { // 找第一个是空格的字符位置 index++; } int next = index + 1; while (index < s.length()) { while (next < result.length && result[next] == ' ') { // 找不是空格的元素 next++; } result[index] = result[next]; index++; next++; } System.out.println(s); System.out.println(new String(result, 0, index)); return new String(result, 0, index); }}
评测结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。
特别说明
欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/46938327】
2 0
- 【LeetCode-面试算法经典-Java实现】【006-ZigZag Conversion(Z字型转换)】
- 【LeetCode-面试算法经典-Java实现】【103-Binary Tree Zigzag Level Order Traversal(二叉树分层Z字形遍历)】
- LeetCode 6 ZigZag Conversion(Z型转换)(String)
- LeetCode OJ 之 ZigZag Conversion (“Z”转换)
- ZigZag Conversion(“Z”形转换)
- LeetCode-6. ZigZag Conversion (JAVA)(之字形转换)
- LeetCode | ZigZag Conversion(锯齿形转换)
- java 实现Leetcode ZigZag Conversion问题
- [Leetcode] ZigZag Conversion (Java)
- [LeetCode][Java] ZigZag Conversion
- 【leetcode】ZigZag Conversion【java】
- LeetCode : ZigZag Conversion [java]
- leetcode ZigZag Conversion(Java)
- 【LeetCode算法练习(C++)】 ZigZag Conversion
- ZigZag Conversion [Z 型字母转换]
- (LeetCode)ZigZag Conversion --- Z型字符串
- leetcode-6. ZigZag Conversion(Z型字符串)
- leetcode-006:ZigZag Conversion
- 高性能RPC over MINA&google protobuf 代码&实例 (一)
- 高性能RPC over MINA&google protobuf 代码&实例 (二)
- 在ASP.Net和IIS中删除不必要的HTTP响应头
- IIS优化网站性能(转)
- Apache Spark in Mac OS Yosemite 安装教程
- 【LeetCode-面试算法经典-Java实现】【006-ZigZag Conversion(Z字型转换)】
- “PEP小学英语”隐私政策
- 学习笔记:The Log(我所读过的最好的一篇分布式技术文章
- 【LeetCode-面试算法经典-Java实现】【007-Reverse Integer(翻转整数)】
- 【LeetCode-面试算法经典-Java实现】【008-String to Integer (atoi) (字符串转成整数)】
- jQuery Mobile教程:jQuery Mobile基本事件
- ACM暑期计划 —— 第三天
- Jquery mobile 新手问题总汇
- 黑马程序员——基础学习感悟总结...