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

Leetcode--ZigZag Conversion

时间:2016-11-10 03:05:07      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:处理   长度   return   code   div   blog   version   leetcode   --   

这道题就是简单的处理字符,通过观察计算就可以得出每个重复字符组的长度为2×numRows-2,然后直接处理就行

static string convert(string s, int numRows) {
        string result;
        int length = 2 * numRows - 2;
        string save[numRows];
        for (int i = 0; i < numRows; i++)
            for (int j = i; j < s.length(); j += length) {
                save[i] += s[j];
                if (i != 0 && i != numRows - 1 && j + length - 2 * i < s.length())
                    save[i] += s[j + length - 2 * i];
            }
        for (int i = 0; i < numRows; i++)
            result += save[i];
        return result;
    }

 

Leetcode--ZigZag Conversion

标签:处理   长度   return   code   div   blog   version   leetcode   --   

原文地址:http://www.cnblogs.com/INnoVationv2/p/6049419.html

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