개발 공부/알고리즘 문제 풀이 36

[LeetCode] 48. Rotate Image

문제 LeetCode 48. Rotate Image https://leetcode.com/problems/rotate-image/ 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 언어 자바스크립트(JavaScript) 접근 방법 가장 밖 테두리의 숫자들 배열을 먼저 바꾸며 점점 작은 테두리의 숫자들 배열을 바꾼다. 테두리의 숫자들 배열을 바꿀 때 왼쪽 위의 숫자부터 오른쪽 위의 숫자까지 차례로 배열을 바꾼다. 코드 /** * @param..

[LeetCode] 22. Generate Parentheses

문제 LeetCode 22. Generate Parentheses https://leetcode.com/problems/generate-parentheses/ Generate Parentheses - 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 언어 자바스크립트(JavaScript) 접근 방법 재귀적으로 푼다. (를 선택하는 경우와 )를 선택하는 경우로 나눈다. 괄호 갯수를 초과했거나 유효하지 않은 경우는 거른다. 코드 /** * @param {number} ..

[LeetCode] 19. Remove Nth Node From End of List

문제 LeetCode 19. Remove Nth Node From End of List https://leetcode.com/problems/remove-nth-node-from-end-of-list/ Remove Nth Node From End of List - 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 언어 자바스크립트(JavaScript) 접근 방법 모든 node의 값을 저장한다. 저장한 값을 이용하여 다시 ListNode를 만드는데, 특정 요소는 제외..

[LeetCode] 17. Letter Combinations of a Phone Number

문제 LeetCode 17. Letter Combinations of a Phone Number https://leetcode.com/problems/letter-combinations-of-a-phone-number/ Letter Combinations of a Phone 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 언어 자바스크립트(JavaScript) 접근 방법 배열의 총크기를 구한다. 나중에 누르는 숫자부터 규칙에 따라 모든 배열에의 요..

[LeetCode] 15. 3Sum

문제 LeetCode 15. 3Sum https://leetcode.com/problems/3sum/ 3Sum - 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 언어 자바스크립트(JavaScript) 접근 방법 중복 제거를 쉽게 하기 위해 오름차순으로 정렬한다. 첫 번째 숫자를 고르면 두 수의 합 문제가 된다. 처음 고르는 숫자 전에 같은 숫자가 있으면 중복이고, 두 번째 고르는 숫자 이후에 같은 숫자가 있으면 중복으로 간주한다. 코드 /** * @param {..

[LeetCode] 92. Reverse Linked List II

문제 92. Reverse Linked List II https://leetcode.com/problems/reverse-linked-list-ii/ Reverse Linked List 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 interview. leetcode.com 언어 자바스크립트(JavaScript) 접근 방법 먼저 루프를 돌면서 reverse 하기 전 숫자 배열과 reverse 하는 동안의 숫자 배열과 reverse가 끝난 후의 숫자 배열을 구한다. 각 배열을 이용하여 새로운 Li..

[LeetCode] 1. Two Sum

문제 1. Two Sum https://leetcode.com/problems/two-sum/ 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 언어 자바스크립트(JavaScript) 접근 방법 루프를 한 번만 돈다. 조건을 만족하는 숫자를 찾을 때 map을 이용한다. 코드 /** * @param {number[]} nums * @param {number} target * @return {number[]} */ var twoSum = functio..

[LeetCode] 12. Integer to Roman

문제 LeetCode 12. Integer to Roman https://leetcode.com/problems/integer-to-roman/ Integer to Roman - 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 언어 자바스크립트(JavaScript) 접근 방법 큰 수를 의미하는 문자부터 몇 개를 사용해야 하는지 결정한다. 문자가 몇 개인지는 그 수로 나눈 몫으로 구하며, 나머지 수는 나눈 나머지로 구한다. 코드 /** * @param {number..

LeetCode 11. Container With Most Water

문제 LeetCode 11. Container With Most Water https://leetcode.com/problems/container-with-most-water/ Container With Most Water - 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 언어 자바스크립트(JavaScript) 접근 방법 조합으로 풀면 복잡도가 O(n^2)로 안 좋다. Two pointer 알고리즘으로 풀어야 하고 더 간단하다. 코드 /** * @param {n..

[LeetCode] 8. String to Integer (atoi)

문제 LeetCode 8. String to Integer (atoi) https://leetcode.com/problems/string-to-integer-atoi/ String to Integer (atoi) - 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 언어 자바스크립트(JavaScript) 접근 방법 parseInt를 사용하여 숫자로 변환한다. praseInt 결과가 isNaN인 경우 거른다. 코드 /** * @param {string} s * @re..