[LeetCode] 3798. Largest Even Number

Chobby·2026년 1월 26일

LeetCode

목록 보기
964/993

😎풀이

  1. s를 스택 구조로 활용
  2. 제일 뒷 자리가 2가 아니라면, 홀수이므로 2가 나올 때까지 제거
  3. 가능한 가장 큰 짝수 배열을 문자열로 변환하여 반환
function largestEven(s: string): string {
    const stack = [...s]
    while(stack.length && stack[stack.length - 1] !== '2') {
        stack.pop()
    }
    return stack.join('')
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글