본문 바로가기

C

(101)
c++ leetcode 525 contiguous array 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 class Solution { public: int findMaxLength(vector& nums) { const int numSize = (int)nums.size(); unordered_map hashMap; int result = 0; hashMap[0].emplace_back(-1); int accum = 0; for (int i = 0; i result) result = diff; } return result; } }; Colored by Color Scripter
c++ leetcode 380 Insert Delete GetRandom o(1) solution code 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 class RandomizedSet { public: /** Initialize your data structure here. */ unordered_map hashmap; vector intarray; RandomizedSet() { } /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ bool insert(int va..
c++ leetcode top-k frequent elements O(nlogn) 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 using namespace std; typedef struct _Point { int first_int; int second_int; } Point; bool integerComp(const Point& p1, const Point& p2) { return p1.second_int > p2.second_int; } class Solution { public: vector topKFrequent(vector& nums, int k) { unordered_map hash..
c++ leetcode top k frequent words (sort and custom comparison) 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 50 51 52 53 using namespace std; typedef struct _Point { int second_int; string first_string; } Point; bool comp(const Point& p1, const Point& p2) { if (p1.second_int > p2.second_int) { return true; } else if (p1.second_int == p2.second_int) { return p..
c++ leetcode 290 word pattern with double hashmap(100% faster) 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 class Solution { public: bool wordPattern(string pattern, string s) { int pSize = (int)pattern.size(); int sSize = (int)s.size(); istringstream ss(s); vector saveVector; string stringBuffer; unordered_map hashmap; unordered_map twohashmap; while (getline(ss, stringBuffer, ' ')) { saveVector..
c++ leetcode 739 daily temperature code 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 using namespace std; class Solution { public: vector dailyTemperatures(vector& temperatures) { stack numSave; int n = temperatures.size(); vector indexes(n, 0); numSave.push({ temperatures[n - 1], n-1 }); for (int i = n - 2; i>=0; i--) { if (temperatures[i] temper..
c++ leet code 227, Basic CalculatorII code 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 using namespace std; class Solution { public: int calculate(string s) { stack num_stack; int answer = 0; int len = (int)s.size(); int currentNumber = 0; char operation = '+'; for (int i = 0; i
c++ leetcode 394 decode_string 풀이 코드 >> 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 using namespace std; class Solution { public: string decodeString(string s) { stack decodeStack; string tmp_num = ""; string tmp_alpha = ""; string tmp_str = ""; string ans; int tmp_int = 0; int sSize = (int)s.size(); int tmp_number = 0; for (int i = 0; i