Python question, Beginner

  • 0

    Hi, I'm a beginner.... I have tested this code in python. It works with the input that Leetcode says incorrect. I cannot figure it out. Anyone can help?



    def reverseWords(self, s):
    	if s is None:
    		return False
    	idx = [i for i, value in enumerate(b) if value is ' ']
    	wor = {}
    	idx = [-1] + idx + [len(b)]
    	for n in range(len(idx)-1):
    		wor[n] = ''.join(b[idx[n]+1:idx[n+1]])
    	r_wor = ''
    	for n in range(len(wor)-1,-1, -1):
    		r_wor_temp = wor[n]
    		if n == 0:
    			r_wor += r_wor_temp
    			r_wor += r_wor_temp + ' '
    	s= r_wor


  • 0

    because you modify the pointer of s

    s = r_wor

    this will not modify s out side the scope of your function

    you have to do

    for i in range(len(s)):
         s[i] = r_wor[i]

    check python parameter passing style wiki, and you know why

Log in to reply

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