본문 바로가기

전체 글67

57. Insert Interval class Solution { public: vector insert(vector& vals, vector& val) { int n = vals.size(); int i = 0; vector ans; while (i < n && vals[i][1] < val[0]) { ans.push_back(vals[i]); ++i; } while (i < n && vals[i][0] 2022. 9. 1.
16. 3Sum Closest class Solution { public: int closest(int a, int b, int t) { return abs(t - a) < abs(t - b) ? a : b; } int threeSumClosest(vector& nums, int target) { sort(nums.begin(), nums.end()); int n = nums.size(); int ans = nums[0] + nums[1] + nums[2]; for (int i = 0; i < n - 2; ++i) { int left = i + 1; int right = n - 1; while (left < right) { int t = nums[i] + nums[left] + nums[right]; if (t < target) le.. 2022. 8. 28.
417. Pacific Atlantic Water Flow 1. 모든 셀에 대해 pacific & atlantic 확인하기 : 직관적 / 복잡한 구현. 더보기 class Solution { public: vector mem; int f(vector& H, int r, int c, bool direct=true) { int n = H.size(), m = H[0].size(); if (r == 0 || c == 0) mem[r][c] |= 1; if (r == n - 1 || c == m - 1) mem[r][c] |= 2; if ((mem[r][c] & 3) == 3) return 3; mem[r][c] ^= 4; if (direct) mem[r][c] |= 8; int dir[] = {-1, 0, 1, 0, -1}; for (int i = 0; i < 4; +.. 2022. 8. 26.
173. Binary Search Tree Iterator /** * 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(left), right(right) {} * }; */ class BSTIterator { public: stack mem; void acc(TreeNode* root) { while (ro.. 2022. 8. 24.