๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿš“ 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.