Excel Sheet Column Title
Problem:
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
Ideas:
Inbound access issues
My Code:
Public classSolution { PublicString Converttotitle (intN) {stringbuffer sb=NewStringBuffer (); if(n <= 0)returnsb.tostring (); while(n! = 0) {sb.append (GetChar (n-1)%26)); N= (n-1)/26; } returnsb.reverse (). toString (); } Public CharGetChar (intnum) { return(Char) (' A ' +num); }}View Code
Others code:
Public class Solution { public String converttotitle (int n) { new StringBuilder (); while (n>0) { result.append ((char) ((n-1)%26 + (int) ' A '); = (n-1)/26; } return result.reverse (). toString (); }}
View Code
The Learning Place:
- In Java, char is two bytes, int is four bytes, so char to int to use the cast, often forget this point, have several times warning.
- usually with S + = part never wanted s = part + s So it can be easily implemented reverse, learning, concrete implementation in other people's code inside there.
Excel Sheet Column Title