Leetcode 6 Z型变换

class Solution {
public:
    string convert(string s, int numRows) {
        int size = s.size();
        
        if(numRows <= 1 )
        {
            return s;
        }
        int gap = 2*numRows -2;
        
        string  new_str = "";
        
       for(int i = 0;i < numRows; i++)
       {//i == row current 
           
         for(int j=i ; j< size; j+=gap)
         {
             
             new_str+=s[j];
             
             if(i > 0 && i < numRows -1 )
             {
               int t = j + gap -2*i;
                 if( t < size)
                new_str+=s[t];
                 
             }
             
             
         }
           
       }
        
        
        return new_str;
        
    }
};

Leetcode 6 Z型变换

Leetcode 6 Z型变换
黑色的位置规律为 +2n-2 用 gap 表示规律,j表示当前黑色位置
i表示为当前行
红色的位置规律为 j+ gap - 2*i

两个循环:
外循环遍历行数,内循环遍历每一行的操作。

相关文章:

  • 2022-12-23
  • 2021-10-04
  • 2021-12-11
  • 2021-05-18
  • 2021-08-06
  • 2021-06-18
  • 2021-09-17
  • 2022-12-23
猜你喜欢
  • 2021-08-03
  • 2021-12-22
  • 2021-05-11
  • 2021-10-02
  • 2021-05-11
  • 2021-10-14
  • 2022-12-23
相关资源
相似解决方案