전체 글
-
[LeetCode] Divide Two Integers알고리즘 2019. 10. 2. 02:54
문제 Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator. Return the quotient after dividing dividend by divisor. The integer division should truncate toward zero. Example 1: Input: dividend = 10, divisor = 3 Output: 3 Example 2: Input: dividend = 7, divisor = -3 Output: -2 Note: Both dividend and divisor will be 32-bit signed intege..
-
[LeetCode] Implement strStr()알고리즘 2019. 9. 30. 01:13
문제 Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa", needle = "bba" Output: -1 Clarification: What should we return when needle is an empty string? This is a great question to ask during an interview. For the purpose of thi..
-
[LeetCode] Remove Duplicates from Sorted Array알고리즘 2019. 9. 29. 19:51
문제 Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,2], Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It does..
-
[LeetCode] Merge k Sorted Lists알고리즘 2019. 9. 28. 20:39
문제 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [ 1->4->5, 1->3->4, 2->6 ] Output: 1->1->2->3->4->4->5->6 Approach Min heap에 list의 앞부분을 하나씩 담은 후 answer list에 min heap의 맨 위 원소를 넣어준다. answer에 들어간 원소는 next가 null이 아닌 경우 그 다음 원소를 다시 min heap에 넣는다. Code https://github.com/chi3236/algorithm/blob/master/LeetCode_MergeKSortedLists_MinH..
-
[LeetCode] Generate Parentheses알고리즘 2019. 9. 26. 02:19
문제 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: [ "((()))", "(()())", "(())()", "()(())", "()()()" ] Approach (의 최대 개수는 n개이고 )는 n이 등장한 만큼만 나와야한다 이 조건을 만족하며 완전탐색을 한다 Code https://github.com/chi3236/algorithm/blob/master/LeetCode_GenerateParentheses.cpp chi3236/algorithm Contribute to chi3236/al..
-
[LeetCode] Remove Nth Node From End of List카테고리 없음 2019. 9. 21. 21:48
문제 Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Given n will always be valid. Approach 끝에서 부터 n번째이기 때문에 조금 신경을 써야한다. vector를 만들고 node를 읽을 때 하나씩 벡터에 push_back해서 모든 노드를 저장한 다음, 전부 읽은 뒤에서 n+1번째랑 n-1를 이어주면 끝 n이 전체 list 길이일..
-
[LeetCode] Letter Combinations of a Phone Number알고리즘 2019. 9. 21. 21:03
문제 Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. Example: Input: "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. Note: Although the above answer is in lexicographical order,..
-
[LeetCode] Container With Most Water알고리즘 2019. 9. 21. 18:47
문제 Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. The above vertical lines are repr..