leetCode algorithm second day

insert image description here

Brush up on algorithm questions and improve your coding skills.

convert integer to roman numeral

Leetcode link: https://leetcode.cn/problems/integer-to-roman/

Problem-solving idea: We also define several special rules in the map of Roman numerals and characters, so that all the rules become from left to right, the big one is on the left, and the small one is on the right. Match from more to less each time, as long as the number is greater than the corresponding value, match the corresponding string, and then add up from left to right.

In addition, Roman numerals still have a lot of numbers. This question only requires numbers within 3000.

var intToRoman = function (num) {
    
    
  const romanNumeralMap = {
    
    
    M: 1000,
    CM: 900,
    D: 500,
    CD: 400,
    C: 100,
    XC: 90,
    L: 50,
    XL: 40,
    X: 10,
    IX: 9,
    V: 5,
    IV: 4,
    I: 1,
  };
  let result = "";

  while (num > 0) {
    
    
    for (let key in romanNumeralMap) {
    
    
      if (num >= romanNumeralMap[key]) {
    
    
        result += key;
        console.log(result);
        num -= romanNumeralMap[key];
        break;
      }
    }
  }
  return result;
};

Convert Roman Numerals to Integers

Leetcode link: https://leetcode.cn/problems/roman-to-integer/

Problem-solving idea: We also define several special rules in the map of Roman numerals and characters, so that all the rules become from left to right, the big one is on the left, and the small one is on the right. Match from more to less each time, as long as the characters match the string and the index is 0, the corresponding number will be added, and the remaining strings will continue to match from large to small.

/**
 * @param {string} s
 * @return {number}
 */
var romanToInt = function (s) {
    
    
  const romanNumeralMap = {
    
    
    M: 1000,
    CM: 900,
    D: 500,
    CD: 400,
    C: 100,
    XC: 90,
    L: 50,
    XL: 40,
    X: 10,
    IX: 9,
    V: 5,
    IV: 4,
    I: 1,
  };

  let num = 0;

  while (s.length) {
    
    
    for (let key in romanNumeralMap) {
    
    
      const index = s.indexOf(key);
      if (index === 0) {
    
    
        s = s.slice(key.length);
        num += romanNumeralMap[key];
        break;
      }
    }
  }

  return num;
};

Write a function to find the longest common prefix in an array of strings

Leetcode link: https://leetcode.cn/problems/longest-common-prefix/

Problem-solving ideas: The longest common prefix is ​​at most the shortest string in the array, so we first find the shortest string, and then reduce its length in turn to determine whether it is the prefix of other element strings in the array. If it is not satisfied at the end when the shortest string length is reduced to 1, there is no common prefix.

/**
 * @param {string[]} strs
 * @return {string}
 */
var longestCommonPrefix = function (strs) {
    
    
  let minStr = "";
  strs.forEach((item) => {
    
    
    if (!minStr || item.length < minStr.length) {
    
    
      minStr = item;
    }
  });

  while (minStr) {
    
    
    const length = minStr.length;
    for (const value of strs) {
    
    
      console.log(value);
      if (value.indexOf(minStr) !== 0) {
    
    
        minStr = minStr.slice(0, minStr.length - 1) || "";
        break;
      }
    }

    if (minStr.length < length) {
    
    
      continue;
    } else {
    
    
      return minStr;
    }
  }
  return "";
};

letter combination of phone number

Leetcode link: https://leetcode.cn/problems/letter-combinations-of-a-phone-number/

Problem-solving idea: backtracking in a loop. The length of digits determines how many times we want to loop. The number of loops each time is determined by the length of the array corresponding to the number. Every loop, it is judged whether the length of the remaining digits is 1. If it is 1, it is the last loop and exits recursion. , otherwise continue recursively.

/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function (digits) {
    
    

  let NumberMap = {
    
    
    2: ["a", "b", "c"],
    3: ["d", "e", "f"],
    4: ["g", "h", "i"],
    5: ["j", "k", "l"],
    6: ["m", "n", "o"],
    7: ["p", "q", "r", "s"],
    8: ["t", "u", "v"],
    9: ["w", "x", "y", "z"],
  };

  let result = [];

  function numberToCharCode(ReminDigits = digits, str = "") {
    
    
    console.log(ReminDigits, str);
    if (ReminDigits.length === 1) {
    
    
      for (const value of NumberMap[ReminDigits]) {
    
    
        let newStr = str + value;
        result.push(newStr);
      }
    } else if(ReminDigits.length > 1) {
    
    
      const newReminDigits = ReminDigits.slice(1);
      for (const value of NumberMap[ReminDigits[0]]) {
    
    
        let newStr = str + value;
        numberToCharCode(newReminDigits, newStr);
      }
    }
  }

  numberToCharCode();
  return result;
};

Guess you like

Origin blog.csdn.net/glorydx/article/details/130163604