Algorithm
-
[Leetcode] 7. Reverse Integer TypescriptAlgorithm 2022. 8. 25. 14:57
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Example 1: Input: x = 123 Output: 321 Example 2: Input: x = -123 Output: -321 Example 3: Input: x = 120 Output: 21 Constraints: -231
-
[Leetcode] 6. Zigzag Conversion TypescriptAlgorithm 2022. 8. 25. 14:23
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 = "PA..
-
[Leetcode] 5. Longest palindromic substring (Typescript)Algorithm 2022. 2. 1. 16:37
Given a string s, return the longest palindromic substring in s. Example 1: Input: s = "babad" Output: "bab" Explanation: "aba" is also a valid answer. Example 2: Input: s = "cbbd" Output: "bb" Constraints: 1 { while (lIndex >= 0 && rIndex { const evenPalindrome = checkPalindrome(i, i + 1); const oddPalindrome = checkPalindrome(i, i + 2); const values = [longestPalindrome, evenPalindrome, oddPal..
-
[leetcode] 4. Median of Two Sorted Arrays (Typescript)Algorithm 2021. 2. 21. 13:59
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. Follow up: The overall run time complexity should be O(log (m+n)). Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2. Example 2: Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanation: merged array = [1,2,3,4] an..
-
[leetcode] 3. Longest Substring Without Repeating Characters (Typescript)Algorithm 2021. 2. 20. 14:14
Given a string s, find the length of the longest substring without repeating characters Example 1: Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answ..
-
[Algorithm] Sliding Window 알고리즘 (Javascript)Algorithm 2021. 2. 14. 14:24
배열이나 리스트의 요소들의 일정 범위 값을 비교할 때 사용하는 알고리즘 이름처럼 고정된 윈도우가 일정한 범위를 유지하면서 이동하는 알고리즘 시간복잡도: O(n) // 특정 크기의 부분 배열의 최대 값을 구하는 예제 function maxSumArr(arr, size) { let maxSum = 0; let tempSum = 0; if(arr.length < size) return null; for(let i = 0; i < size; i++) { tempSum += arr[i]; } tempSum = maxSum; for(let i = size; i < arr.length; i++) { tempSum = tempSum - arr[i - size] + arr[i]; maxSum = Math.max(temp..
-
[leetcode] 2. Add two numbers (Typescript)Algorithm 2021. 2. 13. 13:54
문제 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1: Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 34..
-
[leetcode] 1. Two Sum (Typescript)Algorithm 2021. 2. 12. 14:25
문제 Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Because nums[0] + nums[1] == 9, we return [0, 1]. Exam..