문제 링크 Two Sum - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 유형 배열, Hash Map 문제 풀이 해당 문제는 숫자 배열과 target 값이 주어지고 배열 내 임의의 두 숫자를 더하면 target 값이 되는 두 숫자의 인덱스를 배열 형태로 출력하는 문제이다. 이 문제는 해시 테이블을 이용하면 O(n)의 시간복잡도로 문제를 해결할 수 있다. 먼저, 해시 테이블 Map을 생성하고, 배열을 순차적으로 접근한다. 만약 현재 숫자가 해시 테이블에 저..
문제 링크 Remove All Adjacent Duplicates In String - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com Remove All Adjacent Duplicates in String II - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next..
문제 링크 Longest Substring Without Repeating Characters - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 유형 투 포인터 (Two Pointer) 문제 풀이 해당 문제는 중복되는 문자가 없는 가장 긴 서브 스트링을 찾아 반환하는 문제이다. 단순히 생각해보면 이중 for문으로 substring을 차례대로 만들고, 중복되는 문자 체크와 최대 길이를 저장하며 문제를 해결할 수 있을 것이다. 그러나 이는 O(n^2)의 시간복..
문제 링크 Group Anagrams - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 유형 배열, 문자열 문제 풀이 해당 문제는 여러 문자열이 담긴 배열이 입력으로 주어지고, anagram 단어끼리 묶어서 반환하는 문제이다. Anagram은 단어의 순서를 바꿔서 다른 단어를 찾는 놀이를 말한다. anagram 단어를 묶기 위해 JavaScript의 Map을 사용해 키를 기준으로 anagram 단어를 그룹핑한다. 이때, 기준이 되는 키는 각 문자열을 정렬시..
문제 링크 Add Strings - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 유형 문자열 (String) 문제 풀이 이 문제는 숫자 2개가 string 타입으로 주어지고, 숫자 2개를 더한 연산 결과를 string 타입으로 반환하는 문제이다. 초등학교 때 두 수의 덧셈할 때 일의 자리부터 더하면서 받아올림을 위의 자리로 올려주는 방법을 배웠다. 이 방법을 이용하면 된다. 받아올림(carry)은 각 자리의 합을 10으로 나눈 것이다. 해당 자리에 들어갈..
문제 링크 Search a 2D Matrix - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 유형 2차원 배열 문제 풀이 이 문제는 정렬된 2차원 m x n 배열내에 target이 있으면 true, 없으면 false를 반환하는 문제이다. 정렬된 1차원 배열을 먼저 생각해보자. 정렬된 1차원 배열에서 어떤 값을 탐색할 때 이진 탐색을 이용하면 O(log n)의 시간복잡도로 문제 해결이 가능하다. 문제에서 주어지는 2차원 매트릭스는 각 행, 열의 숫자들이 모..
문제 링크 Rotate Image - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 유형 2차원 배열 문제 풀이 이 문제는 이미지를 나타내는 2차원 행렬이 2차원 배열로 주어지고, 이를 시계방향으로 90도 돌렸을 때의 배열을 반환하는 문제이다. 처음 n x n 행렬의 인덱스를 oldX, oldY라 하고 90도 돌린 행렬의 인덱스를 newX, newY라고 할 때, 인덱스 간의 관계는 다음과 같다. newX = -oldY + (n - 1) newY = oldX..
문제 링크 Find the Duplicate Number - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 유형 배열 문제 풀이 이 문제는 배열 내 중복되는 숫자를 찾아 리턴하는 문제이다. 이 문제는 배열 내의 요소를 인덱스로 보는 방법을 통해 O(n)의 시간복잡도를 갖지만 공간복잡도가 다른 두 방법으로 접근할 수 있다. 새로운 배열에 숫자 존재 여부를 표시하기 nums 배열의 길이의 배열을 만들고, 배열을 순차적으로 접근한다. 현재 숫자에 해당하는 인덱스..