美文网首页
2020-08-06 字节真题 244. 删除字符

2020-08-06 字节真题 244. 删除字符

作者: 苦庭 | 来源:发表于2020-08-06 17:20 被阅读0次

    https://www.lintcode.com/problem/delete-char/description

    My solution / AC

    /**
     * @param str: the string
     * @param k: the length
     * @return: the substring with  the smallest lexicographic order
     */
    const deleteChar = function (str, k) {
        // Write your code here.
        let res = "";
        let start = 0;
        while(res.length<k) {
            let marker = start;
            let c = str[start];
            for(let i=start+1; i<=str.length-(k-res.length); i++) {
                if(str[i]<c) {
                    c = str[i];
                    marker = i;
                }
            }
            res += c;
            start = marker+1;
        }
        return res;
    }
    

    双指针
    在后续字符串中找最小值,marker用来标记下一个查找开始的位置(=marker+1)

    相关文章

      网友评论

          本文标题:2020-08-06 字节真题 244. 删除字符

          本文链接:https://www.haomeiwen.com/subject/ixltdktx.html