๐ Self Study/๐ LeetCode4 LeetCode (Add two numbers) C++ ํํ ๋ฒ์๋ฅผ ๋์ด๊ฐ๋ ์ซ์๊ฐ ์์ด์ ํ ๋นํธ์ฉ ์ฐ์ฐ์ ํด์ค์ผ ํ๋ ๋ฌธ์ . long long์ผ๋ก ํด๋ ์ฒ๋ฆฌ๊ฐ ์๋๋ค. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { string list =.. 2022. 1. 7. LeetCode (Two Sum) C++ class Solution { public: vector twoSum(vector& nums, int target) { vector answer; for(int i = 0 ; i < nums.size() - 1 ; i++) { for(int j = i+1 ; j < nums.size() ; j++) { if(nums[i] + nums[j] == target) { answer.push_back(i); answer.push_back(j); } } } return answer; } }; 2022. 1. 7. LeetCode (Employee Importance) C++ BFS๋ก ์ ๊ทผํด์ ํผ ๋ฌธ์ . ๋จธ๋ฆฟ ์์ ์๊ณ ๋ฆฌ์ฆ์ด ์๋ค๋ฉด ๋ด๊ฐ ํญ์ ํ๋ ๋ฐฉ์์ด ์๋๋ผ ์ฃผ์ด์ง Structure๋ก ํ ์ ์์ด์ผ ํ๋ ๋ฌธ์ ์๋ค. BFS๊ฐ ๊ฐ๋ฌผ๊ฐ๋ฌผ ํด์ง๋ฉด ๋ณด๋ฌ์ค์! /* // Definition for Employee. class Employee { public: int id; int importance; vector subordinates; }; */ class Solution { public: int getImportance(vector employees, int id) { int answer = 0; int index = 0; queue q; q.push(id); while(!q.empty()) { int node = q.front(); // id์ ํด๋นํ๋ ๊ฒ์ด ๋ช ๋ฒ์งธ index.. 2022. 1. 6. LeetCode (Sum of Left Leaves) C++ left leave๋ฅผ ๋ง๋ ๋์ ์กฐ๊ฑด์ ํด๋น node์ left์ right๊ฐ ๋ชจ๋ null ์ธ ๊ฒฝ์ฐ ์ด๋ฏ๋ก ํด๋น ์กฐ๊ฑด์ผ ๋ return ํ๊ณ ์๋ ๊ฒฝ์ฐ๋ ๋ชจ๋ ํ์ํด์ ๋ค์ด๊ฐ๋ฉด ๋๋ค. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(le.. 2022. 1. 6. ์ด์ 1 ๋ค์