Excel Sheet Column Number
Problem:
Given a column title as appear in an Excel sheet, and return its corresponding column number.
Ideas:
Binary conversion method
My Code:
Public classSolution { Public intTitletonumber (String s) {if(s = =NULL|| S.length () = = 0)return0; intLen =s.length (); intCount = 0; intRET = 0; for(inti = len-1; I >=0; I--, count++) { intnum = S.charat (i)-' A ' + 1; RET+ = num * (int) Math.pow (26, Count); } returnret; }}
View Code
Others code:
Public class Solution { publicint titletonumber (String s) { int colnumber = 0; for (int i = 0; i < s.length (); i++) { = colnumber*26 + ((int) S.charat (i) -64); c19/>} return colnumber;} }
View Code
The Learning Place:
My idea is to move forward from the approach, more easily think of the algorithm, but every time to have to make the calculation of the binary.
Other people's idea is through the method of displacement, the size of each shift is 26 so that can be calculated from the front to the back, the program has become a lot more concise.
Excel Sheet Column Number