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

2020-08-06  本文已影响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)

上一篇下一篇

猜你喜欢

热点阅读