본문 바로가기

알고리즘 문제풀이/leetcode142

[leetcode 22] Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. n개의 올바른 괄호로 구성된 문자열 만들기 Example 1: Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] Example 2: Input: n = 1 Output: ["()"] 풀이: -백트래킹 문제 -비교적 쉬웠다. class Solution { public: vector answer; int N; void BackTracking(int front, int back, string tmp){ if(front == N && back == N){ a.. 2020. 10. 15.
[leetcode 20] Valid Parentheses Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Example 1: Input: s = "()" Output: true Example 2: Input: s = "()[]{}" Output: true Example 3: Input: s = "(]" Output: false Example 4: In.. 2020. 10. 15.
[leetcode 19] Remove Nth Node From End of List Given the head of a linked list, remove the nth node from the end of the list and return its head. Follow up: Could you do this in one pass? 끝에서 n번째 노드를 제거하는 문제 Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Example 2: Input: head = [1], n = 1 Output: [] Example 3: Input: head = [1,2], n = 1 Output: [1] 풀이: - 투포인터를 사용하면 한 번에 가능하다는데 귀찮아서 안해봤다. 나중에 해봐야지 - linked list를 이용한 더미 포인터를 만들.. 2020. 10. 15.
[leetcode 17] Letter Combinations of a Phone Number Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order. 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 1: Input: digits = "23" Output: ["ad","ae","af","bd","be","bf","cd","c.. 2020. 10. 14.