[LeetCode] 102. Binary Tree Level Order Traversal
·
알고리즘 문제풀이/LeetCode
문제 트리 문제이다. binary tree의 root 노드가 주어질 때, level(층)을 순서대로 각 노드의 value들을 2차원 배열로 저장하여 리턴하는 문제이다. https://leetcode.com/problems/binary-tree-level-order-traversal/ Binary Tree Level Order Traversal - 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 풀이 각 층에 있는 노드의 value들을 확인하는 문제이다. 확인하는 ..
[LeetCode] 100. Same Tree
·
알고리즘 문제풀이/LeetCode
문제 기본 트리 문제이다. TreeNode 클래스가 주어지며, 2개의 이진트리 p, q가 주어질 때 같은 트리인지 아닌지 확인하는 문제이다. https://leetcode.com/problems/same-tree/ Same Tree - 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 풀이 p와 q의 노드를 저장할 Queue를 생성한다. (pQueue, qQueue) p와 q를 각각 Queue에 저장한다. pQueue가 비어있을 때까지 다음을 반복한다. 1) pQu..
[LeetCode] 70. Climbing Stairs
·
알고리즘 문제풀이/LeetCode
문제 DP 문제이다. n개의 계단이 있을 때, 한 번에 1개 또는 2개의 계단을 올라갈 수 있다. 꼭대기에 오르기까지 계단을 오르는 방법이 몇 가지 인가? https://leetcode.com/problems/climbing-stairs/ Climbing Stairs - 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 풀이 n번 째 계단에 도달하기 위해서는 1) n-1번 째 계단에서 1개의 계단을 오르기 2) n-2번 째 계단에서 2개의 계단을 오르기 위 2가지의..
[LeetCode] 322. Coin Change
·
알고리즘 문제풀이/LeetCode
문제 DP 문제이다. 여러 동전의 가격 coins가 주어질 때, (각 동전의 가격은 다름) amount 가격을 만들기 위해 사용하는 동전의 최소 개수를 구하라. https://leetcode.com/problems/coin-change/ Coin Change - 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 풀이 예시) amount : 만들어야 하는 가격 사용할 수 있는 동전들 : 1원, 2원, 5원 a원 동전을 하나 사용한다고 할 때, amount를 만들기 위..
[LeetCode] 238. Product of Array Except Self
·
알고리즘 문제풀이/LeetCode
문제 1차원 배열 nums가 주어졌을 때, 다음을 만족하는 배열 answer를 리턴하라. answer[i]는 nums[i]를 제외한 요소의 모든 곱이다. https://leetcode.com/problems/product-of-array-except-self/ Product of Array Except Self - 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 풀이 나누기 연산을 사용하지 않는 조건이기 때문에, 다른 방법을 생각해야 한다. 각 answer[i] ..
[LeetCode] 217. Contains Duplicate
·
알고리즘 문제풀이/LeetCode
문제 1차원 배열 nums가 주어졌을 때, 중복되는 값이 들어있으면 true, 모두 다른 값인 경우 false를 반환한다. https://leetcode.com/problems/contains-duplicate/ Contains Duplicate - 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*n)만큼 걸리게 되어 Time Limit Exceeded에 걸리게 된다. -> 따라서, 배열 nums를 정렬한 후 순차적으로 ..
[LeetCode] 121. Best Time to Buy and Sell Stock
·
알고리즘 문제풀이/LeetCode
문제 1차원 배열 prices가 주어졌을 때, prices[i]는 i번째 날의 물건의 가격이다. 어떤 날에 물건을 구입하여 다른 날에 물건을 되판다고 할 때, 최대의 이익을 구하는 문제이다. https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ Best Time to Buy and Sell Stock - 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 풀이 위 그림과 같이 가격이 각 [7, 1, 5, 3,..
[LeetCode] 1. Two Sum
·
알고리즘 문제풀이/LeetCode
문제 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 풀이 이중 for문을 활용하여 문제를 해결한다. i : 0 ~ num.length-1 j : i+1 ~ num.length 모든 범위를 확인하며 nums[i] + nums[j] 가 target이 되는 경우 답을 answer에 저장하고 리턴한다. 코드 class Solution { public int[] twoSum(int[]..