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<=1||numRows>=s.length())return s; StringBuffer newString=new StringBuffer();
int gap=2*numRows-2;//周期数
int n=numRows;//循环数
int index; while(n-->0){
if(n==numRows-1||n==0){
for(int i=numRows-n-1;i<s.length();i=i+gap){
newString.append(s.charAt(i));
}
}
else{
for(int i=numRows-n-1;i<s.length();i=i+gap){
newString.append(s.charAt(i));
if((index=(i/gap*gap+2*numRows-(i%gap)-2))<s.length())
newString.append(s.charAt(index));
}
}
}
return newString.toString(); }
}

Leetcode 6——ZigZag Conversion的更多相关文章

  1. LeetCode 6. ZigZag Conversion & 字符串

    ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...

  2. Leetcode 6. ZigZag Conversion(找规律,水题)

    6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...

  3. LeetCode 6 ZigZag Conversion 模拟 难度:0

    https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...

  4. LeetCode 6 ZigZag Conversion(规律)

    题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...

  5. [LeetCode][Python]ZigZag Conversion

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...

  6. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

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

  7. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

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

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

  9. 【leetcode】ZigZag Conversion

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

  10. leetcode 6. ZigZag Conversion

    https://leetcode.com/problems/zigzag-conversion/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" ...

随机推荐

  1. MFC中CFileDialog用法

    用CFileDialog选择了一个文件后,使用FILE::fopen打开文件错误,使用 的是相对地址,和王工调试了半天,怎么跟踪也没发现错误,原来如此......... CFileDialog文件选择 ...

  2. 芝麻HTTP:分析Robots协议

    利用urllib的robotparser模块,我们可以实现网站Robots协议的分析.本节中,我们来简单了解一下该模块的用法. 1. Robots协议 Robots协议也称作爬虫协议.机器人协议,它的 ...

  3. 二叉树与AVL树

    二叉树 什么是二叉树? 父节点至多只有两个子树的树形结构成为二叉树.如下图所示,图1不是二叉树,图2是一棵二叉树. 图1 普通的树                                    ...

  4. Ubuntu 14.04 鼠标消失解决方案

    Ubuntu 14.04 鼠标消失解决方案: 进入文字命令行模式,输入startx, 返回图像模式.

  5. Linux 的特殊变量(2)

    1.echo $?  输出结果为0 表示命令执行成功 场景:判断上一个命令是否成功 扩展 0:成功 2 :权限拒绝 1~125:表示运行失败 126:找到命令,但是无法执行 127:未找到要运行的命令 ...

  6. ORA-01940: cannot drop a user that is currently connected解决方法

    我们在删除数据库用户时候会碰到如下错误 SQL> DROP USER sys_xj cascade; DROP USER sys_xj cascade*ERROR at line 1:ORA-0 ...

  7. 用Python来找合适的妹子

    时间真的有点仓促,匆匆忙忙撸完这篇文章. 虽然今天是情人节,但还是要关心一下单身狗们,帮助他们俩脱单. 古人云:知己知彼,百战不殆.  好好去了解一下妹子们的内心想法,早日脱单! 这次我在一个某知名婚 ...

  8. 【BZOJ1499】瑰丽华尔兹(动态规划)

    [BZOJ1499]瑰丽华尔兹(动态规划) 题面 BZOJ 题解 先写部分分 设\(f[t][i][j]\)表示当前在\(t\)时刻,位置在\(i,j\)时走的最多的步数 这样子每一步要么停要么走 时 ...

  9. 【USACO09OCT】热浪Heat Wave

    题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for g ...

  10. 【CJOJ2499】【DP合集】棋盘 chess

    Description 给出一张 n × n 的棋盘,格子有黑有白.现在要在棋盘上放棋子,要求: • 黑格子上不能有棋子 • 每行每列至多只有一枚棋子 你的任务是求出有多少种合法的摆放方案.答案模 1 ...