diff --git a/Week_02/week02.cpp b/Week_02/week02.cpp new file mode 100644 index 00000000..cbdedc14 --- /dev/null +++ b/Week_02/week02.cpp @@ -0,0 +1,62 @@ +//有效的字母异位词 +class Solution { +public: + bool isAnagram(string s, string t) { + if(t.empty()) { + if(s.empty()) { + return true; + } + else { + return false; + } + } + else { + if(t.size() != s.size()) { + return false; + } + } + + map map_s; + map map_t; + for(auto var : s) { + map_s[var]++; + } + for(auto var : t) { + map_t[var]++; + } + for(auto var : map_s) { + if(map_t.find(var.first) == map_t.end() || + map_t[var.first] != var.second) { + return false; + } + } + return true; + } +}; + +//前 K 个高频元素 +class Solution { +public: + vector topKFrequent(vector& nums, int k) { + map cnt_list; + for(int num : nums) { + cnt_list[num]++; + } + + priority_queue> queue; + for(auto& var : cnt_list) { + pair info; + info.first = var.second; + info.second = var.first; + queue.push(info); + } + + vector res; + for(int i=0; i info = queue.top(); + res.push_back(info.second); + queue.pop(); + } + return res; + } +}; \ No newline at end of file diff --git a/Week_04/lemonadeChange.py b/Week_04/lemonadeChange.py new file mode 100644 index 00000000..cab78117 --- /dev/null +++ b/Week_04/lemonadeChange.py @@ -0,0 +1,26 @@ +class Solution: + def lemonadeChange(self, bills: List[int]) -> bool: + if not bills: + return False + + changes = [] + for num in bills: + if num == 5: + changes.append(num) + else: + rest = num - 5 + changes.sort(reverse = True) + index = [] + for i, change in enumerate(changes): + if rest >= change: + rest -= change + index.append(i) + if rest == 0: + break + if rest == 0: + for j in reversed(index): + changes.pop(j) + changes.append(num) + else: + return False + return True \ No newline at end of file diff --git a/Week_04/searchMatrix.cpp b/Week_04/searchMatrix.cpp new file mode 100644 index 00000000..f8941d65 --- /dev/null +++ b/Week_04/searchMatrix.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + bool searchMatrix(vector>& matrix, int target) { + if(matrix.empty()) { + return false; + } + + int m = matrix.size(); + int n = matrix[0].size(); + int left = 0; + int right = m*n - 1; + while(left <= right) { + int mid = left + (right-left)/2; + int row = mid/n; + int col = mid%n; + if(matrix[row][col] == target) { + return true; + } + else if(matrix[row][col] < target) { + left = mid + 1; + } + else { + right = mid - 1; + } + } + return false; + } +}; \ No newline at end of file diff --git a/Week_06/longestValidParentheses.cpp b/Week_06/longestValidParentheses.cpp new file mode 100644 index 00000000..ce7980b6 --- /dev/null +++ b/Week_06/longestValidParentheses.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int longestValidParentheses(string s) { + int maxans = 0; + stack stk; + stk.push(-1); + for (int i = 0; i < s.length(); i++) { + if (s[i] == '(') { + stk.push(i); + } else { + stk.pop(); + if (stk.empty()) { + stk.push(i); + } else { + maxans = max(maxans, i - stk.top()); + } + } + } + return maxans; + } +}; \ No newline at end of file diff --git a/Week_06/minPathSum.cpp b/Week_06/minPathSum.cpp new file mode 100644 index 00000000..3ae60cce --- /dev/null +++ b/Week_06/minPathSum.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + int minPathSum(vector>& grid) { + int m = grid.size(); + int n = grid[0].size(); + vector> dp; + dp.resize(m); + for(int i=0; i result; + void dfs(string cur_str, int left, int right, int n) { + if(left == n && right == n) { + result.push_back(cur_str); + return; + } + if(left < right) { + return; + } + + if(left < n) { + dfs(cur_str+"(", left+1, right, n); + } + if(right < n) { + dfs(cur_str+")", left, right+1, n); + } + } + + vector generateParenthesis(int n) { + dfs("", 0, 0, n); + return result; + } +}; \ No newline at end of file diff --git a/Week_07/ladderLength.cpp b/Week_07/ladderLength.cpp new file mode 100644 index 00000000..12a4d972 --- /dev/null +++ b/Week_07/ladderLength.cpp @@ -0,0 +1,43 @@ +class Solution { +public: + int ladderLength(string beginWord, string endWord, vector& wordList) { + set wordSet; + for(string& word : wordList) { + wordSet.insert(word); + } + if(wordSet.find(endWord) == wordSet.end()) { + return 0; + } + + deque queue; + queue.push_back(beginWord); + int len = 1; + while(!queue.empty()) { + int size = queue.size(); + for(int i=0; i links; + int num = 26; + bool is_end = false; +}; + +class Trie { +public: + /** Initialize your data structure here. */ + Trie() { + root = new TrieNode(); + } + + /** Inserts a word into the trie. */ + void insert(string word) { + TrieNode *node = root; + int length = word.size(); + for(int i=0; icontainsKey(word[i])) { + TrieNode *_node = new TrieNode(); + node->put(word[i], _node); + } + node = node->get(word[i]); + } + node->setEnd(); + } + + /** Returns if the word is in the trie. */ + bool search(string word) { + TrieNode *node = root; + int length = word.size(); + for(int i=0; icontainsKey(word[i])) { + node = node->get(word[i]); + } + else { + return false; + } + } + return (node && node->isEnd()); + } + + /** Returns if there is any word in the trie that starts with the given prefix. */ + bool startsWith(string prefix) { + TrieNode *node = root; + int length = prefix.size(); + for(int i=0; icontainsKey(prefix[i])) { + node = node->get(prefix[i]); + } + else { + return false; + } + } + return true; + } +private: + TrieNode *root; +}; + +/** + * Your Trie object will be instantiated and called as such: + * Trie* obj = new Trie(); + * obj->insert(word); + * bool param_2 = obj->search(word); + * bool param_3 = obj->startsWith(prefix); + */ \ No newline at end of file diff --git a/Week_08/findCircleNum.cpp b/Week_08/findCircleNum.cpp new file mode 100644 index 00000000..e6ecb36e --- /dev/null +++ b/Week_08/findCircleNum.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int find(vector& parent, int index) { + if (parent[index] != index) { + parent[index] = find(parent, parent[index]); + } + return parent[index]; + } + + void joint(vector& parent, int index1, int index2) { + parent[find(parent, index1)] = find(parent, index2); + } + + int findCircleNum(vector>& isConnected) { + int provinces = isConnected.size(); + vector parent(provinces); + for (int i = 0; i < provinces; i++) { + parent[i] = i; + } + for (int i = 0; i < provinces; i++) { + for (int j = i + 1; j < provinces; j++) { + if (isConnected[i][j] == 1) { + joint(parent, i, j); + } + } + } + int circles = 0; + for (int i = 0; i < provinces; i++) { + if (parent[i] == i) { + circles++; + } + } + return circles; + } +}; \ No newline at end of file diff --git "a/Week_10 \346\257\225\344\270\232\346\200\273\347\273\223/\346\257\225\344\270\232\346\200\273\347\273\223.md" "b/Week_10 \346\257\225\344\270\232\346\200\273\347\273\223/\346\257\225\344\270\232\346\200\273\347\273\223.md" new file mode 100644 index 00000000..c3610b25 --- /dev/null +++ "b/Week_10 \346\257\225\344\270\232\346\200\273\347\273\223/\346\257\225\344\270\232\346\200\273\347\273\223.md" @@ -0,0 +1 @@ +时间过得真快,一转眼这一期的算法训练营就结束了,对于一个不够勤奋的我来说,有遗憾也有收获,遗憾的是刷的题目不够多,有些地方理解的还不够深刻,比如动态规划,需要后续跟进。收获的是算法的系统学习,虽然之前也零零碎碎的接触到一些算法方面的内容,但是不够系统,没有站在一个更高的角度来看待算法,尤其是老师强调计算机处理复杂问题的重复解时的几个特征,还有就是一定要动手,能看懂和能写出来完全是两码事,而且有些题目时间长了自己可能已经忘了当时是怎么做的,这个是正常现象,所以正如老师强调的“五毒神掌”那样要过遍数,归根结底还是要刻意练习,希望在未来的日子里把遗憾补上,做更好的自己! \ No newline at end of file