题目:

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字形排列的数组,按照行输出
n=2时,字符串坐标变成zigzag的走法就是:

 0 2 4 6

 1 3 5 7

 n=3时的走法是:

 0     4     8

 1  3 5  7 9

 2     6    10 

 n=4时的走法是:

 0      6        12

 1   5 7    11 13

 2 4   8 10    14

 3      9         15 

 利用这个规律,可以按行填字,第一行和最后一行,就是按照2n-2的顺序排列。

 其他行除了上面那个填字规则,就是还要处理斜着那条线的字,可以发现那条线的字的位置永远是当前列j+(2n-2)-2i(i是行的index),同时和 2n-2间隔排列组成中间的行

代码:

public class Solution {
    public String convert(String s, int numRows) {
        if(s == null && s.length() == 0 && numRows <= 0){
            return "";
        }
        if(numRows == 1){
            return s;
        }
        StringBuffer sb = new StringBuffer();
        int size = 2*numRows - 2;
        for(int i = 0;i < numRows;i++){
            for(int j = i;j < s.length();j = j+size){
                sb.append(s.charAt(j));
            if(i != 0 && i != numRows-1){
                int tmp = j + size -2*i;
                if(tmp < s.length()){
                sb.append(s.charAt(tmp));
                }
                }
            }
        }
        return sb.toString();
    }
}

LeetCode(60)-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 ...

随机推荐

  1. 03 SeekBar 音频播放拖拽进度条

    八,  SeekBar  音频播放拖拽进度条       >                 android:progress="40"   第一进度         and ...

  2. MANIFEST.MF Error: No available bundle exports package

    Issue: When you imported some 3rd jars and compiled MANIFEST.MF, you may got following compling erro ...

  3. An universal algorithm design of fixed length substring locating

         An universal algorithm design of fixed length substring locating Stringlocating is a very commo ...

  4. Android项目-高考作文-抽象BaseAdapter

    1, 在使用baseAdapter的时候,总是有需要重复的代码如: @Override public int getCount() { return list.size(); } @Override ...

  5. (NO.00002)iOS游戏精灵战争雏形(十二)

    首先要声明的是,前几篇实现的shoot方法不是一定会命中目标,这取决于目标运行的速度,子弹的速度,子弹发射的时机以及弹道路径中是否有障碍物等等. 这也是符合实际情况的.如果你的要求是一旦发出子弹必定击 ...

  6. How to create DB2 user function easily by DB Query Analyzer 6.03

    How to create DB2user function easily by DB Query Analyzer 6.03 Ma Genfeng (Guangdong Unitoll Servic ...

  7. 高通平台手机开发之LCD

    4.1. LCD 参考文档: 1) 80-NA157-174_E_DSI_Programing_Guide_B-Family_Android_Devices.pdf 2) 80-NN766-1_A_L ...

  8. Dynamics CRM 2011/2013 通过Javascript给lookup字段赋值

    仅仅做下记录,因为老是用到但老是忘记 var value = new Array(); value[0] = new Object(); value[0].id = idValue; value[0] ...

  9. GCC内联函数:__builtin_types_compatible_p

    #if 0 - Built-in Function: int __builtin_types_compatible_p (type1, type2) You can use the built-in ...

  10. Leetcode_145_Binary Tree Postorder Traversal

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42876769 Given a binary tree, r ...