leetcode 171 Excel Sheet Column Number

lc171 Excel Sheet Column Number

Can go from high to low, for example: "ACANV" from A-> V

You can go from high to low: V-> A

The first solution: for (int i = 0; i <s.length (); i ++)

At each iteration, res * 26 coupled to s [i] - 'A' + 1

 1 class Solution {
 2     public int titleToNumber(String s) {
 3         int res = 0;
 4         char[] ss = s.toCharArray();
 5         
 6         for(int i = 0; i<ss.length; i++){
 7             res = res * 26 + ((int)(ss[i]-'A') + 1);
 8         }
 9         
10         return res;
11     }
12 }

 

 

A second solution of i = s.length () - 1 res

Direct addition (s [i] - 'A' + 1) * 26 ^ n

Guess you like

Origin www.cnblogs.com/hwd9654/p/10966805.html