본문 바로가기

[JAVA] LeetCode 122 - Best Time to Buy and Sell Stock II Say you have an array prices for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). i일(day)에 주식의 가격이 적힌 ..
[JAVA] LeetCode 217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 정수형 배열이 주어지면, 배열에 중복된 항목이 있는지 찾아라. 배열에 값이 두 번 이상 나타나면 함수가 true를 반환해야 하고, 모든 항목이 중복되지 않으면 false를 반환해야 한다. 풀이 방법 중복 되지 않는 값을 저장하는 Set을 이용하여 풀이하기! 1. 배열의 길이만큼 반복문을 수행하며, HashSet에 값이 존재할 경우 t..
[JAVA] LeetCode 171 - Excel Sheet Column Number Given a column title as appear in an Excel sheet, return its corresponding column number. 엑셀 시트에 나타나는 열 타이틀이 주어지면, 해당 열의 번호를 반환하라. Example 1:Input: "A" Output: 1 Example 2: Input: "AB" Output: 28 Example 3: Input: "ZY" Output: 701 풀이 방법 예제로 주어진 Input과 Output을 분석해보면, 각 문자의 인덱스 별 열 번호를 공식을 만들 수 있다. 나의 경우, 26^(문자열의 길이 - 현재 문자의 인덱스) * (현재 문자의 번호) 라는 공식을 만들었고, 현재 문자의 번호는 char 타입의 문자(아스키 코드)에서 - 64를 하..
[JAVA] LeetCode 242 - Valid Anagram Given two strings s and t , write a function to determine if t is an anagram of s. 두개의 문자열 s와 t가 주어지면, t가 s의 anagram인지 여부를 판단하는 함수를 작성하라. + anagram : 일종의 말장난으로 어떠한 단어의 문자를 재배열하여 다른 뜻을 가지는 다른 단어로 바꾸는 것 풀이 방법 1. s와 t의 문자열 길이가 다를 경우, 바로 false를 리턴한다. 2. s 문자열을 한 글자씩 HashMap에 넣고, 글자(알파벳) 별 카운팅을 한다. 3. t 문자열을 한 글자씩 HashMap에 존재하는지 비교하여, 해당 알파벳이 없을 경우 false를 리턴한다. 4. 만약 HashMap에 존재하는 글자의 개수가 다를 경우 false..
[JAVA] LeetCode 283 - Move Zeroes Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. 주어진 배열의 상대적 순서를 유지하면서 모든 0을 배열의 끝으로 이동시키는 함수를 작성하라. 풀이 방법 1. 배열의 길이만큼 반복문을 수행하며 nums[i]가 0일 때는 zero, 0이 아니면 index 변수의 값을 증가시켜준다. 2. nums[i]가 0이 아닐 때는, nums[index]에 nums[i] 값을 대입시켜준다. (0을 제외하고 나머지 숫자들을 앞쪽으로 당기게끔) 3. 마지막으로 zero의 개수만큼 배열의 뒷 부분을 0으로 채우면 된다. class Solu..
[JAVA] LeetCode 108 - Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 오름차순으로 정렬된 배열을 균형 이진 트리로 변환하라 균형 이진 트리는 두 하위 트리의 깊이(depth)가 1이 넘게 차이나지 않는 것을 의미힌다. 풀이 방법 1. 배열의 중앙 인덱스(mid)를 루트 노드로 생성한다. 2. 배열의 시작 인덱스(0)부터 m..
[JAVA] LeetCode 169 - Majority Element Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority element always exist in the array. 크기가 n인 배열이 주어질 때, majority인 요소를 를 찾으시오. majority인 요소는 n/2번 이상 나타나는 요소이다. 배열이 비어있지 않고, majority인 요소는 항상 존재한다. (majority라는 단어를 한국말로 나타내기 난해하여.... 다수의.. 이런 뜻을 가진답니다) 풀이 방법 1. 입력으로..
[JAVA] LeetCode 206 - Reverse Linked List Reverse a singly linked list. 단일 연결 리스트를 뒤집어라. 풀이 방법 1. reverse된 결과를 리턴 할 ListNode 타입의 node를 선언한다. 2. 시작 노드부터 마지막 노드까지 각 노드가 순차적으로 head가 되어 반복문을 수행한다. 3. head 노드를 변경하기 전에 저장해 둔 temp 노드의 next값에 node를 할당한다. 4. 글로 백 번 적는 것보다, 한 번 그림으로 그리면서 로직을 따라가면 더 확실히 이해가 될 것이다! class Solution { public ListNode reverseList(ListNode head) { ListNode node = null; while(head != null) { ListNode temp = head; head = ..