본문 바로가기

Algorithm21

[Java] 94. Binary Tree Inorder Traversal 문제 설명 : tree에서 inorder Traversal을 구현하는 문제이다. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public List inorderTraversal(TreeNode root).. 2023. 6. 12.
[Java] 88. Merge Sorted Array 문제 설명 : 2개의 int 배열이 주어지고, 이 두 배열을 합치면서 정렬시키는 문제이다. 정렬하는 과정에서 각 배열에 유효한 값(덮어써지면 안되는 값)을 m과 n으로 알려주고 있다. class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int[] larger = (nums1.length >= nums2.length)?nums1:nums2; int[] shorter = (nums1.length >= nums2.length)?nums2:nums1; int check = (nums1.length >= nums2.length)?nums1.length-m:nums2.length-n; int sLength = shorter.len.. 2023. 6. 12.
[Java] 83. Remove Duplicates from Sorted List 문제 설명 : Linked List에서 중복된 값을 제거하는 문제 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode deleteDuplicates(ListNode head) { ListNode current = head; while(current != null && current.next .. 2023. 6. 9.
[Java] 70. Climbing Stairs 문제 설명 : 1칸 또는 2칸씩 계단을 오를 수 있는데, 계단 수가 주어지면 몇 번의 경우의 수로 계단을 오를 수 있는지 도출하는 문제 - 처음코드class Solution { public int climbStairs(int n) { int result = 0; int quotient = n/2; for(int i=0; i 2023. 6. 9.