전체 글

    LeetCode[day11] - Diameter of Binary Tree

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them 1. 알고리즘을 해결하기 위해서 생각한 방법 - 우선, 해당 문제는 이진 트리가 input으로 주어진다. - 이진트리란 무엇일까? 이진트리는..

    LeetCode[day10] - Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum element in the stack. Methods pop, top and getMin operations will always be called on non-empty stacks. Input ["MinStack","push","push","push","getMi..

    LeetCode[day9] - Backspace String Compare

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. Note that after backspacing an empty text, the text will continue empty. Note: 1 0) {skipS--; i--;} else break; } while (j >= 0) { // Find position of next possible char in build(T) if (T.charAt(j) == '#') {skipT++; j--;} else if (skipT > 0) {skipT--; j--;} else break; ..

    Leetcode[day8] - Middle of the Linked List

    Given a non-empty, singly linked list with head node head, return a middle node of linked list. If there are two middle nodes, return the second middle node. Note: The number of nodes in the given list will be between 1 and 100. Input: [1,2,3,4,5] Output: Node 3 from this list (Serialization: [3,4,5]) The returned node has value 3. (The judge's serialization of this node is [3,4,5]). Note that w..

    Leetcode[day7] - Counting Elements

    Given an integer array arr, count element x such that x + 1 is also in arr. If there're duplicates in arr, count them seperately. Input: arr = [1,2,3] Output: 2 Explanation: 1 and 2 are counted cause 2 and 3 are in arr. Input: arr = [1,1,3,3,5,5,7,7] Output: 0 Explanation: No numbers are counted, cause there's no 2, 4, 6, or 8 in arr. Input: arr = [1,3,2,3,5,0] Output: 3 Explanation: 0, 1 and 2 ..

    Leetcode[day6] - Group Anagrams

    Given an array of strings, group anagrams together. Note: All inputs will be in lowercase.The order of your output does not matter. Input: ["eat", "tea", "tan", "ate", "nat", "bat"] Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ] 1. 알고리즘을 해결하기 위해서 생각한 방법 - HashMap 자료구조를 사용한다. - Key는 "eat","tea","ate"를 하나로 묶을 수 있는 sort된 단어가 필요하다. - value는 list가 들어간다. - key에 대한 value가 있다면, 해당 value인 리스트를 가..

    LeetCode[day5] - 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). Input: [7,1,5,3,6,4]..

    LeetCode[day4] - Move zeros

    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. Note: 1. You must do this in-place without making a copy of the array.Minimize the total number of operations. 2. Minimize the total number of operations. Example: Input: [0,1,0,3,12] Output: [1,3,12,0,0] 1. 알고리즘을 해결하기 위해서 생각한 방법 - 문제의 조건에서는 별도의 다른 array 공간을 만들면 안..