본문 바로가기

전체 글

(42)
[leetcode 75] 142. Linked List Cycle II 문제해석 linked list의 head가 주어졌을 경우 순환이 시작되는 노드를 반환, 만약 순환의 없다면 null을 반환 순환되는 노드는 주어진 노드 내부에 위치하며 내부적으로 pos는 순환되는 노드의 시작 위치를 말하며 (파라미터로 전달되지 않음) 순번은 0부터 시작함 public ListNode detectCycle(ListNode head) { if (head == null || head.next == null) { return null; } Set nodes = new HashSet(); ListNode node = head; while (node != null) { if (nodes.contains(node)) { return node; } nodes.add(node); node = node...
[leetcode 75] 206. Reverse Linked List 문제해석 링크드리스트가 주어졌을 때 역순 변환해서으로 반환하기 정답 예 반복문으로 푸는 방법 public ListNode reverseList(ListNode head) { ListNode reverse = null; while (head != null) { // 새로운 head 생성 ListNode tempHead = new ListNode(head.val); // 새로운 head의 다음 연결리스트를 기존 연걸리스트로 할당 tempHead.next = reverse; // 새로운 head가 포함 된 연결리스트를 기존 리스트로 대체 reverse = tempHead; // head의 다음 연결 리스트로 넘어감 head = head.next; } return reverse; } 재귀로 푸는 방법 publi..
[leetcode 75] 21. Merge Two Sorted Lists 문제해석 두개의 linked list인 list1, list2가 주어졌을 때 두 linked list를 정렬된 상태로 합치고 합쳐진 list의 head를 반환하기 정답 예 public ListNode mergeTwoLists(ListNode list1, ListNode list2) { if (list1 == null) { return list2; } else if (list2 == null) { return list1; } else if (list1.val < list2.val) { list1.next = mergeTwoLists(list1.next, list2); return list1; } else { list2.next = mergeTwoLists(list1, list2.next); return l..
[leetcode 75] 392. Is Subsequence 문제해석 문자열 s와 t가 주어졌을 때 s가 t의 일부가 될 수 있는지 체크 다만 s의 순서는 지켜져야하며 t의 일부가 사라지는 형태로 대체될 수 있음 정답 예 public boolean isSubsequence(String s, String t) { // s의 길이, 마지막 문자 도달 시점과 비교 int sLength = s.length() - 1; // s가 ""인 경우 true if (sLength == -1) { return true; } // s의 순서 int sIndex = 0; char[] schar = s.toCharArray(); char[] tchar = t.toCharArray(); int tLength = t.length(); for (int i = 0; i < tLength; i+..
[leetcode 75] 205. Isomorphic Strings 문제해석 s의 문자열을 t의 문자열로 대체 가능한 경우 Isomorphic이라고 한다 각 문자는 문자의 숫자를 유지하면서 대체가능하여야 하는데 문자 하나가 여러개를 대체할 수 없고 자기 자신은 그대로 대체할 수 있다 예시 Example 1: Input: s = "egg", t = "add" Output: true e > a로, g가 d로 대체될 수 있으므로 true Example 2: Input: s = "foo", t = "bar" Output: false f > b로 대체될 수 있지만 o가 a와 r 두개의 문자로 대체될 수 없으므로 false Example 3: Input: s = "paper", t = "title" Output: true p > t, a > i, e > l, r > e로 대체할 수..
[leetcode 75] 724. Find Pivot Index 문제해석 정수 요소를 갖는 nums 배열이 주어졌을 때 pivot index를 계산해서 반환하기 pivot index는 pivot기준 왼쪽의 합과 오른쪽의 합이 같은 위치인 경우 만약 pivot이 0이면 왼쪽 배열의 합은 0임 이러한 pivot index 중 가장 왼쪽에 위치한 pivot index를 찾아서 반환 답 예 public int pivotIndex2(int[] nums) { int length = nums.length; int[] leftSum = new int[length]; int[] rightSum = new int[length]; for (int i = 1; i < nums.length; i++) { // pivot 기준 왼쪽 배열의 총합을 기록 leftSum[i] = leftSum[i..
[leetcode 75] 1480. Running Sum of 1d Array 문제해석 배열 nums가 주어졌을 때 각 배열의 요소가 이전 요소의 모든 합이 되는 작업을 수행 수 반환 답 예1 public int[] runningSum(int[] nums) { int length = nums.length; int[] sums = new int[length]; sums[0] = nums[0]; for (int i = 1; i < length; i++) { sums[i] = sums[i-1] + nums[i]; } return sums; } 답 예2 public int[] runningSum(int[] nums) { for (int i = 1; i < nums.length; i++) { nums[i] += nums[i-1]; } return nums; }
[leetcode] 1672. Richest Customer Wealth 문제해석 m x n의 accounts라는 정수행렬이 주어졌을 때 각 account[i][j]에는 i번째 고객의 j번째 은행에 있는 돈을 표기해준다. 가장 부유한 고객의 부를 반환해보자 답 예 public int maximumWealth(int[][] accounts) { int length = accounts.length; int maxWealth = 0; for (int i=0 ; i < length; i++) { int wealth = 0; for (int j=0 ; j < accounts[i].length; j++) { wealth += accounts[i][j]; } if (maxWealth < wealth) { maxWealth = wealth; } } return maxWealth; }