1138. Alphabet Board Path

Leetcode 问题: 1138. Alphabet Board Path

题目

On an alphabet board, we start at position (0, 0), corresponding to character board[0][0].

Here, board = [“abcde”, “fghij”, “klmno”, “pqrst”, “uvwxy”, “z”], as shown in the diagram below.

Leetcode

根据字母版上面的路径,移动字母,把传入的字串拼完

答案

JavaScript

/**
 * @param {string} targetSentence
 * @return {string}
 */
var alphabetBoardPath = function(targetSentence) {

    // 字母移动路径
    let letterMovingPathMapping = {};
    let movingLeft = 'L';
    let movingRight = 'R';
    let movingUp = 'U';
    let movingDown = 'D';


    for (let beginLetter = 0; beginLetter < 26 ; beginLetter++ ) {
        // 初始化字母对应表
        letterMovingPathMapping[beginLetter] = {};
        for (let targetLetter = 0; targetLetter < 26 ; targetLetter++ ) {
            // 要移动的 X 距离 : 原始字母在那一层 X 位置 - 目标字母在那一层 X 位置
            //  - movingX > 0 : 往右移动 R
            //  - movingX < 0 : 往左移动 L
            let movingX = Math.floor(targetLetter % 5 - beginLetter % 5);
            // 要移动的 Y 距离 : 原始字母在那一层 Y - 目标字母在那一层 Y
            //  - movingY > 0 : 往下移动 D
            //  - movingY < 0 : 往上移动 U
            let movingY = Math.floor(targetLetter / 5) - Math.floor(beginLetter / 5);

            // 移动路径
            let movingPath = '';
            // 移动路径 Y 剩馀路径
            let movingYLeftPath = '';


            if (movingY < 0 ) {
                //  - movingY < 0 : 往上移动 U
                let movingStep = Math.abs(movingY);
                movingPath += movingUp.repeat(movingStep);
            }
            if (movingY > 0 ) {
                //  - movingY > 0 : 往下移动 D
                let movingStep = movingY;
                if (targetLetter == 25) {
                    // 移动到 Z,最后一步不要移动,会跑到空位置,留到最后一步再动
                    movingStep--;
                    movingYLeftPath = movingDown;
                }
                movingPath += movingDown.repeat(movingStep);
            }

            if (movingX < 0 ) {
                //  - movingX < 0 : 往左移动 L
                let movingStep = Math.abs(movingX);
                movingPath += movingLeft.repeat(movingStep);
            }
            if (movingX > 0 ) {
                //  - movingX > 0 : 往右移动 R
                let movingStep = movingX;
                movingPath += movingRight.repeat(movingStep);
            }

            // 加上最后一步要移动的 Y
            movingPath += movingYLeftPath;
            // 设定字母对应路径
            letterMovingPathMapping[beginLetter][targetLetter] = movingPath;
        }
    }

    // 移动路径
    let pathResult = '';
    // 字母 a 索引
    let baseLetterCode = 'a'.charCodeAt(0);
    // 起始字母索引
    let beginLetterIndex = 'a'.charCodeAt(0) - baseLetterCode;
    // 转换成小写
    let lowerCaseTargetSentence = targetSentence.toLowerCase();

    for (let i in lowerCaseTargetSentence) {
        // 目标字母索引
        let targetLetterIndex = targetSentence[i].charCodeAt(0) - baseLetterCode;
        // 设定对应路径
        pathResult += letterMovingPathMapping[beginLetterIndex][targetLetterIndex] + '!';
        // 将目前指标位置设定为当前字母
        beginLetterIndex = targetLetterIndex;
    }

    return pathResult;
};

参考资料