# 2194. Cells in a Range on an Excel Sheet ## 題目概要 給定一字串,以`:` 分隔兩個位置,這兩個位置是 excel 上面的 cell 位置,第一個為起始位置、第二個則為結尾位置,請輸出從起始點到結尾會經過的所有 cell。  ## 解題技巧 - 用 ASCII Code 來做,最簡單的方式是用 `str.charCodeAt(index)` 將字串轉為 ASCII code,比如 `const a = 'A'; const b = a.charCodeAt(0)` b 為 65。 - `String.fromCharCode()` 則是用於將 ASCII code 轉為字符,比如 `String.fromCharCode(65); // b` ## 程式碼 ```javascript= /** * @param {string} s * @return {string[]} */ var cellsInRange = function(s) { let startCol = s.charCodeAt(0); let startRow = parseInt(s[1]); let endCol = s.charCodeAt(3); let endRow = parseInt(s[4]); let result = []; for (let i = startCol; i <= endCol; i++) { let char = String.fromCharCode(i); for (let j = startRow; j <= endRow; j++) { result.push(char + j); } } return result; }; ``` 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up