Skip to main content

Leetcode - 171. Excel Sheet Column Number

· One min read

https://leetcode.com/problems/excel-sheet-column-number/

Solution 1

/**
* @param {string} columnTitle
* @return {number}
*/
var titleToNumber = function (columnTitle) {
if (columnTitle.length === 1) {
return (columnTitle.charCodeAt(0) % 65) + 1;
}

let result = 0;

for (let i = columnTitle.length - 1; i >= 0; i--) {
const strCode = (columnTitle[i].charCodeAt(0) % 65) + 1;
if (i === columnTitle.length - 1) {
result += strCode;
} else {
result += strCode * 26 ** (columnTitle.length - i - 1);
}
}
return result;
};