###### tags: `codeReview` `LeetCode` # [C]alphabetBoardPath ### Demo ```typescript= const START_CHAR = 'a' const LAST_CHAR = 'z' const COLUMN_COUNT = 5 const SELECTOR = '!' const MOVE_X = { axis: 'x', forward: 'R', back: 'L' } const MOVE_Y = { axis: 'y', forward: 'D', back: 'U' } const START_CHAR_CODE_AT = START_CHAR.charCodeAt() //97 const LAST_ROW = Math.floor((LAST_CHAR.charCodeAt() - START_CHAR_CODE_AT) / COLUMN_COUNT) const MEMORY_CHAR_POINT_DIC = {} const findCharPoint = ({ char }) => { if (MEMORY_CHAR_POINT_DIC[char]) return MEMORY_CHAR_POINT_DIC[char] const charDistance = char.charCodeAt() - START_CHAR_CODE_AT return MEMORY_CHAR_POINT_DIC[char] = { x: charDistance % COLUMN_COUNT, y: Math.floor(charDistance / COLUMN_COUNT) } } const findMoveModes = ({ nextAt }) => { if (nextAt.y != LAST_ROW) return [MOVE_Y, MOVE_X] return [MOVE_X, MOVE_Y] } const findMovePath = ({ currentAt, nextAt }) => { const modes = findMoveModes({nextAt}) // [MOVE_X, MOVE_Y] return modes.map(({ axis, forward, back }) => { const distance = nextAt[axis] - currentAt[axis] if (distance === 0) return '' if (distance > 0) return forward.repeat(distance) return back.repeat(-distance) }) } const alphabetBoardPath = target => { let currentAt = findCharPoint({ char: START_CHAR }) // {x:0,y:0} const charPaths = target.split('').map(char => { const nextAt = findCharPoint({ char }) // {x:1,y:2}, const path = findMovePath({ currentAt, nextAt }) //[D,D,R] currentAt = nextAt return path.join('') + SELECTOR //DDR! }) return charPaths.join('') } alphabetBoardPath('leet') ``` ### 偉恩 [[TypeScript] For Bginners From Taiwanese](https://leetcode.com/problems/alphabet-board-path/discuss/783054/TypeScript-For-Bginners-From-Taiwanese) ### 政儒 #### #2 ```typescript= function alphabetBoardPath(target: string): string { const boardRow = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]; const targetArr = target.split(''); const targetLen = targetArr.length; let currentRow = 0; let currentCol = 0; let output = '' // 幾個字母 for(let i=0; i<targetLen; i++){ // row /*找rowNo*/ const rowNo = boardRow.findIndex(x => x.includes(targetArr[i])); /*算rowGap*/ const rowGap = rowNo-currentRow; /*給currentRow*/ currentRow = currentRow+rowGap; // column /*找column*/ const boardCol = boardRow[rowNo].split(''); const colNo = boardCol.findIndex(x => x === targetArr[i]); /*算colGap*/ const colGap = colNo-currentCol; /*給currentCol*/ currentCol = currentCol+colGap; // 列印 #row[U|D] #col[L|R] output += rowNo+1 !== boardRow.length ? print(rowGap, 1) : print(colGap, 3); output += rowNo+1 !== boardRow.length ? print(colGap, 3) : print(rowGap, 1); output += '!'; } return output; }; // # count # dire 方向(橫:1[U、D]|直:3[L、R]) function print(count: number, dire: number) { const moveWord = ['U', 'D', 'L', 'R']; let pri = ''; while (count !== 0) { if (count > 0) { pri += moveWord[dire]; count--; }else { pri += moveWord[dire-1]; count++; } } return pri } ``` #### #1: 錯誤"zbz" ```typescript= const boardRow = ["abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"]; function alphabetBoardPath(target: string): string { const targetArr = target.split(''); const targetLen = targetArr.length; let currentRow = 0; let currentCol = 0; let output = '' // 幾個字母 for(let i=0; i<targetLen; i++){ // row /*找rowNo*/ const rowNo = boardRow.findIndex(x => x.includes(targetArr[i])); /*列印*/ const rowGap = rowNo-currentRow; output += print(rowGap, 1); currentRow = currentRow+rowGap; // column /*找column*/ const boardCol = boardRow[rowNo].split(''); const colNo = boardCol.findIndex(x => x === targetArr[i]); /*列印*/ const colGap = colNo-currentCol; output += print(colNo-currentCol, 3); currentCol = currentCol+colGap; output += '!'; } return output; }; // # count # dire 方向(橫U、D:1|直L、R:3) function print(count: number, dire: number) { const moveWord = ['U', 'D', 'L', 'R']; let pri = ''; while (count !== 0) { if (count > 0) { pri += moveWord[dire]; count--; }else { pri += moveWord[dire-1]; count++; } } return pri } ```