/javascript

Longest Common Prefix

TL;DR

/**
 * @param {string[]} strs
 * @return {string}
 */
var longestCommonPrefix = function(strs) {
  if (strs.length === 0) return '';
  let answer = strs.reduce((prev, current) => {
    commonPrefix = [];
    if (prev === '' || current === '') return '';
    prev = prev.split('');
    for (i = 0; i < prev.length; i++) {
      if (prev[i] === current[i]) {
        commonPrefix.push(prev[i]);
      } else {
        return commonPrefix.join('');
      }
    }
    return commonPrefix.join('');
  });

  return answer;
};

可能的方法是使用 reduce 函数。 运行时间是运行时间: 64 ms

Subscribe to Necmettin Karakaya

Get the latest posts delivered right to your inbox

Necmettin Karakaya

Necmettin Karakaya

Full Stack Developer