题意:给你一个字符串和行数numRows,要求把该字符串变成一个“之”字形状后,按行数输出该字符串。

例子:"ABCDEFGHIJKLMNO", 4。

该字符串的“之”字形状为:

A       G      M
B F H L N
C E I K O
D J

那么,输出为AGMBFHLNCEIKODJ (按行数输出)

思路:数学推导公式。在字符串中,有两行是特殊的,那就是第一行和最后一行。

     第一行和最后一行,字符都是在一竖上,且字符之间相距的距离都是diff = 2*(numRows-1) (距离是指在原字符串中字符的下标之差)。

   其余的行,在每竖字符之间都会有一个字符,比如F,它与左边的字符B的距离则为2*(numRows-i-1) ,i为B的行标(1)。

public class Solution {
public String convert(String s, int numRows) {
StringBuilder sb = new StringBuilder(); if (numRows == 1) return s; int diff = 2*(numRows-1); for (int i = 0; i < numRows; i++) {
int x,x2;
if (i == 0 || i == numRows-1) {
x = i;
while (x < s.length()) {
sb.append(s.charAt(x));
x += diff;
} }
else {
x = i;
x2 = x + 2*(numRows-i-1);
while (x <s.length()) {
sb.append(s.charAt(x));
x2 = x + 2*(numRows-i-1);
if (x2 < s.length()) {
sb.append(s.charAt(x2));
}
x += diff;
}
}
}
return sb.toString(); }
}

  

LeetCode 6. ZigZag Conversion Question的更多相关文章

  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:ZigZag Conversion 曲线转换

    Question: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...

  6. [LeetCode][Python]ZigZag Conversion

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

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

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

  8. [LeetCode 题解]: ZigZag Conversion

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

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

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

随机推荐

  1. Mounting File Systems

    1.Mounting File Systems Just creating a partition and putting a file system on it is not enough to s ...

  2. IPython notebook 使用介绍

    参考资料: http://mindonmind.github.io/2013/02/08/ipython-notebook-interactive-computing-new-era/ http:// ...

  3. Linux 机器之间建立互信

    原理: 就是两台机器(web-1和web-2)经过预先设置好经过认证的key文件,双方互相访问时,进行自动认证,从而实现互信.   互信的原理了解了,我们可以把配置ssh互信的步骤进行有效的分割. 1 ...

  4. HDU 1199 - Color the Ball 离散化

    [题意]现在有几个球排成一排,编号从1开始,开始时所有球为黑色,现在有n(<=2000)次操作,每次操作将l[i]至r[i](均在int范围)的球凃成颜色c[i](黑色'b'或白色'w'),然后 ...

  5. Freemarker常用技巧(二)

    1 list.break指令<#list sequence as item>  ...</#list>tem_index:当前变量的索引值.item_has_next:是否存在 ...

  6. 引用类型和原始类型的对比(java)

    Java 提供两种不同的类型:引用类型和原始类型(或内置类型).另外,Java 还为每个原始类型提供了封装类(Wrapper). 原始类型 封装类=================boolean Bo ...

  7. MD5加密类

    public class MD5Util { public static String getMD5(String s) { char hexDigits[] = {'0', '1', '2', '3 ...

  8. MD5加密运算

    //MD5 对字符串的加密 -(void)demo1 { NSString *str = @"love"; //对字符串进行MD5加密 str = str.md5String; N ...

  9. Android Support V7 包中 ActionBar的使用

    以下示例为API<11,因为API>=11时本来就有ActionBar可以使用,所以不猜讨论范围之内 今天Google发布了最新的API 18,包括众多新的性能,正好最近在研究Action ...

  10. storm-kafka教程

    一.原理介绍   本文内容参考:https://github.com/apache/storm/tree/master/external/storm-kafka#brokerhosts (一)使用st ...