close
71. Simplify Path

Given an absolute path for a file (Unix-style), simplify it.

For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"

click to show corner cases.

Corner Cases:

 

  • Did you consider the case where path = "/../"?
    In this case, you should return "/".
  • Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
    In this case, you should ignore redundant slashes and return "/home/foo"
 
這題我是用 split 和 join 來做,不過看起來有點慢,但是大致想法是對的
因為程式碼很短執行時間也很短,所以只要寫法有點繞就會慢,附上別人寫的比較精簡的寫法
Success:
62 ms, beats 20.17 % of python submissions 
 
class Solution(object):
    def simplifyPath(self, path):
        """
        :type path: str
        :rtype: str
        """
        result = []
        stack = path.split("/")
        for index, item in enumerate(stack):
            if not item or item == ".": continue
            if item == "..":
                if result: result.pop()
                continue
            result.append((stack[index]))

        result = "/" + "/".join(result)
        return result
較快的寫法
42 ms, beats 72.88 % of python submissions 
 
class Solution(object):
    def simplifyPath(self, path):
        places = [p for p in path.split("/") if p!="." and p!=""]
        stack = []
        for p in places:
            if p == "..":
                if len(stack) > 0:
                    stack.pop()
            else:
                stack.append(p)
        return "/" + "/".join(stack)

402. Remove K Digits

Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.

Note:

  • The length of num is less than 10002 and will be ≥ k.
  • The given num does not contain any leading zero.

 

Example 1:

Input: num = "1432219", k = 3
Output: "1219"
Explanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.

 

Example 2:

Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.

 

Example 3:

Input: num = "10", k = 2
Output: "0"
Explanation: Remove all the digits from the number and it is left with nothing which is 0.

 

這題一開始想得太複雜,想說要找出min 並包含之後的數值
最後看了大家的做法,是用剔除會讓值變大的方式來做 (與之前值比較,如果之前值較大就pop 踢出)

注意一下因為可能會
"9" 1 -> "0" 這種case,所以要在最後加上 out[:-k or None]
out[:-k] 來解決k 可能沒歸0的可能,一般來說是走 out[:None] = out[:] 也就是全印的情況

然後跟最後可能沒東西的狀況,所以還要
  or "0"
 
Success:
65 ms, beats 57.54 % of python submissions 
class Solution(object):
    def removeKdigits(self, num, k):
        """
        :type num: str
        :type k: int
        :rtype: str
        """
        out = []
        for n in num:
            while k and out and n < out[-1]:
                    out.pop()
                    k -= 1
            out.append(n)
        return "".join(out[:-k or None]).lstrip("0") or "0"

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 angledark0123 的頭像
    angledark0123

    CONY的世界

    angledark0123 發表在 痞客邦 留言(1) 人氣()