Skip to content

Commit

Permalink
update solutions to 1 line for Excel-Sheet-Column-Title
Browse files Browse the repository at this point in the history
  • Loading branch information
xcv58 committed Dec 21, 2014
1 parent 4a553a7 commit caaf84a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 8 deletions.
4 changes: 1 addition & 3 deletions Excel-Sheet-Column-Title/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ using namespace std;
class Solution {
public:
string convertToTitle(int n) {
if (n == 0) { return ""; }
char c = --n % 26 +'A';
return convertToTitle(n / 26) + c;
return n == 0 ? "" : convertToTitle((n - 1) / 26) + (char) ((n - 1) % 26 + 'A') ;
}
};

Expand Down
6 changes: 1 addition & 5 deletions Excel-Sheet-Column-Title/Solution.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
class Solution:
# @return a string
def convertToTitle(self, num):
result = []
while num != 0:
result.append((num - 1) % 26)
num = (num - 1) / 26
return ''.join([chr(x + 65) for x in reversed(result)])
return "" if num == 0 else self.convertToTitle((num - 1) / 26) + chr((num - 1) % 26 + 65)

0 comments on commit caaf84a

Please sign in to comment.