Python Simple One-Pass


  • 0
    A
    class Solution(object):
        def removeKdigits(self, num, k):
            """
            :type num: str
            :type k: int
            :rtype: str
            """
            if k == len(num):
                return "0"
            if k == 0:
                return num
            n, i, digits, res = len(num), -1, 0, ""
            while digits < n - k:
                start = i + 1
                cand = 10
                for pos in range(start, k+digits+1):
                    if num[pos] == "0":
                        i = pos
                        cand = 0
                        # digits += 1
                        break
                    else:
                        if int(num[pos]) < cand:
                            cand = int(num[pos])
                            i = pos
                res += str(cand) 
                digits += 1
            return str(int(res)) 
    

Log in to reply
 

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