본문 바로가기

분류 전체보기351

[leetcode 33] Search in Rotated Sorted Array You are given an integer array nums sorted in ascending order, and an integer target. Suppose that nums is rotated at some pivot unknown to you beforehand (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). If target is found in the array return its index, otherwise, return -1. 본래 오름차순인 배열이 특정 위치를 중심으로 회전되어 있다. 주어진 target의 위치를 찾아라. Example 1: Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4.. 2020. 10. 17.
[leetcode 21] Merge Two Sorted Lists Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists. 오름차순순으로 리스트를 합치자. Example 1: Input: l1 = [1,2,4], l2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2: Input: l1 = [], l2 = [] Output: [] Example 3: Input: l1 = [], l2 = [0] Output: [0] 풀이: -포인터만 잘 활용한다면 쉬운 문제이다. - 난이도: easy /** * Definition for singly-lin.. 2020. 10. 17.
[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.