Coding Test/LeetCode
-
[LeetCode] 167. Two Sum II - Input Array Is Sorted c++Coding Test/LeetCode 2022. 12. 26. 16:18
문제 Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1
-
[LeetCode] 283. Move Zeroes c++Coding Test/LeetCode 2022. 12. 24. 17:54
문제 Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0] 문제 해결 처음에는 벡터 원소에 0이 있으면 삭제하는 문제인 줄 알았다. remove_if() 함수를 사용해서 0이라면 삭제하는 코드를 작성했다. class Solution { public: static bool oper(const int& a){ if( a == 0){ r..
-
[LeetCode] 606. Construct String from Binary Tree c++Coding Test/LeetCode 2022. 12. 24. 14:58
문제 Given the root of a binary tree, construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it. Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree. Input: root = [1,2,3,null,4] Output: "1(2()(4))(3)" Explanation: Almost the same as the first ex..
-
[LeetCode] 144. Binary Tree Preorder Traversal c++Coding Test/LeetCode 2022. 12. 23. 17:58
문제 Given the root of a binary tree, return the preorder traversal of its nodes' values. Input: root = [1,null,2,3] Output: [1,2,3] 문제 해결 class Solution { private: void preorder(TreeNode* curr, vector& vec){ if(!curr){ return; } vec.push_back(curr->val); preorder(curr->left, vec); preorder(curr->right, vec); return; } public: vector preorderTraversal(TreeNode* root) { vector vec; preorder(root, v..
-
[LeetCode] 189. Rotate Array c++Coding Test/LeetCode 2022. 12. 23. 15:10
문제 Given an array, rotate the array to the right by k steps, where k is non-negative. Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] 문제 해결 rotate는 맨 뒤 원소가 맨 앞원소로 이동된다. k 개수 만큼 nums맨뒤의 원소가 맨앞으로 rotate된다. 회전한 원소들이 기존 순서를 유지하여 rotate되는 것을 ..
-
[LeetCode] 977. Squares of a Sorted Array c++Coding Test/LeetCode 2022. 12. 23. 14:10
문제 Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. 주어진 배열의 원소들을 제곱한뒤 오름차순으로 재정렬하여 답을 구하는 문제이다. 문제 해결 sort함수를 사용한 문제 풀이이다. class Solution { public: vect..
-
[LeetCode] 145. Binary Tree Postorder Traversal c++Coding Test/LeetCode 2022. 12. 22. 18:12
문제 Given the root of a binary tree, return the postorder traversal of its nodes' values. Input: root = [1,null,2,3] Output: [3,2,1] 문제 해결 vector postorderTraversal(TreeNode* root) { stack stk; if(root){ stk.push(root); } vector ans; while(stk.size()){ auto p = stk.top(); if(p == nullptr){ stk.pop(); ans.push_back(stk.top()->val); stk.pop(); continue; } stk.push(nullptr); if(p->right){ stk.push(p..
-
[LeetCode] 872. Leaf-Similar Trees c++Coding Test/LeetCode 2022. 12. 22. 15:33
문제 Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a leaf value sequence. For example, in the given tree above, the leaf value sequence is (6, 7, 4, 9, 8). Two binary trees are considered leaf-similar if their leaf value sequence is the same. Return true if and only if the two given trees with head nodes root1 and root2 are leaf-similar. Input:..