LeetCode之ZigZag Conversion

惭愧的说,这题我做了1天
想要Bug Free的代码好难,我不断地没有考虑周到
比如 没有考虑numRows为1的情况

Wrong Answer了6遍不敢提交了
老老实实在VS里试验了半天
顺便复习了下string类和成员函数的定义方法

看了看别人解的代码
发觉自己的思路还蛮奇葩的~
我的中心思想是对称

恩恩 睡觉

class Solution {
public:
    string convert(string s, int numRows) {
        string r = s;
        int i, j;
        int count = 0;
        int Max = s.size()-1;
        if (numRows!=1)
        {
            for (j = 0; j<numRows; j++)
            {
                for (i = 1; i <= Max/ (numRows-1)+1; i++)
                {
                    if (j == 0 || j == numRows - 1)
                    {
                        if ((numRows - 1)*(2 * i - 2) + j>=0 && (numRows - 1)*(2 * i - 2) + j<=Max)
                        {
                            r[count] = s[(numRows - 1)*(2 * i - 2) + j]; count++;
                        }
                    }
                    else{
                        if ((numRows - 1)*(2 * i - 2) - j>=0 && (numRows - 1)*(2 * i - 2) - j<=Max)
                        {
                            r[count] = s[(numRows - 1)*(2 * i - 2) - j]; count++;
                        }
                        if ((numRows - 1)*(2 * i - 2) + j>=0 && (numRows - 1)*(2 * i - 2) + j<=Max)
                        {
                            r[count] = s[(numRows - 1)*(2 * i - 2) + j]; count++;
                        }
                    }
                }
            }
        }
        return r;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章