代码随想录算法训练营第八天 | 344.反转字符串、541.反转字符串II、54.替换数字、151.翻转字符串里的单词、55.右旋转字符串

99 阅读2分钟

344.反转字符串

541.反转字符串II

54.替换数字

151.翻转字符串里的单词

55.右旋转字符串

344. 反转字符串

p.s. 可以使用 ^=(按位异或)的方法交换顺序,但在js中这样会更麻烦,字母直接调用异或的结果是0,需转换。

var reverseString = function(s) {
    if (s.length === 1) return s;

    let i=0, j=s.length-1;
    while (i<j) {
        let tmp = s[i];
        s[i] = s[j];
        s[j] = tmp;
        i++;
        j--;
    }
    
    return s;
};

541. 反转字符串II

  • 我的思路:纯模拟
  • better:对交换条件进行合并,优化循环条件
var reverseStr = function(s, k) {
    if (s.length === 1) return s;

    const arrS = Array.from(s);
    let cnt = 0;
    let i=0, j=0;

    const swapArrS = (start, end) => {
        let s = start, e = end;
        while (s < e) {
            const tmp = arrS[s];
            arrS[s] = arrS[e];
            arrS[e] = tmp;
            s++;
            e--;
        }
    }

    for ( ;j < arrS.length; ) {
        if (cnt === 2 * k) {
            swapArrS(i, i+k-1);
            cnt = 0;
            i = j;
        } else {
            cnt++;
            j++;
        }
    }

    if (arrS.length - i - 1 < k ) {
        swapArrS(i, arrS.length - 1)
    }

    if (arrS.length - i - 1 >= k) {
        swapArrS(i, i+k-1);
    }

    return arrS.join('');
};

// better
for (let i=0; i<arrS.length; i+=(2*k)) {
    if (i + k <= arrS.length) {
        swapArrS(i, i+k-1);
    } else {
        swapArrS(i, arrS.length-1);
    }
}

54. 替换数字

p.s. 可以扩容后后序填充来做,但在JS中不可行:JS中的字符串是不可变的,i.e. 不可直接修改常量字符串的内容 e.g. a='123'; a[0]='6'再打印a会发现没有变化

const readline = require('readline');
 
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
 
rl.on('line', (input) => {
    const arr = Array.from(input);
    for (let i=0; i<arr.length; i++) {
        if (!isNaN(+arr[i])) {
            arr[i] = 'number';
        }
    }
    console.log(arr.join(''));
})

151. 翻转字符串里的单词

  • 陷阱:a[-1]的结果是undefined:)
var reverseWords = function(s) {
    const as = Array.from(s);
    const res = [];

    for (let i=as.length-1, j=as.length-1; i>=0; ) {
        while (as[i] === ' ') {
            i--;
            j--;
        }
        if (i < 0)  break;

        while (as[i] !== ' ' && !!as[i])   {
            i--;
        }

        if (res.length !== 0) res.push(' ');
        res.push(...as.slice(i+1, j+1));
        j = i;
    }
    
    return res.join('');
};

55. 右旋转字符串

const readline = require('readline');
 
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

const lines = [];
rl.on('line', function(line) {
    lines.push(line);
    if (lines.length === 2){
        const k = parseInt(lines[0], 10);
        const s = lines[1];
        
        // 偷懒做法1
        const as = Array.from(s);
        const n = as.length;
        ns = [...as.slice(n-k), ...as.slice(0, n-k)];
        console.log(ns.join(''));
        
        // 巧妙做法2: 多次反转!
        const as = Array.from(s);
        const n = as.length;
        
        const reverse = (arr, start, end) => {
            let i=start, j=end;
            while(i<j) {
                let tmp = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp;
                i++;
                j--;
            }
        }
        
        reverse(as, 0, n-1);
        reverse(as, 0, k-1);
        reverse(as, k, n-1);
        console.log(as.join(''));
    }
});