# My solutions in 3 languages, does any one have one line solution in Java or C++?

• Java:

``````int result = 0;
for (int i = 0; i < s.length(); result = result * 26 + (s.charAt(i) - 'A' + 1), i++);
return result;
``````

C++:

``````int result = 0;
for (int i = 0; i < s.size(); result = result * 26 + (s.at(i) - 'A' + 1), i++);
return result;
``````

Python:

``````return reduce(lambda x, y : x * 26 + y, [ord(c) - 64 for c in list(s)])
``````

Python version is beautiful because reduce function and list comprehensive.

I don't know whether exist similar approach to achieve one line solution in Java or C++.
One possible way is defining another method like this:

``````public int titleToNumber(int num, String s)
``````

to store previous result and make recursive call.
But this add much more lines.

• you wanna this?

``return s != "" ? 26*titleToNumber(s.substr(0,s.size()-1)) + s[s.size()-1] -'A'+1 : 0;``

• Yes, it works. Thanks!

• Java code like this?
`return s.length()==0?0:(s.charAt(s.length()-1)-'A'+1)+26*titleToNumber(s.substring(0,s.length()-1));`

``` ```

• Now that java 8 has lambda expressions and functional interface. You should be able to do it in the similar fashion to your python code.

• def titleToNumber(self, s):
return reduce(lambda x,y: 26*x+y, [ord(c)-ord('A')+1 for c in list(s)])

don't need to remember 65

• So cool ! Thanks.

• Jesus, genius~

• So short! Genius!

• Something interesting to know:

Calculation in the for loop slows down the program. The one liner java solution above runs in 252ms.
If calculations are in the braces(208ms, a lot faster):
int result = 0;
for (int i = s.length() - 1, base = 1; i>=0; i--){
result += base * (s.charAt(i) - 64);
base *= 26;
}
return result;

• You think it's a big deal?

• Depends. In your case you go for shorter lines of code. What I commented is in terms of faster speed.
No intention to depreciate your good work. As I commented, "It's interesting to know".

• Actually, your point is not true. You can try to read the byte code of this. Or running it 10000 times locally to see whether they really have running time difference. Or run them on leetcode more than 10 times. The difference of running time is more depend on the workload of server.

• // Thanks for sharing. Here is my solution in C for reference.

``````  int titleToNumber(char* s) {

int num = 0;

for(; *s; s++)  num = num*26 + *s - 'A' + 1;

return num;

}``````

• This post is deleted!

• This post is deleted!

• C++ solution in one line(8ms):

``````int titleToNumber(string s) {
return accumulate(s.begin(), s.end(), 0, [](int num, char ch) { return num * 26 + ch - 'A' + 1; });
}``````

• My solution looks like the same but go from left to right.

``````    return s.length() == 0 ? 0 : (int)Math.pow(26,s.length()-1)*(s.charAt(0)-'A'+1)+titleToNumber(s.substring(1,s.length()));
``````

• great code! just curious. why did you use list(s) in the python solution.
this can work as well:
return reduce(lambda x, y : x * 26 + y, [ord(c) - 64 for c in s])

• @xcv58 Java 8 version of one line

``````public int titleToNumber(String s) {
return IntStream.range(0, s.length()).reduce(0, (result, i) -> result * 26 + (s.charAt(i) - 'A' + 1));
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.