Likou Brush Hundred Days Plan Day4 Z-shape transformation

learning target:

I will continue to update my unique algorithm ideas, hoping to bring you different thinking expansion!
If you find it helpful, please like, follow and support!
Your encouragement is what keeps me going!
! ! !

Likou Question Bank Question 6 Official Link


Learning Content:

zigzag transformation

将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。

比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下:

P   A   H   N
A P L S I I G
Y   I   R
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"。

请你实现这个将字符串进行指定行数变换的函数:

string convert(string s, int numRows);

Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
PIN
ALSIG
YAHR
PI
Example 3:

Input: s = "A", numRows = 1
Output: "A"

提示:

1 <= s.length <= 1000
s 由英文字母(小写和大写)、',''.' 组成
1 <= numRows <= 1000

Source: LeetCode
Link: https://leetcode-cn.com/problems/zigzag-conversion

study-time:

2022.1.10


Learning output:

Thought 1
insert image description here
Why does it take so long to execute?
Because the algorithm logic is not clever enough, it is the result of hard solution. But the design idea is quite clever!

Problem- solving ideas
insert image description here
1. First, we can use a two-dimensional array to store the Z-character sequence, and then read the two-dimensional array in order to get the result
2. How do we construct this two-dimensional array? First, get the number of rows and columns of the array. The number of rows must be numRows, so what about the number of columns? After careful analysis, it is not difficult to find that the number of columns is actually the length of the string s divided by two plus one.
3. How do we put this in zigzag order? We just need to put the characters of the string one by one in order from top to bottom. To this end, I wrote a method to get a character from a string. In fact, this process can be done with a queue, but the logic is relatively simple, so I use a pointer instead.
4. The point is, we traverse the first column first, and then traverse the second column. According to the requirements of the title, we can find the rule, if the number of rows is 4, then there will be two short columns between the two long columns, and if the number of rows is 3, then there will be a short column between the two long columns. In fact, it is very simple, that is, the first and last parts of the short column part have no elements. So how do we know whether the current column is a long column or a short column? By index, we can take the remainder of the current index by numRows - 1, which is a long column if it is 0. For example, index 0, long column, at this time numRows is 3, then the remaining one is 0 is 2. 2%(3-1)==0
5. We already know how to judge long columns and short columns, we just You can use two for loops to iterate over the entire two-dimensional array. If it is a long column, just write from top to bottom. If it is a short column, write from bottom to top, and go out beyond the two points on the edge. When writing a long column, we can write to the entire column, but when writing a short column, we have to exit after writing one and then go to the next column. Because there is only one short column per column.
6. We write # after all the strings are taken. Because if the Char array is not assigned, the default is '\u0000', so when we output the result, we only need to use two for loops to traverse the two-dimensional array, and then judge, if it is not # or \u0000, then record it.
7. Return the recorded string, which is the Z string we want.

public class Solution {
    
    
    public string Convert(string s, int numRows) {
    
    
        //(s.Length/numRows)*2-1就是Z型数组长度
        //numRows就是宽度
        //s的长度除以宽度就是 最长的列的个数
        //Z型数组长度减去最长的列的个数 就是短列的个数
        //如果传入s长度14  rows为3
        //那么Z型数组长度为(14/3)*2-1=7
        //最长列个数为14/3 为4
        //短列为7-4 为3
        //其次我们还需要用长度
        
        if(s.Length==0||s.Length==1||numRows==1)return s;

        int ArrayLength=s.Length/2+1;   //Z型数组长度   7
        int LongLength=(s.Length/numRows);
        int ShortLength=ArrayLength-LongLength;
        int between=numRows-2;  //短的        //2
      
        char[,] rows=new char[numRows,ArrayLength];
        
        int curindex=-1; //现在放入的个数
        
        for(int i=0;i<ArrayLength;i++){
    
      //0-6
            for(int j=0;j<numRows;j++){
    
       //0-2
               // if(curCount>=s.Length){
    
    
               //     rows[j,i]='#';
               //     continue;
               // }

                if((i%(between+1)!=0&&i!=0) ){
    
     //是短列
                    if(numRows>2){
    
    
                        if(j==0||j==numRows-1){
    
     //奇数列头尾两个不设值
                            rows[numRows-i%(between+1)-1,i]='#';
                        }else{
    
    
                            rows[numRows-i%(between+1)-1,i]=GetOneChar();
                            break;
                        }
                    }else{
    
    
                        rows[numRows-i%(between+1)-1,i]=GetOneChar();
                        break;
                    }
                }else{
    
    
                    rows[j,i]=GetOneChar();
                } 
                
            }
        }

        string ss="";
        for(int i=0;i<numRows;i++){
    
    
            for(int j=0;j<ArrayLength;j++){
    
    
                if(rows[i,j]!='#'&&rows[i,j]!='\u0000'){
    
    
                    ss+=rows[i,j];
                }
            }
        }

        ss.Replace("\u0000", string.Empty);
        return ss;


        char GetOneChar(){
    
    
            curindex++;
            if(curindex>=s.Length){
    
    
                 return '#';
            }else{
    
    
                return s[curindex];
            }
        }
    }
}

Author: guinea pig Xiaohuihui
The copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/m0_48781656/article/details/122416452