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

【LeetCode 6】Z 字形变换

时间:2019-11-04 00:19:03      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:ack   顺序   就是   string   过程   i++   模拟   lag   tps   

题目链接

【题解】


还想着模拟这个过程。然后发现只有行有用啊!...
那就建个rows大小的字符串数组存每行从左到右的字符就行啦。。
然后就是i从1变到n然后又变回1反复就好了。
最后把1..rows按顺序首尾连接在一起就行

【代码】

class Solution {
public:
    string convert(string s, int numRows) {
        if (numRows==1) return s;
        string dic[5000];
        for (int i = 1;i <= numRows;i++) dic[i]="";
        dic[1] = dic[1]+s[0];
        int len = s.size();
        int row = 1;
        int flag = 1;
        for (int i = 1;i<=len-1;i++){
            row+=flag;
            if (row>numRows){ row = numRows-1;flag = -1;}
            if (row<=0) {row = 2;flag = 1;}
            dic[row]=dic[row]+s[i];
        }
        string temp = "";
        for (int i = 1;i <= numRows;i++){
            temp = temp + dic[i];
        }
        return temp;
    }
};

【LeetCode 6】Z 字形变换

标签:ack   顺序   就是   string   过程   i++   模拟   lag   tps   

原文地址:https://www.cnblogs.com/AWCXV/p/11789826.html

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