Fork me on GitHub
打赏

剑指 Offer 05. 替换空格

题目

力扣-剑指 Offer 05. 替换空格

请实现一个函数,把字符串 s 中的每个空格替换成"%20"。

示例 1:
输入:s = "We are happy."
输出:"We%20are%20happy."
 
限制:
0 <= s 的长度 <= 10000

题解

该题难度为简单。

解法一:使用strings.Replace

//Go
func replaceSpace(s string) string {
    return strings.Replace(s, " ", "%20", -1)
}

解法二:遍历添加

//Go
func replaceSpace(s string) string {
    ans := ""
    for _,v := range s{
        if v == ' '{
            ans = ans + "%20"
        } else {
            ans = ans + string(v)
        }
    }
    return ans
}

leetcode-执行:

执行用时:
0 ms, 在所有 Go 提交中击败了100.00%的用户
内存消耗:
3.4 MB, 在所有 Go 提交中击败了16.95%的用户

牛客网执行:

运行时间:2ms
超过100.00%用Go提交的代码
占用内存:956KB
超过23.81%用Go提交的代码
posted @ 2021-03-03 23:21  Zoctopus_Zhang  阅读(46)  评论(0编辑  收藏  举报
// function btn_donateClick() { var DivPopup = document.getElementById('Div_popup'); var DivMasklayer = document.getElementById('div_masklayer'); DivMasklayer.style.display = 'block'; DivPopup.style.display = 'block'; var h = Div_popup.clientHeight; with (Div_popup.style) { marginTop = -h / 2 + 'px'; } } function MasklayerClick() { var masklayer = document.getElementById('div_masklayer'); var divImg = document.getElementById("Div_popup"); masklayer.style.display = "none"; divImg.style.display = "none"; } setTimeout( function () { document.getElementById('div_masklayer').onclick = MasklayerClick; document.getElementById('btn_donate').onclick = btn_donateClick; var a_gzw = document.getElementById("guanzhuwo"); a_gzw.href = "javascript:void(0);"; $("#guanzhuwo").attr("onclick","follow('33513f9f-ba13-e011-ac81-842b2b196315');"); }, 900);