From b46db7dd9ec5d69cd5f69eff067002c8d2a1b0ef Mon Sep 17 00:00:00 2001 From: honeyaya Date: Wed, 8 May 2019 22:36:37 +0800 Subject: [PATCH 1/2] 150_week_04 --- Week_04/id_150/Leetcode_169_150.cpp | 52 +++++++++++ Week_04/id_150/Leetcode_241_150.cpp | 136 ++++++++++++++++++++++++++++ Week_04/id_150/Leetcode_746_150.cpp | 20 ++++ 3 files changed, 208 insertions(+) create mode 100644 Week_04/id_150/Leetcode_169_150.cpp create mode 100644 Week_04/id_150/Leetcode_241_150.cpp create mode 100644 Week_04/id_150/Leetcode_746_150.cpp diff --git a/Week_04/id_150/Leetcode_169_150.cpp b/Week_04/id_150/Leetcode_169_150.cpp new file mode 100644 index 00000000..ba47e5c8 --- /dev/null +++ b/Week_04/id_150/Leetcode_169_150.cpp @@ -0,0 +1,52 @@ +Method 1: +class Solution { +public: + int majorityElement(vector& nums) { + sort(nums.begin(), nums.end()); + return nums[nums.size()/2]; + } +}; + + +Method 2: +class Solution { +public: + int majorityElement(vector& nums) { + int num = nums[0]; + int cnt = 1; + for(int i = 1; i < nums.size(); i++){ + if(nums[i] == num) { + cnt++; + } else{ + if(cnt > 0) cnt--; + else{ + num = nums[i]; + cnt = 1; + } + } + } + return num; + } +}; + +Method 3: +class Solution { +public: + int majorityElement(vector& nums) { + vector bits(32,0); + for(int num : nums) { + for(int i = 0; i < 32; i++) { + bits[i] += (num >> (31- i)) & 1; // get the number of 1 + } + } + + int res = 0; + for(int i = 0; i < 32; i++) { + if(bits[i] > (nums.size()/2)) { + res |= (1 << (31 - i)); + } + } + return res; + } +}; + diff --git a/Week_04/id_150/Leetcode_241_150.cpp b/Week_04/id_150/Leetcode_241_150.cpp new file mode 100644 index 00000000..04266efb --- /dev/null +++ b/Week_04/id_150/Leetcode_241_150.cpp @@ -0,0 +1,136 @@ +Method 1: +class Solution { +public: + vector diffWaysToCompute(string input) { + vector res; + + res = dfs(input, 0, input.size()); + + return res; + } + + + vector dfs(string input, int begin, int end) { + vector res; + bool sign = false; + for(int i = begin; i < end; i++) { + if((input[i] == '+') || (input[i] == '-') || (input[i] == '*')) { + sign = true; + break; + } + } + if(!sign) { + int tmp = 0; + string validation = ""; + for(int i = begin; i < end; i++) { + tmp = tmp * 10 + (input[i] - '0'); + validation += input[i]; + } + res.push_back(tmp); + return res; + } + for(int i = begin; i < end; i++) { + if((input[i] == '+') || (input[i] == '-') || (input[i] == '*')) { + vector left = dfs(input, begin, i); + vector right = dfs(input, i + 1, end); + //cout << "left:" << left << " right:" << right << endl; + if(input[i] == '+'){ + for(int le : left) { + for(int ri : right) { + res.push_back(le + ri); + } + } + } + else if(input[i] == '-') { + for(int le : left) { + for(int ri : right) { + res.push_back(le - ri); + } + } + } + else{ + for(int le : left) { + for(int ri : right) { + res.push_back(le * ri); + } + } + } + } + } + + return res; + } +}; + +// 2 * 3 - 4 * 5 +// 2 * 3 - 4 * 5 + +// (2 * 3) - 4 +// 2 * (3 - 4) + +// (2 * 3 - 4) * 5 +// ((2 * 3) - 4) * 5 = 10 +// (2 * (3 - 4)) * 5 = -10 +// 2 * (3 - 4 * 5) +// 2 * ((3 - 4) * 5) = -10 +// 2 * (3 - (4 * 5)) = -34 +// 2 * 3 - (4 * 5) = -14 + +// 2 * 3 - 4 * 5 * 6 + +// (2 * 3 - 4 * 5) * 6 +// 2 * (3 - 4 * 5 * 6) +// 2 * 3 - (4 * 5 * 6) +// 2 * 3 - 4 * (5 * 6) + +Method 2: + +class Solution { + +public: + bool isoperator(char c){ + if(c == '+' || c == '*' || c =='-') + return true; + return false; + } + + vector diffWaysToCompute(string input) { + vector ans; + if(input.size() == 0){ + return ans; + } else{ + for(int i = 0; i < input.length(); i++){ + if(isoperator(input[i])){ + vector l = diffWaysToCompute(input.substr(0,i)); + vector r = diffWaysToCompute(input.substr(i+1)); + + for(int j = 0; j < l.size(); j++){ + for(int k = 0; k < r.size(); k++){ + int a = l[j]; + int b = r[k]; + switch(input[i]){ + case '+':{ + ans.push_back(a+b); + break; + } + case '-':{ + ans.push_back(a-b); + break; + } + case '*':{ + ans.push_back(a*b); + break; + } + } + } + } + } + } + if(ans.empty()){ + ans.push_back(atoi(input.c_str())); + } + return ans; + } + } + +}; diff --git a/Week_04/id_150/Leetcode_746_150.cpp b/Week_04/id_150/Leetcode_746_150.cpp new file mode 100644 index 00000000..ad010000 --- /dev/null +++ b/Week_04/id_150/Leetcode_746_150.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int minCostClimbingStairs(vector& cost) { + vector res(cost.size() + 1, 0); + res[0] = cost[0]; + res[1] = cost[1]; + + for(int i = 2; i < cost.size() + 1; i++) { + if(i == cost.size()) { + res[i] = min(res[i - 1], res[i - 2]); + } else { + res[i] = min(res[i - 1], res[i - 2]) + cost[i]; + } + } + + return res[cost.size()]; + + } + +}; From 3181c701860f1516f27424beaa5f4003c4ab88b5 Mon Sep 17 00:00:00 2001 From: honeyaya Date: Thu, 9 May 2019 22:43:44 +0800 Subject: [PATCH 2/2] 150-w4 --- Week_04/id_150/Leetcode_563_150.cpp | 23 +++++++++++++++++++++++ Week_04/id_150/Leetcodede_455_10.cpp | 23 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 Week_04/id_150/Leetcode_563_150.cpp create mode 100644 Week_04/id_150/Leetcodede_455_10.cpp diff --git a/Week_04/id_150/Leetcode_563_150.cpp b/Week_04/id_150/Leetcode_563_150.cpp new file mode 100644 index 00000000..46b31856 --- /dev/null +++ b/Week_04/id_150/Leetcode_563_150.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int maxProfit(vector& prices, int fee) { + if(prices.size() == 1) + return 0; + + int buy = prices[0]; + int money = 0; + for(int i = 1; i < prices.size(); i++) { + if((prices[i] - buy) > fee) { + cout << prices[i] << " " << buy << " " << endl; + money += prices[i] - buy - fee; + buy = prices[i] - fee; + } else { + if(prices[i] < buy) + buy = prices[i]; + } + } + + return money; + } +}; + diff --git a/Week_04/id_150/Leetcodede_455_10.cpp b/Week_04/id_150/Leetcodede_455_10.cpp new file mode 100644 index 00000000..2a32df96 --- /dev/null +++ b/Week_04/id_150/Leetcodede_455_10.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int findContentChildren(vector& g, vector& s) { + sort(g.begin(), g.end()); + sort(s.begin(), s.end()); + + int gdx = 0, sdx = 0; + + int res = 0; + while((gdx < g.size()) && (sdx < s.size())) { + if(g[gdx] <= s[sdx]) { + res++; + gdx++; + sdx++; + } else { + sdx++; + } + } + + return res; + } +}; +