python3 10

[Python3] 문자열 뒤집기 reversed()

python에서 문자열을 뒤집는 방법은 여러 가지가 있습니다. reversed() 파이썬에서 기본적으로 제공하는 reversed 함수를 사용할 수 있습니다. 그러나 reversed 함수는 뒤집어진 문자열이 아닌 뒤집어진 iterator를 반환합니다. s = "apple pie" print(reversed(s)) # 출력 결과 : 따라서 반환받은 iterator를 join 함수를 통해 다시 문자열로 만들어야 합니다. join함수를 사용하여 길이가 0인 문자열 ''에 iterator를 추가하여 뒤집어진 문자열을 만들 수 있습니다. s = "apple pie" s_iter = reversed(s) print(''.join(s_iter)) # 출력 결과 : eip elppa 슬라이싱 reversed 함수 사용 ..

[LeetCode] 1971. Find if Path Exists in Graph

https://leetcode.com/problems/find-if-path-exists-in-graph Find if Path Exists in Graph - LeetCode Can you solve this real interview question? Find if Path Exists in Graph - There is a bi-directional graph with n vertices, where each vertex is labeled from 0 to n - 1 (inclusive). The edges in the graph are represented as a 2D integer array edges, where leetcode.com 접근 BFS 또는 DFS 로 탐색하는 문제 graph,..

[LeetCode] 989. Add to Array-Form of Integer

https://leetcode.com/problems/add-to-array-form-of-integer Add to Array-Form of Integer - LeetCode Add to Array-Form of Integer - The array-form of an integer num is an array representing its digits in left to right order. * For example, for num = 1321, the array form is [1,3,2,1]. Given num, the array-form of an integer, and an integer k, return the ar leetcode.com 접근 정수 배열인 num 을 정수로 바꾸어 num 과..

[Python3] dictionary 사용 방법

hashmap 을 구현할 수 있는 python dictionary 사용 방법을 서술합니다. dictionary 는 key 와 value 한 쌍의 집합체입니다. 또한 집합 원소 간 순서가 보장되지 않습니다. (순서가 없음) 선언 hashMap = {} 아래와 같이 초기화 할 수 있습니다. hashMap = dict(apple = 10, banana = 6, grape = 7) key 와 value 추가 및 업데이트 keyName 이 없을 경우 추가, 존재하는 경우 value 를 업데이트합니다. hashMap['keyName'] = value key 와 value 삭제 list 삭제와 동일한 방식입니다. del hashMap['keyName'] key 존재 여부 확인 in, not in 구분을 통해 확인 여..

[LeetCode] 278. First Bad Version

https://leetcode.com/problems/first-bad-version/ 피봇을 사용하여 탐색하는 문제입니다. First Bad Version - LeetCode Can you solve this real interview question? First Bad Version - You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed base leetcode.com 솔루션 # The isBadVersion API is..

[LeetCode] 205. Isomorphic Strings

제 3의 문자열로 표현하여 문제를 해결할 수 있습니다. https://leetcode.com/problems/isomorphic-strings/ Isomorphic Strings - LeetCode Isomorphic Strings - Given two strings s and t, determine if they are isomorphic. Two strings s and t are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving th leetcode.com 솔루션 class Solutio..

[Python3] 다차원 리스트 생성

계속 까먹는 파이썬 다차원 리스트 생성, 알고리즘 문제 푸는 데 유용한 도구가 되니 더 이상 까먹지 않도록 블로그에 기록합니다. 생성 모든 원소 값 0을 갖는 2차원 리스트를 생성합니다. n = 2 table = [[0 for _ in range(n)] for _ in range(n)] # [[0,0], [0,0]] 주의점 아래 프로그램을 실행하여 동일한 결과를 얻을 수 있으나, 일반적인 다차원 리스트를 생성하는 것이 아닌 각 인덱스에 대한 참조 리스트를 생성합니다. n = 2 table =[([0]*n)] *n # [[0,0], [0,0]] table[1][1] 값을 1로 만들고자 할 때, table[0][1] 또한 1로 변경되는 것을 확인할 수 있습니다. print(table) # [[0,0], [0..

[LeetCode] 167. Two Sum II - Input Array Is Sorted

Two pointer 알고리즘을 활용하여 시간복잡도가 O(n) 이내가 되도록 문제를 풀어야 합니다. 본문 https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ Two Sum II - Input Array Is Sorted - LeetCode Two Sum II - Input Array Is Sorted - Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers..

[LeetCode] 438. Find All Anagrams in a String

Sliding Window 알고리즘과 HashMap (파이썬에서는 dictionary로 구현) 자료구조에 대한 이해가 필요한 문제였습니다. https://leetcode.com/problems/find-all-anagrams-in-a-string 문제 핵심 일반적인 반복문 또는 Two pointer 기법을 사용하면 time limit 따라서 계산 횟수를 최소화 하는 기법을 사용해야 함 Two pointer 알고리즘과 비슷한 Sliding Window 알고리즘을 사용해야 한다 두 개의 해쉬맵을 만들어 계산을 간소화 솔루션 class Solution: def findAnagrams(self, s: str, p: str) -> List[int]: s_length, p_length = len(s), len(p)..