KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • recursion
  • Level 1
  • tree
  • Level 0
  • programmers
  • 그래프
  • string
  • 다이나믹 프로그래밍
  • Level 2
  • 제코베 JS 100제
  • js
  • 백준
  • C++
  • 문자열
  • 수학
  • codeup
  • Level1
  • C
  • LeetCode
  • 정렬

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Array / 228번 / Summary Ranges / JS

2023. 6. 17. 13:50

< 문제 바로가기 >

 

Summary Ranges - LeetCode

Can you solve this real interview question? Summary Ranges - You are given a sorted unique integer array nums. A range [a,b] is the set of all integers from a to b (inclusive). Return the smallest sorted list of ranges that cover all the numbers in the arr

leetcode.com

 

< 문제 간단설명 >

연속수를 이루는 모든 범위를 배열에 담아서 반환한다.

 

/**
 * @param {number[]} nums
 * @return {string[]}
 */
var summaryRanges = function (nums) {
  let result = [];

  let i = 0;
  while (i < nums.length) {
    let start = nums[i];

    // 연속수가 아닐때까지 인덱스 증가
    while (nums[i + 1] === nums[i] + 1) {
      i += 1;
    }

    let end = nums[i];

    // 시작수와 끝수가 같다면 하나만 결과에 추가
    result.push(start === end ? `${start}` : `${start}->${end}`);
    i += 1;
  }

  return result;
};
저작자표시 (새창열림)

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Tree / 530번 / Minimum Absolute Difference in BST / JS  (0) 2023.06.17
LeetCode / Matrix / 2352번 / Equal Row and Column Pairs / JS  (0) 2023.06.17
LeetCode / Design / 155번 / Min Stack / JS  (0) 2023.05.14
LeetCode / Design / 232번 / Implement Queue using Stacks / JS  (0) 2023.05.14
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Tree / 530번 / Minimum Absolute Difference in BST / JS
    • LeetCode / Matrix / 2352번 / Equal Row and Column Pairs / JS
    • LeetCode / Design / 155번 / Min Stack / JS
    • LeetCode / Design / 232번 / Implement Queue using Stacks / JS
    KimMinJun
    KimMinJun

    티스토리툴바