码迷,mamicode.com
首页 > 其他好文 > 详细

ZigZag Conversion

时间:2015-01-28 19:41:56      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

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字形,然后一行一行输出 

 

public class Solution {
    public String convert(String s, int nRows) {
        ArrayList<ArrayList<String>> allList = new ArrayList<ArrayList<String>>();//用于存放ZigZag后的二维字符数组
        char[] char_array = s.toCharArray();
        int size = char_array.length;//字符总长度
        
        if(size<=nRows||nRows==1){
            return s;
        }
        
        for(int i=0;i<nRows;i++){//初始化allList
            ArrayList<String> list = new ArrayList<String>();
            allList.add(list);
        }
        int x = 0,y = 0;//二维坐标(初始点为(0,0))
        int stepX = 0,stepY = 1;//2个坐标上的增量
        int value = 2*nRows-2;
        for(int n=0;n<size;n++){
            String letter = String.valueOf(char_array[n]);
            allList.get(y).add(letter);
            
            if(n%value == 0){
                stepX = 0;
                stepY = 1;
            }
            else if(n%value == nRows-1){
                stepX = 1;
                stepY = -1;
            }
            y = y+stepY;//跟新坐标
        }
        String result = "";
        
         for(int j=0;j<nRows;j++){
             ArrayList<String> lists = allList.get(j);
             for(String list:lists){
                 result = result+list;
             }
        }
        return result;
        
    }
}

 

ZigZag Conversion

标签:

原文地址:http://www.cnblogs.com/mrpod2g/p/4256653.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!