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


  • 60

    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.


  • 33
    M

    you wanna this?

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

  • 0

    Yes, it works. Thanks!


  • 1
    K

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


  • 0
    Z

    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.


  • 0
    B

    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


  • 0
    Y

    So cool ! Thanks.


  • 0
    F

    Jesus, genius~


  • 0
    R

    So short! Genius!


  • 0
    S

    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;


  • 0

    You think it's a big deal?


  • 0
    S

    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".


  • 0

    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.


  • 4
    T

    // 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;
        
        }

  • 0
    W
    This post is deleted!

  • 0
    W
    This post is deleted!

  • 2
    W

    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; }); 
    }

  • 1
    T

    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()));
    

  • 0
    S

    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])


  • 1
    U

    @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));
        }
    

Log in to reply
 

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