leetcode 23

[알고리즘][Python] LeetCode 771 Jewels and Stones 문제 풀이

문제 출처 : leetcode.com/problems/jewels-and-stones/ Jewels and Stones - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : 내가 총 몇개의 보석을 가졌는지 출력하는 문제이다. 문제 풀이 : 딕셔너리 자료형을 활요해서 쉽게 풀이 할 수 있다. 풀이 코드 import collections class Solution: def numJewelsInStones(self, jewels: str, stones: s..

[알고리즘][Python] LeetCode 622 Design Circular Queue 문제 풀이

문제 출처 : leetcode.com/problems/design-circular-queue/ Design Circular Queue - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : 원형 큐 클래스를 작성하는 문제이다. 문제 풀이 : 나머지 연산을 이용해서 두개의 인덱스를 이용해서 구현하면 된다. 풀이 코드 class MyCircularQueue: def __init__(self, k: int): self.Q = [-1] * k self.fron..

[알고리즘][Python] LeetCode 232 Implement Queue Using Stacks 문제 풀이

문제 출처 : leetcode.com/problems/implement-queue-using-stacks/ Implement Queue using Stacks - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : Stack을 이용해서 Queue를 구현 하는 문제이다. 문제 풀이 : 다른 기능들은 쉽게 구현 할 수 있지만 pop을 수행 할때는 순서에 유의해서 temp 배열에 저장하고 원하는 값을 꺼낸뒤에 다시 원상태로 복귀 시켜줘야 한다. 풀이 코드 c..

[알고리즘][Python] LeetCode 225 Implement Stack Using Queues 문제 풀이

문제 출처 : leetcode.com/problems/implement-stack-using-queues/ Implement Stack using Queues - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : 큐를 이용해서 스택을 구현하는 문제이다. 문제 풀이 : 큐는 삽입은 뒤에서 삭제는 앞에서 하기 때문에 여기서 신경쓸 것은 Pop 연산이다. 스택은 삭제 또한 뒤에서 하기 때문에 삭제 대상 직전까지 다른 배열에 임시로 저장하고 원하는 값을 제거..

[알고리즘][Python] LeetCode 20 Valid Parentheses 문제 풀이

문제 출처 : leetcode.com/problems/valid-parentheses/ Valid Parentheses - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : 괄호가 제대로 구성되어 있는지 검사하는 문제이다. 문제 풀이 : 시작 괄호와 끝 괄호의 짝을 매칭해가면서 검사하면 된다. 풀이 코드 class Solution: def isValid(self, s: str) -> bool: stack = [] a = { ')': '(', ']': ..

[알고리즘][Python] LeetCode 92 Reverse Linked List 2 문제 풀이

문제 출처 : leetcode.com/problems/reverse-linked-list-ii/ Reverse Linked List II - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : 시작점과 끝점이 주어졌을 때 그 구간을 뒤집어서 반화하는 문제이다. 문제 풀이 : 시작점과 끝점 앞과 뒤를 저장해 놓고 그 사이를 뒤집어서 반환하면 된다. ( 이 문제의 풀이는 Python Algorithm Interview 책을 참고 했습니다. ) 풀이 코드 c..

[알고리즘][Python] LeetCode 328 Odd Even Linked List 문제 풀이

문제 출처 : leetcode.com/problems/odd-even-linked-list/ Odd Even Linked List - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : 홀수번째 리스트 끼리, 짝수번째 리스트끼리 묶어서 다시 링크드 리스트를 반환하는 문제이다. 문제 풀이 : 문제에 제약 조건으로 상수 공간을 사용하라고 했기 때문에 하나씩 건너뛰면서 모두 저장하는 방식이 아닌 순차적으로 논리적인 연결을 수행해야 한다. ( 이 문제의 풀이는..

[알고리즘][Python] LeetCode 24 Swap Nodes in Pairs 문제 풀이

문제 출처 : leetcode.com/problems/swap-nodes-in-pairs/ Swap Nodes in Pairs - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : 링크드 리스트의 페어의 값을 바꾸는 문제이다. 문제 풀이 : 링크드 리스트의 연결 상태를 변경하여 풀이할 수 있다. 다만 무한 루프에 빠지지 않게 주의해서 코드를 작성해야한다. ( 본 풀이는 Python Algorithm Interview 책의 풀이를 참고했습니다. ) 풀이..

[알고리즘][Python] LeetCode 2 Add Two Numbers 문제 풀이

문제 출처 : leetcode.com/problems/add-two-numbers/ Add Two Numbers - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : 주어진 2개의 링크드 리스트에 있는 수를 더해서 다시 링크드 리스트로 반환하는 문제이다. 문제 풀이 : 링크드 리스트를 읽어 드려 합을 더하고 다시 링크드 리스트로 만드는 과정을 거치면 문제를 풀 수 있다. 풀이 코드 class ListNode: def __init__(self, val ..

[알고리즘][Python] LeetCode 206 Reverse Linked List 문제 풀이

문제 출처 : leetcode.com/problems/reverse-linked-list/ Reverse Linked List - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 : 주어진 링크드 리스트를 뒤집어서 반환하는 문제이다. 문제 풀이 : 주어진 링크드 리스트를 하나씩 탐색하면서 새로운 링크드리스트에 연결시켜주어 반환하면 된다. 풀이 코드 class ListNode: val = 0 next = None def __init__(self, val ..