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;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章