본문 바로가기

Algorithm/Implementation

(C++) - LeetCode (easy) 171. Excel Sheet Column Number

반응형

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

 

Excel Sheet Column Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

간단 구현 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

정답 ans를 선언해줍니다.

📔 풀이과정

alphabat형태를 하고 있으므로 26진수임을 알 수 있습니다. 진법 계산을 columnTitle을 순회하며 완료 후 ans에 해당 자리에 해당하는 수를 더해줍니다.

📔 정답출력

ans를 반환합니다.


📕 Code

📔 C++

class Solution {
public:
    int titleToNumber(string columnTitle) {
        int ans = 0;
        for(int i = 0; i < columnTitle.size(); i++) {
            ans += (columnTitle[columnTitle.size() - 1 -i] - 'A' + 1) * pow(26,i);
        }
        return ans;
    }
};

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.