본문 바로가기

전체 글

(115)
Linked List Cycle 2 (in Python) Problem Given a linked list, return the node where the cycle begins. If there is no cycle, return null.To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.Note: Do not modify the linked list. Example 1:Input: head = [3,2,0,-4], pos = 1 Outp..
Minimum Depth of Binary Tree (in Python) ProblemGiven a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Note: A leaf is a node with no children.Example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7return its minimum depth = 2. Solution Approach 1 : Recursion 12345678910111213141516171819class Solution: def minDepth(sel..
Sort linked list (in Python) Problem Sort a linked list in O(n log n) time using constant space complexity. Example 1:Input: 4->2->1->3 Output: 1->2->3->4 Example 2:Input: -1->5->3->4->0 Output: -1->0->3->4->5 Solution 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152class ListNode: def __init__(self, x): self.val = x self.next = None class Solution(object): def merge(self, l1, ..
Minimum Increment to Make Array Unique (in Python) Problem Given an array of integers A, a move consists of choosing any A[i], and incrementing it by 1.Return the least number of moves to make every value in A unique. Example 1:Input: [1,2,2] Output: 1 Explanation: After 1 move, the array could be [1, 2, 3]. Example 2:Input: [3,2,1,2,1,7] Output: 6 Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7]. It can be shown with 5 or less ..
Climbing Stairs (in Python) ProblemYou are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer. Example 1:Input: 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps Example 2:Input: 3 Output: 3 Explanation: There are three ways to climb..
Add Binary (in Python) Problem Given two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0.Example 1:Input: a = "11", b = "1" Output: "100"Example 2:Input: a = "1010", b = "1011" Output: "10101" Solution 123456789101112131415161718192021222324class Solution: def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str """ # ans = in..
Rotate Image (in Python) ProblemYou are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Note:You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.Example 1:Given input matrix = [ [1,2,3], [4,5,6], [7,8,9] ], rotate the input matrix in-place such that it becomes: [ [7,4,1], [8,5,2..
Permutations (in Python) Problem Given a collection of distinct integers, return all possible permutations.Example:Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] Solution •종료 조건 : index가 현재 배열의 길이와 같을 때.•i번째 원소와 현재 선택한 원소(first)를 바꾸고•i+1번째 원소부터 다시 탐색을 시작한다.•i번째 원소를 다시 돌려 놓는다. (다음 탐색에 영향을 주지 않기 위해서.) 123456789101112131415161718192021222324class Solution: def permute(self, nums): """ :type..