알고리즘
-
[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..
-
[LeetCode] Reverse Integer알고리즘 2019. 9. 7. 01:34
문제 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer..
-
[LeetCode] Longest Substring Without Repeating Characters알고리즘 2019. 9. 6. 00:51
문제 Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Note that the answer must be a..
-
[LeetCode] Next Permutation알고리즘 2019. 9. 4. 23:03
문제 Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place and use only constant extra memory. Here are some examples. Inputs are in the left-hand column and its corresponding outputs ..