Given a string columnTitle
that represents the column title as appears in an Excel sheet, return its corresponding column number.
For example:
A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
Example 1:
Input: columnTitle = "A " Output: 1
Example 2:
Input: columnTitle = "AB " Output: 28
Example 3:
Input: columnTitle = "ZY " Output: 701
Constraints:
1 <= columnTitle.length <= 7
columnTitle
consists only of uppercase English letters.columnTitle
is in the range [ "A ", "FXSHRXW "]
.program main
implicit none
character(len=7), parameter :: columnTitle = "A "
integer :: columnNumber
call getColumnNumber(columnTitle, columnNumber)
write (*, '(I0)') columnNumber
contains
subroutine getColumnNumber(columnTitle, columnNumber)
implicit none
character(len=*), intent(in) :: columnTitle
integer, intent(out) :: columnNumber
integer :: i
character :: c
columnNumber = 0
do i = len(columnTitle), 1, -1
c = columnTitle(i:i)
if (c >= 'A' .and. c <= 'Z') then
columnNumber = columnNumber * 26 + ichar(c) - ichar('A') + 1
end if
end do
end subroutine getColumnNumber
end program main
1
! Declare variables
integer :: i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
character(len=7) :: columnTitle
character(len=1) :: letter
integer :: columnNumber
! Initialize variables
i = 0
j = 0
k = 0
l = 0
m = 0
n = 0
o = 0
p = 0
q = 0
r = 0
s = 0
t = 0
u = 0
v = 0
w = 0
x = 0
y = 0
z = 0
letter = ' '
columnNumber = 0
! Read input
read(*,*) columnTitle
! Convert column title to number
do i = 1, len(columnTitle)
letter = columnTitle(i:i)
select case (letter)
case ('A')
columnNumber = columnNumber + 1
case ('B')
columnNumber = columnNumber + 2
case ('C')
columnNumber = columnNumber + 3
case ('D')
columnNumber = columnNumber + 4
case ('E')
columnNumber = columnNumber + 5
case ('F')
columnNumber = columnNumber + 6
case ('G')
columnNumber = columnNumber + 7
case ('H')
columnNumber = columnNumber + 8
case ('I')
columnNumber = columnNumber + 9
case ('J')
columnNumber = columnNumber + 10
case ('K')
columnNumber = columnNumber + 11
case ('L')
columnNumber = columnNumber + 12
case ('M')
columnNumber = columnNumber + 13
case ('N')
columnNumber = columnNumber + 14
case ('O')
columnNumber = columnNumber + 15
case ('P')
columnNumber = columnNumber + 16
case ('Q')
columnNumber = columnNumber + 17
case ('R')
columnNumber = columnNumber + 18
case ('S')
columnNumber = columnNumber + 19
case ('T')
columnNumber = columnNumber + 20
case ('U')
columnNumber = columnNumber + 21
case ('V')
columnNumber = columnNumber + 22
case ('W')
columnNumber = columnNumber + 23
case ('X')
columnNumber = columnNumber + 24
case ('Y')
columnNumber = columnNumber + 25
case ('Z')
columnNumber = columnNumber + 26
case ('AA')
columnNumber = columnNumber + 27
case ('AB')
columnNumber = columnNumber + 28
case ('AC')
columnNumber = columnNumber + 29
case ('AD')
columnNumber = columnNumber + 30
case ('AE')
columnNumber = columnNumber + 31
case ('AF')
columnNumber = columnNumber + 32
case ('AG')
columnNumber = columnNumber + 33
case ('AH')
columnNumber = columnNumber + 34
case ('AI')
columnNumber = columnNumber + 35
case ('AJ')
columnNumber = columnNumber + 36
case ('AK')
columnNumber = columnNumber + 37
case ('AL')
columnNumber = columnNumber + 38
case ('AM')
columnNumber = columnNumber + 39
case ('AN')
columnNumber = columnNumber + 40
case ('AO')
columnNumber = columnNumber + 41
case ('AP')
columnNumber = columnNumber + 42
case ('AQ')
columnNumber = columnNumber + 43
case ('AR')
columnNumber = columnNumber + 44
case ('AS')
columnNumber = columnNumber + 45
case ('AT')
columnNumber = columnNumber + 46
case ('AU')
columnNumber = columnNumber + 47
case ('AV')
f951: Error: Unexpected end of file in βtemp.f95β
def title_to_number(column_title: str) -> int:
result = 0
for c in column_title:
result = result * 26 + (ord(c) - ord('A') + 1)
return result
The algorithm iterates through the given columnTitle
string and computes the corresponding column number by treating the string like a base-26 number representation, where 'A' represents the digit 1 and 'Z' represents the digit 26. To convert each character to its corresponding numeric value, we subtract the ASCII value of 'A' and add 1. In each iteration, the current result is multiplied by 26 and the numeric value of the current character is added to the result. Finally, the accumulated result is returned as the column number.
int titleToNumber(string columnTitle) {
int result = 0;
for (char c : columnTitle) {
result = result * 26 + (c - 'A' + 1);
}
return result;
}
The algorithm iterates through the given columnTitle
string and computes the corresponding column number by treating the string like a base-26 number representation, where 'A' represents the digit 1 and 'Z' represents the digit 26. To convert each character to its corresponding numeric value, we subtract the ASCII value of 'A' and add 1. In each iteration, the current result is multiplied by 26 and the numeric value of the current character is added to the result. Finally, the accumulated result is returned as the column number.