반응형
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;
}
};
*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
'Algorithm > Implementation' 카테고리의 다른 글
(C++) - LeetCode (easy) 219. Contains Duplicate II (0) | 2023.01.09 |
---|---|
(C++) - LeetCode (easy) 217. Contains Duplicate (0) | 2023.01.08 |
(C++) - LeetCode (easy) 168. Excel Sheet Column Title (2) | 2022.12.08 |
(C++) - LeetCode (easy) 119. Pascal's Triangle II (2) | 2022.11.28 |
(C++) - LeetCode (easy) 118. Pascal's Triangle (0) | 2022.11.27 |