분류 전체보기
-
[LeetCode] Merge Two Sorted List알고리즘 2019. 9. 21. 01:49
문제 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 Approach https://noname122.tistory.com/9?category=851290 [LeetCode] Median of Two Sorted Array 문제 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ..
-
[LeetCode] Regular Expression Matching알고리즘 2019. 9. 21. 01:24
문제 Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.'.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Note: s could be empty and contains only lowercase letters a-z. p could be empty and contains only lowercase letters a-z, and charact..
-
[LeetCode] Longest Common Prefix알고리즘 2019. 9. 20. 01:34
문제 Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: ["flower","flow","flight"] Output: "fl" Example 2: Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. Approach 빈 vector 걸러내고, string들 중 길이가 가장 짧은 것을 기준으로 최대 탐색치를 정한다음 스트링별로 0번 in..
-
[LeetCode] String to Int알고리즘 2019. 9. 20. 01:11
문제 Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters a..
-
[LeetCode] ZigZag Conversion알고리즘 2019. 9. 19. 21:39
문제 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string s, int numRows); Example 1: Input: s = ..
-
[LeetCode] Longest Palindromic Substring알고리즘 2019. 9. 19. 01:42
문제 Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example 2: Input: "cbbd" Output: "bb" Approach Brute force로 접근. 인풋 스트링 길이에 해당하는 substring부터 길이를 1씩 줄이며 찾아나감 시간복잡도 O(n^3) Optimization Dynamic Programming O(n^2) 추가: Palindrome은 어떤 string이 palindrome일때 앞 ..
-
[LeetCode] 3Sum알고리즘 2019. 9. 18. 01:43
문제 Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example: Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] Approach 처음 했던 시도는 2Sum 문제에서 활용했던 방식인 hash를 활용하는 것이었으나 무슨 짓을 해도 time limit ..
-
[LeetCode] Roman To Integer알고리즘 2019. 9. 8. 01:46
문제 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from le..