From 70034865d4f355ae861054ade7a39f1b235c461b Mon Sep 17 00:00:00 2001 From: DingDing <2295481203@qq.com> Date: Sat, 20 Apr 2019 09:20:39 +0800 Subject: [PATCH 01/13] Create LeetCode1.cpp --- Week_01/id_11/LeetCode1.cpp | 106 ++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 Week_01/id_11/LeetCode1.cpp diff --git a/Week_01/id_11/LeetCode1.cpp b/Week_01/id_11/LeetCode1.cpp new file mode 100644 index 00000000..d94f5264 --- /dev/null +++ b/Week_01/id_11/LeetCode1.cpp @@ -0,0 +1,106 @@ +#ifndef SOLUTION_H +#define SOLUTION_H + +#include +#include + +#include +#include + +#include +using namespace __gnu_cxx; +using namespace std; + +class Solution +{ +public: + Solution(); + + static vector twoSum(int a[], int len, int target); + static vector twoSum2(int a[], int len, int target); + static vector twoSum3(int a[], int len, int target); + + + + +}; + +vector Solution::twoSum(int a[], int len,int target) +{ + //1 traverse the array + vector res; + res.clear(); + for(int i = 0; i < len; ++i) + { + for(int j = i+1; j < len; ++j) + { + if(a[i] + a[j] == target) + { + res.push_back(i); + res.push_back(j); + + return res; + + } + } + } + // + return res; +} + +vector Solution::twoSum2(int a[], int len, int target) +{ + //1 traverse the array + vector res; + res.clear(); + hash_map val2Mark; + + for(int i = 0; i < len; ++i) + { + val2Mark[a[i]] = i; + } + + for(int i = 0; i < len; ++i) + { + auto it = val2Mark.find(target - a[i]); + if(it != val2Mark.end()) + { + res.push_back(i); + res.push_back(it->second); + + return res; + + } + } + + // + return res; +} + +vector Solution::twoSum3(int a[], int len, int target) +{ + //1 traverse the array + vector res; + res.clear(); + + + hash_map val2Mark; + for(int i = 0; i < len; ++i) + { + val2Mark[a[i]] = i; + auto it = val2Mark.find(target - a[i]); + if(it != val2Mark.end()) + { + res.push_back(i); + res.push_back(it->second); + + return res; + + } + } + + // + return res; +} + +#endif // SOLUTION_H From ce980e02b551376ce527e97ae71608853ac62a68 Mon Sep 17 00:00:00 2001 From: DingDing <2295481203@qq.com> Date: Sat, 20 Apr 2019 09:29:26 +0800 Subject: [PATCH 02/13] Update NOTE.md --- Week_01/id_11/NOTE.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Week_01/id_11/NOTE.md b/Week_01/id_11/NOTE.md index c684e62f..8700f7e5 100644 --- a/Week_01/id_11/NOTE.md +++ b/Week_01/id_11/NOTE.md @@ -1 +1,7 @@ -# 学习笔记 \ No newline at end of file +# 学习笔记 + +1.思考过程,直接暴力破解,写出,O(n2)的解决方法,外测循环都是不可省略的,但是我们可以优化内层循环,来提高算法执行效率 +2.内层循环中使用的是继续遍历剩余数组看是否有符合条件的元素,遍历的时间复杂度事O(n),我们可以根据优化索引的效率 +3.我们可以使用hash这种索引效率为O(1)的数据结构来提高算法效率,可以提前遍历一遍数组将数组元素放入到hash_map中,在查找时可以使用,hash根据值查找索引 + 这里的是一共遍历了两遍数组,有没有可能只遍历一遍,可以的 +4.直接将hash_map中的数组在查找循环中存入数据,这样就可以的。 From c0a6bdd36d81e8189265728540a0604581aef0de Mon Sep 17 00:00:00 2001 From: DingDing <2295481203@qq.com> Date: Sat, 20 Apr 2019 10:17:09 +0800 Subject: [PATCH 03/13] Create LeetCode2.cpp --- Week_01/id_11/LeetCode2.cpp | 70 +++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 Week_01/id_11/LeetCode2.cpp diff --git a/Week_01/id_11/LeetCode2.cpp b/Week_01/id_11/LeetCode2.cpp new file mode 100644 index 00000000..c466d31e --- /dev/null +++ b/Week_01/id_11/LeetCode2.cpp @@ -0,0 +1,70 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { + + int s1 = len(l1); + int s2 = len(l2); + + if(s1 < s2) + { + ListNode* temp = l1; + l1 = l2; + l2 = temp; + } + + int add = 0; + ListNode* res = l1; + ListNode* prev = l1; + while(l2 != NULL) + { + int sum = l1->val+ l2->val + add; + add = sum / 10; + l1->val = sum %10; + prev = l1; + l1 = l1->next; + l2 = l2->next; + } + + + while(l1!= NULL) + { + int sum = l1->val + add; + add = sum / 10; + l1->val = sum %10; + prev = l1 ; + l1 = l1->next; + } + + if(add != 0) + { + prev->next = new ListNode(add); + } + + return res; + + } + + + int len(ListNode* l) + { + int size = 0; + ListNode* p = l; + while(p != NULL) + { + p = p->next; + size++; + } + return size; + + } + + +}; From 43620c7f7904823009e9bf95c5bf893647e98deb Mon Sep 17 00:00:00 2001 From: DingDing <2295481203@qq.com> Date: Sat, 20 Apr 2019 14:15:00 +0800 Subject: [PATCH 04/13] Create LeetCode3.cpp --- Week_01/id_11/LeetCode3.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Week_01/id_11/LeetCode3.cpp diff --git a/Week_01/id_11/LeetCode3.cpp b/Week_01/id_11/LeetCode3.cpp new file mode 100644 index 00000000..6fcfacf5 --- /dev/null +++ b/Week_01/id_11/LeetCode3.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int lengthOfLongestSubstring(string s) { + + string result; + + int max = 0; + for(int i = 0; i < s.size(); ++i) + { + char str = s.at(i); + int pos = result.find_first_of(str); + if(pos != string::npos) + result.erase(0,pos+1); + result.push_back(str); + if(result.size() > max) + { + max = result.size(); + } + } + + return max; + } +}; From 16a7db2f4f3db17608f0514a92210bfb202232a5 Mon Sep 17 00:00:00 2001 From: DingDing <2295481203@qq.com> Date: Fri, 26 Apr 2019 20:11:21 +0800 Subject: [PATCH 05/13] Create LeetCode_242.cpp --- Week_02/id_11/LeetCode_242.cpp | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Week_02/id_11/LeetCode_242.cpp diff --git a/Week_02/id_11/LeetCode_242.cpp b/Week_02/id_11/LeetCode_242.cpp new file mode 100644 index 00000000..e3f5f1d5 --- /dev/null +++ b/Week_02/id_11/LeetCode_242.cpp @@ -0,0 +1,61 @@ +#include +#include +using namespace std; +class Solution { +public: + bool isAnagram_1(string s, string t) { + unordered_map maps; + for(int i = 0; i < s.size(); i++) + { + auto it = maps.find (s[i]); + if(it == maps.end()) + { + maps[s[i]] = 0; + continue; + } + maps[s[i]]++; + } + + + unordered_map mapt; + for(int i = 0; i < t.size(); i++) + { + auto it = mapt.find (t[i]); + if(it == mapt.end()) + { + mapt[t[i]] = 0; + continue; + } + mapt[t[i]]++; + } + + + for(auto it =maps.begin(); it != maps.end();++it) + { + auto f = mapt.find(it->first); + if(f == mapt.end() || f->second != it->second) + { + return false; + } + } + + return true; + + } + bool isAnagram_2(string s, string t) { + + int i, x[26] = {0},y[26] = {0}; + for(i = 0; s[i]!='\0';i++) x[s[i] - 'a']++; + for(i = 0; t[i]!='\0';i++) y[t[i] - 'a']++; + for(i = 0; i < 26; i++) + { + if(x[i] != y[i]) + { + return false; + } + } + + return true; + + } +}; From 3b2bdfb4f60cf13ec5b0f9c97c1bcbf7b4b6ee11 Mon Sep 17 00:00:00 2001 From: DingDing <2295481203@qq.com> Date: Fri, 26 Apr 2019 21:16:09 +0800 Subject: [PATCH 06/13] Create LeetCode_692.cpp --- Week_02/id_11/LeetCode_692.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Week_02/id_11/LeetCode_692.cpp diff --git a/Week_02/id_11/LeetCode_692.cpp b/Week_02/id_11/LeetCode_692.cpp new file mode 100644 index 00000000..4106dd7e --- /dev/null +++ b/Week_02/id_11/LeetCode_692.cpp @@ -0,0 +1,29 @@ + + //自定义排序方式,单词出现的次数由高到低排序 +struct intComp { + bool operator() (const pair& lhs, const pair& rhs) const{ + return lhs.first > rhs.first; + } +}; +class Solution { +public: + vector topKFrequent(vector& words, int k) { + vector resVec(k);//存储结果 + map hashMap; + //用于统计各个单词出现的次数,并按照字母顺序排序 + for (auto word : words) { + hashMap[word] += 1; + } + //然后按照单词出现的次数进行排序 + multiset, intComp> mySet; + for (auto &item : hashMap) { + mySet.insert({ item.second, item.first }); + } + //取出前k个 + multiset>::iterator it = mySet.begin(); + for (int i = 0; i < k; ++i, ++it) { + resVec[i] = it->second; + } + return resVec; + } +}; From 7b51108711be274f2a06d1b0254e9097443d3ff8 Mon Sep 17 00:00:00 2001 From: DingDing <2295481203@qq.com> Date: Fri, 26 Apr 2019 22:23:17 +0800 Subject: [PATCH 07/13] Update LeetCode_692.cpp --- Week_02/id_11/LeetCode_692.cpp | 83 ++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/Week_02/id_11/LeetCode_692.cpp b/Week_02/id_11/LeetCode_692.cpp index 4106dd7e..56633817 100644 --- a/Week_02/id_11/LeetCode_692.cpp +++ b/Week_02/id_11/LeetCode_692.cpp @@ -26,4 +26,87 @@ class Solution { } return resVec; } + + vector topKFrequent_1(vector& words, int k) { + + vector resVec(k); + + map hashMap; + + for(auto it = words.begin(); it != words.end(); it++) + { + hashMap[*it]++; + } + + vector> pairVec; + for(auto it = hashMap.begin(); it != hashMap.end();it++) + { + pairVec.push_back(std::make_pair(it->first,it->second)); + } + + QuickSort(pairVec,k); + for(int i = 0; i < k; i++) + { + resVec[i] = pairVec[i].first; + } + + return resVec; + } +private: + void QuickSort(vector >& pairVec,int k) + { + QuickSort_C(pairVec,0,pairVec.size()-1,k); + } + + void QuickSort_C(vector >& pairVec,int s,int t,int k) + { + if(s >= t) + { + return ; + } + + int m = Parition(pairVec,s,t); + + QuickSort_C(pairVec,s,m-1,k); + + QuickSort_C(pairVec,m+1,t,k); + + + } + + int Parition(vector >& pairVec,int s,int t) + { + int pivot = t; + int i = s; // < + int j = s; + + for(;j < t;j++) + { + pair item = pairVec[j]; + if(item.second > pairVec[pivot].second) + { + Swap(pairVec,i,j); + i++; + }else if((item.second == pairVec[pivot].second) + && item.first >& pairVec,int i,int j) + { + pair tmp = pairVec[i]; + pairVec[i] = pairVec[j]; + pairVec[j] = tmp; + } + + + }; From 57653a748504b57145cf1a3cfdcf1455701b1027 Mon Sep 17 00:00:00 2001 From: DingDing <2295481203@qq.com> Date: Sat, 27 Apr 2019 19:30:46 +0800 Subject: [PATCH 08/13] Rename LeetCode_242.cpp to LeetCode_242_011.cpp --- Week_02/id_11/{LeetCode_242.cpp => LeetCode_242_011.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Week_02/id_11/{LeetCode_242.cpp => LeetCode_242_011.cpp} (100%) diff --git a/Week_02/id_11/LeetCode_242.cpp b/Week_02/id_11/LeetCode_242_011.cpp similarity index 100% rename from Week_02/id_11/LeetCode_242.cpp rename to Week_02/id_11/LeetCode_242_011.cpp From 0639a0246e4a6f8c4fd53bddaad37ae35c354329 Mon Sep 17 00:00:00 2001 From: DingDing <2295481203@qq.com> Date: Sat, 27 Apr 2019 19:31:50 +0800 Subject: [PATCH 09/13] Rename LeetCode_692.cpp to LeetCode_692_011.cpp --- Week_02/id_11/{LeetCode_692.cpp => LeetCode_692_011.cpp} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Week_02/id_11/{LeetCode_692.cpp => LeetCode_692_011.cpp} (100%) diff --git a/Week_02/id_11/LeetCode_692.cpp b/Week_02/id_11/LeetCode_692_011.cpp similarity index 100% rename from Week_02/id_11/LeetCode_692.cpp rename to Week_02/id_11/LeetCode_692_011.cpp From 60245fef497e629e36afccb43a66c5531633fdee Mon Sep 17 00:00:00 2001 From: DingDing <2295481203@qq.com> Date: Sun, 5 May 2019 00:05:41 +0800 Subject: [PATCH 10/13] Create LeetCode_997_011.cpp --- Week_03/id_11/LeetCode_997_011.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Week_03/id_11/LeetCode_997_011.cpp diff --git a/Week_03/id_11/LeetCode_997_011.cpp b/Week_03/id_11/LeetCode_997_011.cpp new file mode 100644 index 00000000..0d1d38b7 --- /dev/null +++ b/Week_03/id_11/LeetCode_997_011.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int findJudge(int N, vector>& trust) { + + vector record( N + 1, 0); + + for( int i = 0; i < trust.size(); i++){ + record[ trust[i][0]] = -1; + if( record[trust[i][1]] != -1) + record[trust[i][1]]++; + } + + int max_pos = -1, max_num = -1; + for( int i = 1; i < record.size(); i++){ + // cout< max_num) + max_pos = i, max_num = record[i]; + } + + if( max_num == N - 1) + return max_pos; + return -1; + + } +}; From 30d61604449b46a52ebb7686c3a74d7f19a27dc8 Mon Sep 17 00:00:00 2001 From: DingDing <2295481203@qq.com> Date: Sun, 5 May 2019 00:16:08 +0800 Subject: [PATCH 11/13] Create LeetCode_703_011 --- Week_03/id_11/LeetCode_703_011 | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Week_03/id_11/LeetCode_703_011 diff --git a/Week_03/id_11/LeetCode_703_011 b/Week_03/id_11/LeetCode_703_011 new file mode 100644 index 00000000..c242758e --- /dev/null +++ b/Week_03/id_11/LeetCode_703_011 @@ -0,0 +1,19 @@ +class KthLargest { +public: + int n; + priority_queue,greater> q; //建立最小堆 + + KthLargest(int k, vector& nums) { + n=k; + for(int i=0;i Date: Sun, 12 May 2019 21:03:30 +0800 Subject: [PATCH 12/13] Create LeetCode_720_011 --- Week_04/id_11/LeetCode_720_011 | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Week_04/id_11/LeetCode_720_011 diff --git a/Week_04/id_11/LeetCode_720_011 b/Week_04/id_11/LeetCode_720_011 new file mode 100644 index 00000000..abf2638d --- /dev/null +++ b/Week_04/id_11/LeetCode_720_011 @@ -0,0 +1,29 @@ +class Solution { +public: + string longestWord(vector& words) { + + sort(words.begin(), words.end(), [](string &x, string &y)->bool{return x.length() stringSet; + string res = ""; + for(int i=0; ires.length()) + res = words[i]; + stringSet.insert(words[i]); + + }else{ + if(stringSet.find(words[i].substr(0, words[i].length()-1))!=stringSet.end()){ + stringSet.insert(words[i]); + if(words[i].length()>res.length()) + res = words[i]; + } + } + } + return res; + + } +}; From 12d7f34f9ab91a666d93b0e0781c8ace34ee078c Mon Sep 17 00:00:00 2001 From: DingDing <2295481203@qq.com> Date: Sun, 12 May 2019 21:31:26 +0800 Subject: [PATCH 13/13] Create LeetCode_309_011 --- Week_04/id_11/LeetCode_309_011 | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Week_04/id_11/LeetCode_309_011 diff --git a/Week_04/id_11/LeetCode_309_011 b/Week_04/id_11/LeetCode_309_011 new file mode 100644 index 00000000..7dbb8aab --- /dev/null +++ b/Week_04/id_11/LeetCode_309_011 @@ -0,0 +1,21 @@ +class Solution { +public: + int maxProfit(vector& prices) { + + if(prices.size()==0) return 0; + + int n = prices.size(); + vector> dp(n, vector(3,0)); + dp[0][0] = -prices[0]; //持有 + dp[0][1] = 0; //刚刚卖出 + dp[0][2] = 0; //冷冻期,这之后可以买入 + + for(int i=1; i