Home > other >  Python code to be executed multiple times the result is different? Why
Python code to be executed multiple times the result is different? Why

Time:10-15

This problem is to seek the longest prefix
 
Def longestCommonPrefix (STRS) :
STRS=list (set (STRS))
Lenth=len (STRS)
If the STRS!=[] :
Pub_str=""
If STRS [0]=="" :
Return ""
Elif lenth==1 and STRS [0]!="" :
Return STRS [0]
For I in STRS [0] :
Pub_str +=I
Print (pub_str)
For j in (1, lenth - 1) :
If len (pub_str) & gt; Len (STRS) [j] or not STRS [j]. J startswith (pub_str) :
Return pub_str] [: - 1
The else:
The continue
The else:
Return ""
The STRS=[" aa ", "a"]
The index=longestCommonPrefix (STRS)
Print (index)

Why the above code execution result different for many times, from time to time is to None for a, is in don't understand

CodePudding user response:

Debug the view variable flow can be clear at a glance,
  • Related