[leetcode, JS] 1805. Number of Different Integers in a String

mxxn·2023년 11월 14일
0

leetcode

목록 보기
121/198

문제

문제 링크 : Number of Different Integers in a String

풀이

/**
 * @param {string} word
 * @return {number}
 */
var numDifferentIntegers = function(word) {
    return new Set(word.replace( /[\D]/gim, ' ').split(' ').filter(el => el !== '').map(el => el.replace(/\b0+/, '') )).size

};
  1. 문자열 word에서 숫자가 아닌 것들을 ' '로 변환하고
  2. ' '로 split한 후에
  3. 빈 값이 아닌 것들로 filter 하여
  4. map을 돌려 0으로 시작하는 element들은 0을 제거하고
  5. set으로 중복 제거 후 size를 return
  • Runtime 50ms, Memory 42.54MB

다른 풀이

/**
 * @param {string} word
 * @return {number}
 */
var numDifferentIntegers = function(word) {
    return new Set(word.split(/[^\d]+/).filter(el => el !== '').map(el => el.replace(/\b0+/, '') )).size

};
  1. 먼저 풀이와 비슷하지만 첫 단계에서 replace와 split을 나눈것을 split으로 통합
  • Runtime 47ms, Memory 42.04MB
profile
내일도 글쓰기

0개의 댓글