[LeetCode] 19. Remove Nth Node From End of List
·
코딩테스트
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 1️⃣ head = null 이라면 return null 2️⃣ n == 1 일 경우 (맨 뒤의 노드 삭제) 3️⃣ n == 2 일 경우 (뒤에서 두번째 노드 삭제) : next.next가 없기때문에 따로 진행 *️⃣ 그 외 class Solution { ..
[LeetCode] Delete Node in a Linked List
·
코딩테스트
leetcode.com/explore/featured/card/top-interview-questions-easy/93/linked-list/553 처음에는 그냥 다음걸로 바꿔주면 되는 거 아닌가 생각해서 하단 코드를 적었는데 결과가 다음노드로 넘어가는 게 아니라 현재노드의 바로 다음노드만 바뀌는 것을 확인했다. class Solution { public void deleteNode(ListNode node) { node = node.next; node.next = node.next.next; } } Input : [4,5,1,9] 5 Output : [4,5,1] Expected answer : [4,1,9] 생각을 해보니 단방향 Linked List 라서 주어진 node 이전의 노드 next는 바꿀수..
[LeetCode] Binary Tree Level Order Traversal
·
코딩테스트
leetcode.com/explore/featured/card/top-interview-questions-easy/94/trees/628 Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 레벨 순회하기 (큐를 이용!) LinkedList를 parent 노드와 child 노드로 나누어서 while 문에서 해결하려고 했는데 잘 안됐다. 못풀어서 답을 본 문제..
[LeetCode] 101. Symmetric Tree
·
코딩테스트
leetcode.com/problems/symmetric-tree/ Symmetric 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 첫 번째 시도 ( Fail ) 리스트에 각 숫자를 담아서 맨 앞이랑 맨 뒤를 짝을 지어 비교하면 될 것이라고 생각하고 코드를 구현했다. class Solution{ ArrayList list = new ArrayList(); public void align(TreeNode root){ if (root != null){ ..
[LeetCode] 98. Validate Binary Search Tree
·
코딩테스트
leetcode.com/explore/featured/card/top-interview-questions-easy/94/trees/625/ Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 첫 번째 시도 ( Fail ) 재귀함수를 써보려고 했는데, 결과적으로는 개별노드의 왼쪽/오른쪽 자식노드만 기준을 만족한다. 문제에 대한 이해가 부족했다. class S..
[LeetCode] Maximum Depth of Binary Tree
·
코딩테스트
leetcode.com/explore/featured/card/top-interview-questions-easy/94/trees/555/ Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com root 가 null 이면 return 0 하고, leaf 노드이면 1을 리턴하면 되지 않을까..해서 어쨋든 루트 노드에 닿아야 한다고 생각했던 나는 루트 노드의 개수..
[LeetCode] 1051. Height Checker
·
코딩테스트
leetcode.com/problems/height-checker/ Height Checker - 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 보자마자 떠오른 방법은 정렬한 배열을 만들어서 비교하는것 나중에 풀이를 검색해보니, 대부분이 사람들이 해당 방식을 사용했다. 오늘 추가로 알아낸 점은 Arrays 라이브러리의 copyOf를 사용할 필요없이 clone(); 메서드로 쉽게 배열을 복제할 수 있다는 것! Arrays.copyOf는 예전에 배열 정렬할 때 사용..
[LeetCode] 831. Masking Personal Information
·
코딩테스트
leetcode.com/problems/masking-personal-information/ Masking Personal Information - 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 처음에 지역번호에 + 이외에 - 도 나오는 줄 알고 조건 걸었는데, 조건이 필요가 없었다. 그래서 반복적인 조건을 없애보려는 노력도 부질없는..😂 첫 번째 풀이 class Solution { public String maskPII(String S) { // mask an..
[LeetCode] 1518. Water Bottles
·
코딩테스트
leetcode.com/problems/water-bottles/ Water Bottles - 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 변수 설정 애매하게 해서 돌아돌아 맞춘 정답...🤦‍♀️ class Solution { public int numWaterBottles(int numBottles, int numExchange) { int drink = numBottles; while (numBottles >= numExchange){ drink += nu..
[LeetCode] 202. Happy Number
·
코딩테스트
leetcode.com/problems/happy-number/ Happy 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 아.. 재귀호출 StackOverflowError 가 났다. class Solution { public boolean isHappy(int n) { if(n == 1){ return true; } if(n == 2 || n == 3){ return false; } int sum = 0; while(n > 0){ sum += M..