[leetcode, JS] 2496. Maximum Value of a String in an Array

mxxn·2024년 6월 1일
0

leetcode

목록 보기
167/198

문제

문제 링크 : Maximum Value of a String in an Array

풀이

/**
 * @param {string[]} strs
 * @return {number}
 */
var maximumValue = function(strs) {
    let maxLeng = 0
    for(let str of strs) {
        const newStr = str.replace(/[^0-9]/gi,'')
        maxLeng = newStr !== str ? Math.max(maxLeng, str.length) : Math.max(maxLeng, +str)
    }
    return maxLeng
};
  1. for문을 돌며 문자가 섞여있는지 확인하여 maxLeng을 구하는 방식
  • Runtime 59 ms, Memory 49.15 MB

다른 풀이

/**
 * @param {string[]} strs
 * @return {number}
 */
var maximumValue = function(strs) {
    return Math.max(strs.reduce((ac, cur) => {
        let count = 0
        if(isNaN(+cur)){
            count = cur.length
        }else {
           count = +cur
        }
        if(ac < count) ac = count

        return ac
    }, 0))
};
  1. 위와 같은 방식의 풀이지만 reduce를 활용한 방식
  • Runtime 55 ms, Memory 48.59 MB
profile
내일도 글쓰기

0개의 댓글