6. ZigZag Conversion

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 NA P L S I I GY 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"

这个题可以叫找规律。。。细心点就好了。。。找到每个元素下标的规律就好啦。

var convert = function(s, numRows) {
    if (numRows===1)
        return s;
    var cycle = numRows*2-2;
    var result = "";
    var length = s.length;
    var diff = (numRows-2)*2;
    for (var i = 0;i < numRows;i++) {
        var count = 0;
        if (i===0||i===(numRows-1)) {
            while ((i+count*(cycle))

你可能感兴趣的:(6. ZigZag Conversion)