尝试解决算法问题的曲折模式

问题如下:


字符串“PAYPALISHIRING”在给定数量的行上以锯齿形图案写入,如下所示:(您可能希望以固定字体显示此图案以提高可读性)


P   A   H   N

A P L S I I G

Y   I   R

然后逐行阅读:“PAHNAPLSIIGYIR”


编写将采用字符串并在给定行数的情况下进行此转换的代码:


字符串转换(字符串 s,int numRows);示例 1:


输入:s = "PAYPALISHIRING", numRows = 3 输出:"PAHNAPLSIIGYIR" 示例 2:


输入:s = "PAYPALISHIRING", numRows = 4 输出:"PINALSIGYAHRPI" 解释:


P     I    N

A   L S  I G

Y A   H R

P     I

我已经编写了以下代码,但是在如何将行标记为一次向下移动方面我陷入了困境,我增加了起始行,但是当它曲折回到顶部时,它应该减少。我无法弄清楚在不影响向下运动的情况下进行这项工作的逻辑。任何帮助,将不胜感激。


const convert = (s, numRows) => {

    let startRow = 0

    let endRow = numRows - 1

    let startColumn = 0

    let endColumn = Math.floor((s.length / 2) - 1)

    s = s.split('')

    let results = []

    // to setup the columns


    for (let i = 0; i < numRows; i++) {

        results.push([])

    }


    while (startRow <= endRow && startColumn <= endColumn && s.length) {

        for (let i = startRow; i <= endRow; i++) {

            results[i][startColumn] = s.shift()

        }

        for (let i = endRow - 1; i >= startRow; i--) {

            results[i][startColumn + 1] = s.shift()

            startColumn++

        }

        //this line seems to be the issue

        startRow++

    }

    return results

}


console.log(convert('PAYPALISHIRING', 4))


潇湘沐
浏览 214回答 2
2回答

拉丁的传说

我重写了您的 while 循环,如下所示,我只是走“之字形”模式!希望它足够简单易懂。let c=0, row=0,col=0, down=0;while(c<s.length) {&nbsp; &nbsp; results[row][col]=s[c];&nbsp; &nbsp; if(down==0) { // moving down&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; row++;&nbsp; &nbsp; &nbsp; &nbsp; if(row==numRows) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; down = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; col++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row-=2;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else { // moving up&nbsp; &nbsp; &nbsp; &nbsp; row--;&nbsp; &nbsp; &nbsp; &nbsp; col++;&nbsp; &nbsp; &nbsp; &nbsp; if(row==0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; down=0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; c++;}附言。上面的代码无法处理numRows < 3,因此您必须在此循环之前管理它们。

德玛西亚99

我的微积分有点生疏,但这个问题背后的逻辑似乎是一个正弦波。我在创建阻止它工作的 sin 方程的某个地方出现了数学错误(r 从不等于 c 与当前参数),但如果这是您选择的方向,希望这会有所帮助。/*If x-axis is position in string, and y-axis is row number...n=number of rowsEquation for a sin curve: y = A sin(B(x + C)) + DD=vertical shift (y value of mid point)D=median of 1 and nn:&nbsp; median:1&nbsp; &nbsp;12&nbsp; &nbsp;1.53&nbsp; &nbsp;24&nbsp; &nbsp;2.55&nbsp; &nbsp;36&nbsp; &nbsp;3.57&nbsp; &nbsp;4median=(n+1)/2D=(n+1)/2A=amplitude (from the mid-point, how high does the curve go)median + amplitude = number of rowsamplitude = number of rows - medianA=n-DC=phase shiftPhase shift for a sin curve starting at its lowest point: 3π/2(so at time 1, row number is 1, and curve goes up from there)C=3π/2Period is 2π/Bn&nbsp; &nbsp;p3&nbsp; &nbsp;44&nbsp; &nbsp;65&nbsp; &nbsp;86&nbsp; &nbsp;10period=2(n-1)2(n-1)=2π/BB(2(n-1)=2πB=2π/2(n-1)B=π/(n-1)Variables:s = stringn = number of rowsc = current row number being evaluatedp = position in stringr = row number*/var output='';function convert(s,n) {&nbsp; &nbsp; D=(n+1)/2&nbsp; &nbsp; A=n-D&nbsp; &nbsp; C=(3*Math.PI)/2&nbsp; &nbsp; B=Math.PI/(n-1)&nbsp; for (c=1;c<=n;c++) { //loop from 1st row to number of rows&nbsp; &nbsp; for (p=1;p<=s.length;p++) { //loop from 1st to last character in string&nbsp; &nbsp; r=A*Math.sin(B*(p+C))+D //calculate the row this character belongs in&nbsp; &nbsp; &nbsp; &nbsp; if (r==c) { output+= s.charAt(r) } //if the character belongs in this row, add it to the output variable. (minus one because character number 1 is at position 0)}}//do something with output here}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript