1. 程式人生 > >LeetCode-6. Z 字形變換

LeetCode-6. Z 字形變換

題目地址:https://leetcode-cn.com/problems/zigzag-conversion/
題意:字面意思
思路:模擬一下就好了
AC程式碼:

class Solution {
public:
    string convert(string s, int numRows) {
        string ans = "";
        int length = s.length();
        int p = 0;
        if(numRows==1)
            return s;
        for(int i=0;i<numRows;i++){
            for(int j = i;j<length;j+=2*(numRows-1)){
                ans+=s[j];
                //cout<<s[j]<<" ";
                if(i!=0 && i!=numRows-1 && j+2*(numRows-i-1)<length ){
                    ans+=s[j+2*(numRows-i-1)];
                    //cout<< s[j+2*(numRows-i-1)]<<" ";
                }
            }
           // cout<<endl;
        }
        return ans;
    }
};