My AC python code


  • 1
    S

    first time to share my idea in leetcode:

    1. split into arrays

    2. delete the tail arrays of 0

    3. compare the first n array, n is min array length

    4. if the first n array is equal, then the one with more array is the bigger version.

      class Solution:

      @param a, a string

      @param b, a string

      @return a boolean

      def compareVersion(self, version1, version2):
      def removeTailZero(l):
      for i in xrange(len(l)-1,-1,-1):
      if int(l[i])!=0:
      break
      del l[i]
      v1,v2 = version1.split("."),version2.split(".")
      removeTailZero(v1)
      removeTailZero(v2)
      for i in xrange(min(len(v1),len(v2))):
      if int(v1[i])>int(v2[i]):
      return 1
      elif int(v2[i])>int(v1[i]):
      return -1
      if len(v1)>len(v2): return 1
      elif len(v1)<len(v2): return -1
      else: return 0

    Hope someone could make it shorter and more beautiful:)


  • 0
    C

    check out my post


Log in to reply
 

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