본문 바로가기

C

(101)
c++ leetcode 155 Min Stack solution (double stack) code >> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 class MinStack { public: /** initialize your data structure here. */ vector real_vector; vector save_min_vector; MinStack() { } void push(int val) { real_vector.push_back(val); if (save_min_vector.empty() || save_min_vector.back() >= val) save_min_vector.push_back(val); } void pop() { if (real..
c++ leetcode 1249 Minimum Remove to make valid parenthesis 문제풀이 코드 >> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 class Solution { public: bool isPushAble(char c) { if ((c == '{') || (c == '(') || (c == '[')) { return true; } else return false; } set getToBeDeletedIndex(string s) { int sSi..
c++ leet code 20. Valid Parentheses stack solution code >> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 class Solution { public: bool isPushAble(char c) { if ((c == '{') || (c == '(') || (c == '[')) { return true; } else return false; } bool isValid(string s) { int sSize = (int)s.size(); if (sSize == 0 || sSize == 1) return false; unordered_map hash; hash['}'] = ..
c++ leetcode O(N) solution Longest Substring Without repetition code >> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 class Solution { public: int lengthOfLongestSubstring(string s) { int total_size = (int)s.size(); if (total_size == 1) { return 1; } if (total_size == 0) { return 0; } unordered_map char_index; int max_length = 0; int d = 1; int i = 0; char_index[s[i]] = 0; for (i=1; i
c++ leetcode 49 Group Anagrams solutions code CODE >> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Solution { public: vector groupAnagrams(vector& strs) { vector ans; unordered_map dict; int i = 0; for (auto& str : strs) { string tempstring = str; std::sort(tempstring.begin(), tempstring.end()); dict[tempstring].emplace_back(str); } for (auto& vec : dict) { ans.emplace_back(vec.second); } return ans; } }; Colored by Color Scr..
c++ leetcode 415 AddStrings solution 코드 >> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 #include #include #include #include using namespace std; string ..
c++ leetcode 680번 valid palindrome II code >> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 class Solution { public: bool validPalindrome(string s) { int strlen = (int)s.size(); int i = 0; int j = strlen - 1; if (j == 0) return true; while (j >= 0 && i deletedPalindrome(temp1); bool result2 = this->deletedP..
c++ leetcode 125 valid palindrome solution code >> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 class Solution { public: bool isPalindrome(string s) { int i = 0; int strlen = (int)s.size(); int j = strlen - 1; if (j == 0) return true; while (j >= 0 && i = 0))) { j--; if (j = strlen) && (j