본문 바로가기

0

(102)
Swap Nodes in Pairs (in Python) ProblemGiven a linked list, swap every two adjacent nodes and return its head. Example:Given 1->2->3->4, you should return the list as 2->1->4->3.Note:Your algorithm should use only constant extra space.You may not modify the values in the list's nodes, only nodes itself may be changed. Solution •current 노드를 저장해뒀다가 다음 라운드에서 next 변경해줘야함. 123456789101112131415161718192021222324252627class Solution..
Generate Parentheses (in Python) Problem GIven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n=3, a solution set is:[ "((()))", "(()())", "(())()", "()(())", "()()()" ] Solution Approach 1 : Brute Force Complexity Analysis•Time Complexity : \(O(2^{2n}*n)\). For each of \(O(2^{2n})\) sequences, we need to create and validate the sequence, which takes \(O(n)\) ..
Valid Parentheses (in Python) Problem Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: 1. Open brackets must be closed by the same type of brackets.2. Open brackets must be closed in the correct order. Note that an empty string is also considered valid. Example 1:Input: "()" Output: true Example 2:Input: "()[]{}" Output: true E..
Remove Node From End of List (in Python) Problem Given a linked list, remove the n-th node from the end of list and return its head. Note:Given n will always be valid. Example:Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Solution Approach 1 : Two pass algorithmRemove the (L-n+1)th node from the beginning in the list, where L is the list length. •while문을 이용..
Letter Combinations of a Phone Number (in Python) Problem Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters. Example:Input: "23" Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].Note :Although the above answer is in lexicographical orde..
3Sum (in Python) ProblemGiven an array nums of n integers, are there elements a, b, c in nums such that a+b+c=0? Find all unique triplets in the array which gives the sum of zero.Note :The solution set must not contain duplicate triplets.Example:Given array nums = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ] Solution •배열 정렬.•i를 기준으로 left boundary와 right boundary를 설정.1. 세 수의 합이 0보다 작을 경우 ..
Container With Most Water (in Python) Problem Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, O). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note : You may not slant the container and n is at least 2. The above vertical lines are..
ZigZag Conversion (in Python) Problem The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this :P A H N A P L S I I G Y I RAnd then read line by line : "PAHNAPLSIIGYIR" Solution Approach 1 : Sort by Row IntuitionBy iterating through the string from left to right, we can easily determine which row in the Zig-Zag pattern that a character belongs to. Algorithm1. We can use min(numRows, len(..