• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Uriel's Corner

            Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
            posts - 0, comments - 50, trackbacks - 0, articles - 594
            將一個之字形排列的字符串按行輸出,e.g.,

            P    A   H    N
            A P L S I  I G
            Y    I    R

            Input: s = "PAYPALISHIRING", numRows = 3
            Output: "PAHNAPLSIIGYIR"

            直接模擬即可,python寫得比較爛


            C++版:


             1 #6
             2 #Runtime: 88 ms (Beats 13.2%)
             3 
             4 class Solution {
             5 public:
             6     string convert(string s, int nRows) {
             7         if(nRows == 1) return s;
             8         string res;
             9         int i = 0, j = 0, n = s.length();
            10         while(j < s.length()) {
            11             res.push_back(s[j]);
            12             j += 2 * nRows - 2;
            13         }
            14         for(i = 1; i < nRows - 1; ++i) {
            15             j = i;
            16             while(j < s.length()) {
            17                 res.push_back(s[j]);
            18                 j += 2 * nRows - 2 * (i + 1);
            19                 if(j < s.length()) {
            20                     res.push_back(s[j]);
            21                     j += 2 * i;
            22                 }
            23             }
            24         }
            25         j = nRows - 1;
            26         while(j < s.length()) {
            27             res.push_back(s[j]);
            28             j += 2 * nRows - 2;
            29         }
            30         return res;
            31     }
            32 };



            Python版:


             1 #6
             2 #Runtime: 1107 ms (Beats 10.51%)
             3 #Memory: 21.3 MB (Beats 5.28%)
             4 
             5 class Solution(object):
             6     def convert(self, s, numRows):
             7         """
             8         :type s: str
             9         :type numRows: int
            10         :rtype: str
            11         """
            12         if numRows == 1:
            13             return s
            14         col, i = 0, 0
            15         ans = []
            16         while i < len(s):
            17             if col % (numRows - 1) == 0:
            18                 t = []
            19                 for j in range(numRows):
            20                     if i >= len(s):
            21                         t.append(' ')
            22                     else:
            23                         t.append(s[i])
            24                         i += 1
            25             else:
            26                 t= []
            27                 for j in range(numRows - 1, -1, -1):
            28                     if j % (numRows - 1) == col % (numRows - 1):
            29                         t.append(s[i])
            30                         i += 1
            31                     else:
            32                         t.append(' ')
            33             ans.append(t)
            34             col += 1
            35         ans = list(map(list, zip(*ans)))
            36         ans = [i for item in ans for i in item]
            37         ans = ''.join(ans)
            38         return ans.replace(' ''')
            久久亚洲电影| 亚洲精品无码久久久久AV麻豆| 国产A三级久久精品| 亚洲人成伊人成综合网久久久| 亚洲综合日韩久久成人AV| 久久精品成人国产午夜| 色悠久久久久久久综合网| 久久狠狠爱亚洲综合影院| 国产一级做a爰片久久毛片| 久久午夜福利电影| 无码人妻少妇久久中文字幕蜜桃| 99久久无码一区人妻| 久久久一本精品99久久精品88| 久久精品成人免费网站| 久久WWW免费人成—看片| 久久亚洲AV成人无码国产| 久久精品国产黑森林| 国产精品久久永久免费| 亚洲精品国产第一综合99久久| 精品九九久久国内精品| 亚洲AV日韩精品久久久久久久 | 国产精品99久久久久久www| 中文字幕人妻色偷偷久久| 久久综合伊人77777麻豆| 久久99国产精品一区二区| 久久综合噜噜激激的五月天| 欧美与黑人午夜性猛交久久久| 91久久香蕉国产熟女线看| 久久久久久国产精品免费无码| 久久亚洲精品无码aⅴ大香| 久久国产热这里只有精品| 国産精品久久久久久久| 国内精品九九久久久精品| 新狼窝色AV性久久久久久| 国产美女亚洲精品久久久综合| 久久这里的只有是精品23| 久久久国产99久久国产一| 国产精品99久久久精品无码| 午夜视频久久久久一区| 午夜精品久久影院蜜桃 | 久久久久久久综合狠狠综合|