亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試解決算法問題的曲折模式

嘗試解決算法問題的曲折模式

瀟湘沐 2022-06-09 16:47:02
問題如下:字符串“PAYPALISHIRING”在給定數量的行上以鋸齒形圖案寫入,如下所示:(您可能希望以固定字體顯示此圖案以提高可讀性)P   A   H   NA P L S I I GY   I   R然后逐行閱讀:“PAHNAPLSIIGYIR”編寫將采用字符串并在給定行數的情況下進行此轉換的代碼:字符串轉換(字符串 s,int numRows);示例 1:輸入:s = "PAYPALISHIRING", numRows = 3 輸出:"PAHNAPLSIIGYIR" 示例 2:輸入:s = "PAYPALISHIRING", numRows = 4 輸出:"PINALSIGYAHRPI" 解釋:P     I    NA   L S  I GY A   H RP     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))
查看完整描述

2 回答

?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

我重寫了您的 while 循環,如下所示,我只是走“之字形”模式!希望它足夠簡單易懂。


let c=0, row=0,col=0, down=0;

while(c<s.length) {

    results[row][col]=s[c];

    if(down==0) { // moving down 

        row++;

        if(row==numRows) {

            down = 1;

            col++;

            row-=2;

        }

    } else { // moving up

        row--;

        col++;

        if(row==0) {

            down=0;

        }

    }

    c++;

}

附言。上面的代碼無法處理numRows < 3,因此您必須在此循環之前管理它們。


查看完整回答
反對 回復 2022-06-09
?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

我的微積分有點生疏,但這個問題背后的邏輯似乎是一個正弦波。我在創建阻止它工作的 sin 方程的某個地方出現了數學錯誤(r 從不等于 c 與當前參數),但如果這是您選擇的方向,希望這會有所幫助。


/*If x-axis is position in string, and y-axis is row number...

n=number of rows


Equation for a sin curve: y = A sin(B(x + C)) + D

D=vertical shift (y value of mid point)

D=median of 1 and n

n:  median:

1   1

2   1.5

3   2

4   2.5

5   3

6   3.5

7   4

median=(n+1)/2

D=(n+1)/2


A=amplitude (from the mid-point, how high does the curve go)

median + amplitude = number of rows

amplitude = number of rows - median

A=n-D


C=phase shift

Phase 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π/2



Period is 2π/B

n   p

3   4

4   6

5   8

6   10

period=2(n-1)

2(n-1)=2π/B

B(2(n-1)=2π

B=2π/2(n-1)

B=π/(n-1)


Variables:

s = string

n = number of rows

c = current row number being evaluated

p = position in string

r = row number


*/

var output='';

function convert(s,n) {

    D=(n+1)/2

    A=n-D

    C=(3*Math.PI)/2

    B=Math.PI/(n-1)

  for (c=1;c<=n;c++) { //loop from 1st row to number of rows

    for (p=1;p<=s.length;p++) { //loop from 1st to last character in string

    r=A*Math.sin(B*(p+C))+D //calculate the row this character belongs in

        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

}


查看完整回答
反對 回復 2022-06-09
  • 2 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號