[LeetCode] 6. 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".
解法:
n = 2 时,字符串坐标变成zigzag的走法就是:
2 4 6 3 5 7
n = 3 时,字符串坐标变成zigzag的走法就是:
4 8
5 7 9
6 10
n = 5 时,字符串坐标变成zigzag的走法就是:
8 16
9 15 17
10 14 18
11 13 19
12 20
可以发现,画红色的(一个循环)长度永远是 2n-2。
利用这个规律,可以按行填字,第一行和最后一行,就是按照2n-2的顺序一点点加的。
其他行除了上面那个填字规则,就是还要处理斜着那条线的字,可以发现那条线的字的位置永远是 j+(2n-2)-2i(i 是当前行的索引,j 是当前行的当前循环的起始值,如 i = 1 时,j 依次等于 1, 9, 17....)。
public class Solution {
public String convert(String s, int numRows) {
if ((s == null) || (s.length() == 0) || (numRows <= 0))
return "";
if (numRows == 1)
return s;
StringBuilder res = new StringBuilder("");
int size = 2 * numRows - 2;
for (int i = 0; i < numRows; i++) {
for (int j = i; j < s.length(); j += size) {
res.append(s.charAt(j));
if ((i != 0) && (i != numRows - 1)) {
int temp = j + size - 2 * i;
if (temp < s.length()) {
res.append(s.charAt(temp));
}
}
}
}
return res.toString();
}
}
[LeetCode] 6. ZigZag Conversion ☆☆☆的更多相关文章
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
- Leetcode 6. ZigZag Conversion(找规律,水题)
6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...
- LeetCode 6 ZigZag Conversion 模拟 难度:0
https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...
- LeetCode 6 ZigZag Conversion(规律)
题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- [LeetCode 题解]: ZigZag Conversion
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 The string ...
- [LeetCode] 6. ZigZag Conversion 之字型转换字符串
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 ...
- leetcode 6. ZigZag Conversion
https://leetcode.com/problems/zigzag-conversion/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" ...
随机推荐
- cenos环境变量配置
Beego环境搭建和bee工具安装使用,以Windows环境为例. 首先,下载并安装好GO并配置好GOROOT和GOPATH环境变量(如果您是用msi包安装的go,那么这些环境变量已经设置好了).并在 ...
- 使用libpcab抓包&处理包
#include <stdio.h> #include <stdlib.h> #include <strings.h> #include <string.h& ...
- 团队协作第八周个人PSP
11.3 --11.9本周例行报告 1.PSP(personal software process )个人软件过程. 类型 任务 开始时间 结束时间 中断时间 实际用时 ...
- 20145214 《Java程序设计》第8周学习总结
20145214 <Java程序设计>第8周学习总结 教材学习内容总结 日志API 使用日志的起点是Logger类,Logger类的构造函数标示为protected,不是java.util ...
- Java Class Object
Object类 它是所有类的基类. public class Person { } //实际上是 public class Person extends Object { } Object类的方法 t ...
- lintcode-151-买卖股票的最佳时机 III
151-买卖股票的最佳时机 III 假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格.设计一个算法来找到最大的利润.你最多可以完成两笔交易. 注意事项 你不可以同时参与多笔交易(你必须在 ...
- 透过汇编另眼看世界之DLL导出函数调用
前言:我一直对DLL技术充满好奇,一方面是因为我对DLL的导入/导出机制还不是特别的了解,另一面是因为我发现:DLL技术在Windows平台下占有重要的地位,几乎所有的Win32 API都是以导出函数 ...
- 错误 10 非静态的字段、方法或属性“Test10.Program.a”要求对象引用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test ...
- DELPHI dbgrid 选中的是第几行 怎么判断?
使用DataSource.DataSet.RecNo可以得到dbgrid选中的是第几行,示例代码如下: procedure TForm1.btn1Click(Sender: TObject); beg ...
- RT-thread 设备驱动组件之IIC总线设备
本文主要介绍RT-thread中IIC总线设备驱动,涉及到的主要文件有:驱动框架文件(i2c_core.c,i2c_dev.c,i2c-bit-ops.c,i2c_dev.h,i2c.h):底层硬件驱 ...