Why python output dif from my local output py 2.7


  • 0
    R
    class Solution(object):
        def reverseWords(self, s):
            """
            :type s: a list of 1 length strings (List[str])
            :rtype: nothing
            """
            s = ' '.join([word[::-1] for word in (''.join(i for i in s))[::-1].split()])
    

    and also why change input type from str to list of chars, which make trouble form coding : )

    And my code is not pythonic, anyone have list comprehension solution

    class Solution(object):
        def reverseWords(self, s):
            """
            :type s: a list of 1 length strings (List[str])
            :rtype: nothing
            """
            #if len(s) == 0:
            #    return '';
            s.reverse()
            start,n= 0, len(s)
            for i in range(len(s)):
                if s[i]==' ':
                    s[start:i]=s[start:i][::-1]
                    start = i+1
            s[start:n]=s[start:n][::-1]

  • 0
    R

    it got WA on case "a b", which local ouput "b a", which is correct, but OJ output "a b"


  • 0

    You have to modify the given list, referenced by s, not make s reference something else. That's only your local variable, the caller can't even see it.


  • 0
    R

    ok, I know. python str is immutable, which would make parameter be list of chars, same as java. But my python code is not pythonic, do you have list comprehension solution: )


  • 0

    I don't see a good way to use list comprehension, but maybe you'll like this solution of mine.


Log in to reply
 

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