본문 바로가기

Algorithm/Leetcode16

[Leetcode(릿코드)] 1. Two Sum (Easy) 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 문제 배열과 타겟이 주어진다 배열의 2개 요소의 합이 타겟과 같으면, 그 요소들의 인덱스를 출력하는 문제이다 해결 배열을 한번 돌면서 target에서 해당 요소를 뺴고 s에 저장한다 그리고 배열의 현재 인덱스 다음부터 한번 더 돌면서 s랑 같으면 결과에 넣어서 출력한다 public static int[] twoSum(int[] nums, int.. 2020. 11. 7.
[Leetcode(릿코드)] 454. 4SUM 2 (Medium) leetcode.com/problems/4sum-ii/ 4Sum 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 문제 배열 4개가 주어진다 각 배열의 요소에서 1개씩 뽑아서 모두 더한다 그리고 0이 나오는 경우의 개수를 출력한다 예를 들면, A = [1, 2] B = [-2,-1] C = [-1, 2] D = [0, 2] 이면, The two tuples are: 1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + .. 2020. 11. 3.
[Leetcode(릿코드)] 378. Kth Smallest Element in a Sorted Matrix (Medium) Binarysearch 문제 leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ Kth Smallest Element in a Sorted 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차원 배열이 주어진다 각 배열은 정렬이 되어있다 예를 들면, matrix = [[1, 5, 9], [10, 11, 13], [12, 13, 15] ], k = 8 이 주어지면, 1 5 9 10 1.. 2020. 11. 3.
[Leetcode(릿코드)] 107. Binary Tree Level Order Traversal II (Easy) BFS 문제 leetcode.com/problems/binary-tree-level-order-traversal-ii/ Binary Tree Level Order Traversal 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 문제 이진트리를 BFS로 탐색한 후, 같은 레벨끼리 리스트로 묶어서 리턴하는 문제이다 해결 2가지 방식으로 풀었다 1. BFS로 탐색한 후, 가장 상위 레벨의 노드부터 호출하기 위해 스택에 담아 리턴한다 static void bf.. 2020. 11. 1.
[Leetcode(릿코드)] 111. Minimum Depth of Binary Tree (Easy) BFS 문제 leetcode.com/problems/minimum-depth-of-binary-tree/ Minimum Depth of Binary 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 문제 루트 노드에서 시작해서 리프 노드까지 가장 짧은 거리를 구하는 문제이다 dfs 문제에서 골랐는데, bfs로 푸는 게 더 적합한 듯하다 해결 BFS로 탐색하다가 자식노드가 없으면 더이상 탐색하지 않는다 private static int bfs(TreeNo.. 2020. 11. 1.
[Leetcode(릿코드)] 441. Arranging Coins (Easy) Binarysearch 문제leetcode.com/problems/arranging-coins/Arranging Coins - LeetCodeLevel 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문제1+2+3..+k 2020. 10. 28.
[Leetcode(릿코드)] 349. Interserction of Two Arrays (Easy) BinarySearch 문제 leetcode.com/problems/intersection-of-two-arrays/ Intersection of Two Arrays - 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 문제 두 배열의 교집합을 구하는 문제이다 ex) nums1 = [1,2,2,1], num2= [2,2] 답은 [2] 해결 2가지 방식으로 풀었다 Runtime은 Set은 6~7ms / BinarySearch가 5ms 걸렸다 1. Set (O(n)) .. 2020. 10. 27.