1704. 判断字符串的两半是否相似-easy-模拟

思路:全部转为小写字母以后统计前半段和后半段小写元音数量即可。

  1. /**
  2. * @param {string} s
  3. * @return {boolean}
  4. */
  5. var halvesAreAlike = function(s) {
  6. const n = s.length;
  7. s = s.toLowerCase();
  8. let result1 = 0;
  9. let result2 = 0;
  10. for (let i = 0; i < n; i++) {
  11. if (['a', 'e', 'i', 'o', 'u'].includes(s[i])) {
  12. if (i < n / 2) {
  13. result1++;
  14. }else{
  15. result2++;
  16. }
  17. }
  18. }
  19. return result1 === result2;
  20. };

1705. 吃苹果的最大数目-medium-优先队列

思路:使用优先队列保存[过期时间,苹果数目],对于第i天,从队列中出队已过期的记录或没有苹果的记录,如果第i天仍有苹果,答案加一,苹果数减一,再次入队,一直循环到队列为空。
JavaScript的优先队列实现可参考:
优先队列-priority queue

var eatenApples = function(apples, days) {
  const priorityQueue = new PriorityQueue((a, b) => a[0] === b[0] ? a[1] < b[1] : a[0] < b[0]);
  const n = apples.length;
  let result = 0;
  for (let i = 0; i < n; i++) {
    priorityQueue.push([i + days[i], apples[i]]);
    while (!priorityQueue.empty() && (priorityQueue.top()[0] <= i || !priorityQueue.top()[1])) {
      priorityQueue.pop();
    }
    if (!priorityQueue.empty() && !!priorityQueue.top()[1]) {
      result++;
      let [day, apple] = priorityQueue.top();
      priorityQueue.pop();
      apple--;
      priorityQueue.push([day, apple]);
    }
  }
  for (let i = n; !priorityQueue.empty(); i++) {
    while (!priorityQueue.empty() && (priorityQueue.top()[0] <= i || !priorityQueue.top()[1] )) {
      priorityQueue.pop();
    }
    if (!priorityQueue.empty() && !!priorityQueue.top()[1]) {
      result++;
      let [day, apple] = priorityQueue.top();
      priorityQueue.pop();
      apple--;
      priorityQueue.push([day, apple]);
    }
  }
  return result;
};

1706. 球会落何处-medium-模拟

思路:模拟球运动的轨迹即可,需要注意JavaScript函数的参数是按值传递,即传递对象的引用的副本。并不像其他语言类似C是传递进去一个引用,另外需要注意浅拷贝问题。


/**
 * @param {number[][]} grid
 * @return {number[]}
 */
var findBall = function(grid) {
  const m = grid.length;
  const n = grid[0].length;
  const result = new Array(n);
  const getNext = (last, now, val, next) => {
    //next的赋值利用了传递对象的引用的副本这一特点。
    if (last[1] === now[1]) {
      if (last[0] < now[0]) {
        if (val === -1) {
          return false;
        } else {
          next[0] = now[0];
          next[1] = now[1] + 1;
          return true;
        }
      } else {
        if (val === 1) {
          return false;
        } else {
          next[0] = now[0];
          next[1] = now[1] + 1;
          return true;
        }
      }
    } else {
      if (val === 1) {
        next[1] = now[1];
        next[0] = now[0] + 1;
        return true;
      } else {
        next[1] = now[1];
        next[0] = now[0] - 1;
        return true;
      }
    }
  };
  for (let i = 0; i < n; i++) {
    let now = { 0: i, 1: 0 };
    let last = { 0: i, 1: -1 };
    const next = {};
    while (true) {
      if (i === 0) {
        console.log(last, now);
      }
      const hasNext = getNext(last, now, grid[now[1]][now[0]], next);

      if (i === 0) {
        console.log(next);
      }
      if (!hasNext) {
        result[i] = -1;
        break;
      }
      if (next[0] < 0 || next[0] >= n) {
        result[i] = -1;
        break;
      }
      //对象的赋值需要避免浅拷贝
      last = { ...now };
      now = { ...next };

      if (next[1] === m) {
        result[i] = next[0];
        break;
      }
    }
  }
  return result;
};