알고리즘 트레이닝

문제 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered valid. Example 1: Input: "()" Output: trueEx..
문제 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: trueExample 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it..
문제 Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].풀이 class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]..
문제 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.Note: All given inputs are in lowercase letters a-z. 풀이 class Solution: def lo..
문제 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 1000For 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 fro..
문제 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321Example 2: Input: -123 Output: -321Example 3: Input: 120 Output: 21Note: 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 ov..
문제 (요약) 주어진 입력에서 고른 두 수의 곱을 출력하라. 이 곱은 증가하는 연속숫자(예, 2, 23, 23456, 456 등)로 이루어져야 한다. 이 값이 여러 개인 경우 최댓값을 출력하고 조건에 맞도록 두 수를 고를 수 없을 경우 -1을 출력하라 접근 입력으로 10^3개 이하의 수열이 주어지므로 이들을 조합하여 곱을 구하면 10^6 개의 곱을 얻고 이들 값이 각각 조건을 만족하는지 확인하면 된다. 이는 충분히 작은 숫자이므로 전수조사(O(N^2))로 풀이할 수 있다. 풀이 #include #include #include FILE* stream; using namespace std; int T, N; int main(void) { cin >> T; for (int t = 1; t > N; vector..
개요 여러 언어에서 기본적으로 제공하는 자료구조인 배열은 특정 순서로 무언가 저장하기에 좋고 간단하지만 단점이 있습니다. 배열은 유연하지 않습니다. 예를 들어, 배열의 크기를 미리 지정해야 하며, 프로그램의 동작 중에 그것을 수정하는 것은 매우 어렵습니다. (이 단점은 C++의 표준 템플릿 라이브러리(STL, Standard Template Library)인 벡터(Vector)에서 해결됩니다.) 또한 요소의 삽입 및 삭제가 어렵습니다. 삽입을 위한 공간을 확보하거나 삭제 후 빈 공간을 채우기 위해 나머지 요소를 좌/우로 이동해야 합니다. 이번 포스트에서는 연결리스트(Linked List)라고 하는 중요한 자료구조의 구현을 살펴보겠습니다. 가장 간단한 형태의 연결 리스트는 한 방향으로 연결된 리스트로 리스..
쓴웃음
'알고리즘 트레이닝' 카테고리의 글 목록 (3 Page)