From f862ebf0c70c37c62f83b5ad50b441c830f5850f Mon Sep 17 00:00:00 2001 From: Camille0512 Date: Sun, 27 Feb 2022 00:41:18 +0800 Subject: [PATCH] modified offer05. Add two python versions. --- problems/剑指Offer05.替换空格.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/剑指Offer05.替换空格.md b/problems/剑指Offer05.替换空格.md index 530545fb..59f74a18 100644 --- a/problems/剑指Offer05.替换空格.md +++ b/problems/剑指Offer05.替换空格.md @@ -260,8 +260,24 @@ class Solution: ``` +```python +class Solution: + def replaceSpace(self, s: str) -> str: + # method 1 - Very rude + return "%20".join(s.split(" ")) + + # method 2 - Reverse the s when counting in for loop, then update from the end. + n = len(s) + for e, i in enumerate(s[::-1]): + print(i, e) + if i == " ": + s = s[: n - (e + 1)] + "%20" + s[n - e:] + print("") + return s +``` javaScript: + ```js /** * @param {string} s