Given a column title as appearing in an Excel sheet, return its corresponding column number.
Example:
Input: s="ZY"
Output: 701
Approach
C++
#include <bits/stdc++.h>using namespace std;int titleToNumber(string s){int n = s.size();int res = 0;for (int i = 0; i < n; i++){res = res * 26;res += s[i] - 'A' + 1;}return res;}int main(){string s = "ZY";cout << titleToNumber(s);return 0;}
No comments:
Post a Comment