Q6:ZigZag Conversion
6. ZigZag Conversion
官方的链接:6. ZigZag Conversion
Description :
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"
.
问题描述
Z型转换输出转换后的字符串
思路
方法一、参考官网
这也是比较容易理解的,建立numRows个字符串,然后遍历原字符串,对数组正数和倒数,逐个把字符按照规则添加到这些字符串数组中,最后合并。
方法二、不建立数组,直接根据规则一行一行拼接字符串。
1、Z型走法,一组总共是n = numRows + numRows - 2,即n个字符
2、从第0行开始计数,记为第row行,第0行和最后一行numRows-1的规则比较好确认:n * j + row即为第row所有的字符,j从0开始,直到n * j + row临界,row行共有j个字符
3、其他行的字符,除了上面的记录字符,在不会越界的情况下,还会多一个连接的字符,这个字符的下标可以这样计算:(j + 1) * n - row,其实(j + 1) * n是下一组的开头,减去当前的行数row,即可得到下一个字符,比如上面的例子,row=1,j=0,下一组的字符是A(第一行的第2个字符)(如下图),计算出来的下标3,即P(第2行第2个字符)(如下图),合并。
public class Q6_ZigZagConversion {
public String convert(String s, int numRows) {
if (numRows == 1) {
return s;
}
int n = numRows + numRows - 2, len = s.length();
StringBuilder result = new StringBuilder();
for (int row = 0; row < numRows; row++) {
int j = 0, headIndex = j * n + row, tailIndex = (j + 1) * n - row;
while (headIndex < len) {
result.append(s.charAt(headIndex));
j++;
headIndex = j * n + row;
if (row != 0 && row != numRows - 1 && tailIndex < len) {
result.append(s.charAt(tailIndex));
tailIndex = (j + 1) * n - row;
}
}
}
return result.toString();
} public static void main(String[] args) {
Q6_ZigZagConversion s = new Q6_ZigZagConversion();
System.out.println(s.convert("PAYPALISHIRING", 3));
}
}
Q6:ZigZag Conversion的更多相关文章
- No.006:ZigZag Conversion
问题: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- leetcode:ZigZag Conversion 曲线转换
Question: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...
- LeetCode之“字符串”:ZigZag Conversion
题目链接 题目要求: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...
- LeetCode OJ:ZigZag Conversion(字符串的Z字型转换)
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 6. ZigZag Conversion
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- 64. ZigZag Conversion
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- LeetCode 6 ZigZag Conversion(规律)
题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...
- No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- leetcode第六题 ZigZag Conversion (java)
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
随机推荐
- PHPXhprof扩展在windows安装
1.下载在这里 http://dev.freshsite.pl/php-extensions/xhprof.html.(找不到资源可以私我我给你,这个上传不了资源) 注意:一定要找对应的php版本,t ...
- kubernetes 1.17.2 结合 Ceph 13.2.8 实现 静态 动态存储 并附带一个实验
关于部署和相关原理 请自行搜索 这里 给出我的操作记录和排查问题的思路 这一节对后面的学习有巨大的作用!!! [root@bs-k8s-ceph ~]# ceph -s cluster: -1a9a- ...
- Day4 - L - Tram POJ - 1847
Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In e ...
- eshop7-mysql
1. Mysql 安装 执行 yum -y install mysql-server 注意:(1)是否使用sudo 权限执行请根据您具体环境来决定 (2)检查是否已经安装mysql-server rp ...
- 解决datagridview 横向的scrollbar不显示
下午遇到这个问题.看到了网上各种解决办法.都没搞定. 新建了一个datagridview.发现是没问题了.仔细对比了一下它们的属性. 在Columns的属性中,有一项:Frozen, 把这个值改顺默认 ...
- https://blog.csdn.net/yyoinge/article/details/81557604
https://blog.csdn.net/yyoinge/article/details/81557604 http://www.mamicode.com/info-detail-2346464.h ...
- webservice调试(XML参数)
<![CDATA[ <?xml version="1.0" encoding="UTF-8"?><MsgText> <use ...
- 3D打印技术的火爆,真的会让传统模具行业没落吗?
当一种新生事物出现时,人们除了赞美它带来的新畅想外,往往还会对"旧事物"贬低几分--各种淘汰观点总是不绝于耳.但可惜的是,新生事物取代旧事物的事儿并不会必然发生.比如,直到现在广播 ...
- linux下nginx的安装和配置
准备目录 [root@sqh ~]# mkdir /usr/local/nginx [root@sqh ~]# cd /usr/local/nginx 添加一些支持和依赖 1.安装gcc 安装redi ...
- Vulkan 之 Synchronization
1.2之前定的版本采用 vkSemaphore和vkFence来进行同步, VkSemaphore allowed applications to synchronize operations acr ...