[LeetCode] 2194. Cells in a Range on an Excel Sheet

Chobby·4일 전
1

LeetCode

목록 보기
685/710

😎풀이

  1. s 문자열을 :을 기준으로 분리
  2. 열에 해당하는 알파벳을 숫자형으로 형변환
  3. 열 순회
    3-1. 행 순회
    3-2. 현재 행렬을 연결하여 포함된 셀 배열에 추가
  4. 선택된 셀 목록 반환
function cellsInRange(s: string): string[] {
    const [startPos, endPos] = s.split(":")
    const [sc, sr] = startPos.split("")
    const [ec, er] = endPos.split("")
    const numStartCol = sc.charCodeAt(0)
    const numEndCol = ec.charCodeAt(0)
    const numStartRow = Number(sr)
    const numEndRow = Number(er)
    const listOfCell = []
    for(let col = numStartCol; col <= numEndCol; col++) {
        const curColAlphabet = String.fromCharCode(col)
        for(let row = numStartRow; row <= numEndRow; row++) {
            listOfCell.push(curColAlphabet + row)
        }
    }
    return listOfCell
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글