본문 바로가기

Python/PS in Python55

LeetCode160. Intersection of Two Linked Lists - Python LeetCode160. Intersection of Two Linked Lists - Python Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: begin to intersect at node c1. Notes: If the two linked lists have no intersection at all, return null. The linked lists must retain their original structure after the function returns. You may assume the.. 2020. 9. 16.
LeetCode 141. Linked List Cycle 141. Linked List Cycle Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter. Return t.. 2020. 9. 14.
[LeetCode] Easy 문제 풀이 모음 - Python 1. Two Sum Given an array of integers nums and and integer target, return the 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 re.. 2020. 9. 12.
LeetCode 198. House Robber - Python LeetCode 198. House Robber - Python You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. Given a list of non-negat.. 2020. 9. 11.
LeetCode 155. Min Stack - Python 155. Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum element in the stack. Constraints: Methods pop, top and getMin operations will always be called on non-empty stacks. Example: Input [".. 2020. 9. 9.
LeetCode 53. Maximum Subarray - Python LeetCode 53. Maximum Subarray - Python Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle. Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: [4,-1,2.. 2020. 9. 7.
LeetCode 101. Symmetric Tree - Python LeetCode 101. Symmetric Tree - Python Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the following [1,2,2,null,3,null,3] is not: 1 / \ 2 2 \ \ 3 3 Solution : Recursive class Solution: def isSymmetric(self, root: TreeNode) -> bool: if not root: #루트가 없으면 True 반환.. 2020. 9. 4.
LeetCode 1. Two Sum - Python LeetCode 1. Two Sum - Python Given an array of integers nums and and integer target, return the 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[.. 2020. 8. 31.
LeetCode 543. Diameter of Binary Tree - Python LeetCode 543. Diameter of Binary Tree - Python Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. 이진트리의 지름 구하기 즉 가장 긴 path의 '길이'를 구하면 된다. 이 때, 노드 사이의 길이란 노드 사이의 edge의 개수이다. 예를 들어 아래의 경우 path [4,2,1,3] 나 [5,2,1,3]가 가장 길기 때문에.. 2020. 8. 25.
LeetCode 121. Best Time to Buy and Sell Stock - Python Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. Note that you cannot sell a stock before you buy one. Example 1: Input: [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day .. 2020. 8. 24.