文章目录

  • 数组的度(题目编号697:[link](https://leetcode-cn.com/problems/degree-of-an-array/))
  • 二叉搜索树(题目编号700:[link](https://leetcode-cn.com/problems/search-in-a-binary-search-tree/))
  • 数据流中的第K大元素(题目编号703:[link](https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/))
  • 二分查找(题目编号704:[link](https://leetcode-cn.com/problems/binary-search/))
  • 转换成小写字母(题目编号709:[link](https://leetcode-cn.com/problems/to-lower-case/))
  • 1比特与2比特字符(题目编号717:[link](https://leetcode-cn.com/problems/1-bit-and-2-bit-characters/))
  • 图像渲染(题目编号733:[link](https://leetcode-cn.com/problems/flood-fill/))
  • 设计哈希集合(题目编号705:[link](https://leetcode-cn.com/problems/design-hashset/))
  • 建立哈希映射(题目编号706:[link](https://leetcode-cn.com/problems/design-hashmap/))
  • 翻转字符串里的单词(题目编号151:[link](https://leetcode-cn.com/problems/reverse-words-in-a-string/))
  • 面试题 16.07. 最大数值([link](https://leetcode-cn.com/problems/maximum-lcci/))
  • 在线选举(题目编号911:[link](https://leetcode-cn.com/problems/online-election/))
  • 前 K 个高频元素(题目编号347:[link](https://leetcode-cn.com/problems/top-k-frequent-elements/))
  • 形成两个异或相等数组的三元组数目(题目编号1442:[link](https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/))
  • 最大数(题目编号179:[link]())
  • 不同路径(题目编号62:[link](https://leetcode-cn.com/problems/unique-paths/))
  • 不同路径II(题目编号63:[link](https://leetcode-cn.com/problems/unique-paths-ii/))
  • 组合(题目编号77:[link](https://leetcode-cn.com/problems/combinations/))
  • 单词搜索(题目编号79:[link](https://leetcode-cn.com/problems/word-search/))
  • 找到K个最接近的元素(题目编号658:[link](https://leetcode-cn.com/problems/find-k-closest-elements/))
  • 目标和(题目编号494:[link](https://leetcode-cn.com/problems/target-sum/),2021.7.7)
  • 随机数索引(题目编号398:[link](https://leetcode-cn.com/problems/random-pick-index/),2021.7.7)
  • 两数相加II(题目编号445:[link](https://leetcode-cn.com/problems/add-two-numbers-ii/))
  • 移动所有球到每个盒子所需的最小操作数(题目编号1769:[link](https://leetcode-cn.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/))
  • 字母板上的路径(题目编号1138:[link](https://leetcode-cn.com/problems/alphabet-board-path/))
  • 索引处的解码字符串(题目编号880:[link](https://leetcode-cn.com/problems/decoded-string-at-index/))
  • 安排电影院座位(题目编号1386:[link](https://leetcode-cn.com/problems/cinema-seat-allocation/))
  • 统计二叉树中好节点的数目(题目编号1448:[link](https://leetcode-cn.com/problems/count-good-nodes-in-binary-tree/))
  • 飞机座分配概率(题目编号1227:[link](https://leetcode-cn.com/problems/airplane-seat-assignment-probability/))
  • 连续的子数组和(题目编号523:[link](https://leetcode-cn.com/problems/continuous-subarray-sum/))
  • 整数拆分(题目编号343:[link](https://leetcode-cn.com/problems/integer-break/))
  • 最少侧跳次数(题目编号1824:[link](https://leetcode-cn.com/problems/minimum-sideway-jumps/),2021.7.28)
  • 重新安排行程(题目编号332:[link](https://leetcode-cn.com/problems/reconstruct-itinerary/),2021.7.27)
  • 将二叉搜索树变平衡(题目编号1382:[link](https://leetcode-cn.com/problems/balance-a-binary-search-tree/),2021.7.28)
  • 课程表IV(题目编号1462:[link](https://leetcode-cn.com/problems/course-schedule-iv/),2021.7.28)
  • 具有所有最深节点的最小子树(题目编号865:[link](https://leetcode-cn.com/problems/smallest-subtree-with-all-the-deepest-nodes/),2021.7.28)
  • 两个字符串的删除操作(题目编号583:[link](https://leetcode-cn.com/problems/delete-operation-for-two-strings/),2021.7.28)
  • 剑指Offer II020 回文字符串的个数(链接:[link](https://leetcode-cn.com/problems/a7VOhD/),2021.8.22)
  • 剑指Offer II没有重复元素集合的全排列(题目编号083,链接:[link](https://leetcode-cn.com/problems/VvJkup/))
  • 多次搜索(题目编号:面试题17.17,[link](https://leetcode-cn.com/problems/multi-search-lcci/),2021.8.22)
  • 堆盘子(题目编号:面试题03.03,[link](https://leetcode-cn.com/problems/stack-of-plates-lcci/))
  • 实现一个魔法字典(题目编号676:[link](https://leetcode-cn.com/problems/implement-magic-dictionary/),2021.8.22)
  • 从英文中重建数字(题目编号:423:[link](https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/),2021.8.27)
  • 零钱兑换II(题目编号518:[link](https://leetcode-cn.com/problems/coin-change-2/),2021.8.27)
  • 剑指Offer II 085. 生成匹配的括号(链接:[link](https://leetcode-cn.com/problems/IDBivT/),2021.8.27)
  • 吃苹果的最大数目(题目编号1705:[link](https://leetcode-cn.com/problems/maximum-number-of-eaten-apples/),2021.8.27)
  • 剑指Offer II 046. 二叉树的右侧视图(链接:[link](https://leetcode-cn.com/problems/WNC0Lk/),2021.8.27)
  • 网络延迟时间(题目编号743:[link](https://leetcode-cn.com/problems/network-delay-time/),2021.8.27)
  • 插入、删除和随机访问都是 O(1) 的容器(题目编号:剑指offerII 面试题30,[link](https://leetcode-cn.com/problems/FortPu/))
  • 获取好友已观看的视频(题目编号1311:[link](https://leetcode-cn.com/problems/get-watched-videos-by-your-friends/))
  • 子字符串突变后可能得到的最大整数(题目编号1946:[link](https://leetcode-cn.com/problems/largest-number-after-mutating-substring/))
  • 解码异或后的排列(题目编号1734:[link](https://leetcode-cn.com/problems/decode-xored-permutation/),2021.9.26)
  • 二叉搜索树与双向链表(题目编号:剑指offer36,[link](https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/submissions/),2021.9.26)
  • 最长连续序列(题目编号:剑指Offer II 119,[link](https://leetcode-cn.com/problems/WhsWhI/),2021.9.26)
  • 扣分后的最大得分([link](https://leetcode-cn.com/problems/maximum-number-of-points-with-cost/),2021.9.29)
  • 火柴拼正方形(题目编号473:[link](https://leetcode-cn.com/problems/matchsticks-to-square/),2021.9.29)
  • 设计地铁系统(题目编号1396:[link](https://leetcode-cn.com/problems/design-underground-system/),2021.9.29)
  • 爱吃香蕉的珂珂(题目编号875:[link](https://leetcode-cn.com/problems/koko-eating-bananas/),2021.10.6)
  • 单词长度的最大乘积(题目编号:剑指Offer II 005,[link](https://leetcode-cn.com/problems/aseY1I/),2021.10.6)
  • 将字符串拆成递减的连续值(题目编号:1849,[link](),2021.10.6)
  • 删除最短的子数组使剩余数组有序(题目编号1574:[link](https://leetcode-cn.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/),2021.10.23)
  • 所有子集(题目编号:剑指Offer II 079,[link](https://leetcode-cn.com/problems/TVdhkn/),2021.10.23)
  • 到达目的地的方案数(题目编号:1976,[link](),2021.10.23)

此前已完成137道简单题目。

  1. 数组的度(题目编号697:link)

#include<unordered_map>
class Solution {
public:int findShortestSubArray(vector<int>& nums) {unordered_map<int, int> map_pos, map_count;for(int i = 0; i < nums.size(); i++){if(map_pos.find(nums[i]) == map_pos.end()){map_pos[nums[i]] = i;map_count[nums[i]] = 1;}else{map_count[nums[i]]++;}}int degree = 0;vector<int> target_nums;for(unordered_map<int, int>::iterator it = map_count.begin(); it != map_count.end(); it++){if(it->second == degree){target_nums.push_back(it->first);}else if(it->second > degree){target_nums.clear();target_nums.push_back(it->first);degree = it->second;}}unordered_map<int, int> map_pos_inverse;for(int i = nums.size() - 1; i >= 0; i--){if(find(target_nums.begin(), target_nums.end(), nums[i]) == target_nums.end()) continue;if(map_pos_inverse.find(nums[i]) == map_pos_inverse.end()){map_pos_inverse[nums[i]] = i;}}int min_length = nums.size();for(int i = 0; i < target_nums.size(); i++){min_length = min(min_length, map_pos_inverse[target_nums[i]] - map_pos[target_nums[i]] + 1);}return min_length;}
};
  1. 二叉搜索树(题目编号700:link)

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}* };*/
class Solution {
public:TreeNode* searchBST(TreeNode* root, int val) {if(root == NULL) return NULL;if(root->val == val) return root;if(root->val < val) return searchBST(root->right, val);return searchBST(root->left, val);}
};
  1. 数据流中的第K大元素(题目编号703:link)

class KthLargest {
public:stack<int> s1, s2;int K = 0;KthLargest(int k, vector<int>& nums) {this->K = k;for (int i = 0; i < nums.size(); i++) {if (s1.size() == 0) {s1.push(nums[i]);}else {this->add(nums[i]);}}}int add(int val) {if (s1.size() == this->K) {if (val <= s1.top()) return s1.top();s1.pop();}while (s1.size() > 0 && s1.top() < val) {s2.push(s1.top());s1.pop();}s1.push(val);while (s2.size() > 0) {s1.push(s2.top());s2.pop();}return s1.top();}
};
class KthLargest {
public:multiset<int> topK;int K = 0;KthLargest(int k, vector<int>& nums) {this->K = k;for(int num: nums){add(num);}}int add(int val) {this->topK.insert(val);if(this->topK.size() > this->K){this->topK.erase(this->topK.begin());}return *(this->topK).begin();}
};
class KthLargest {
public:priority_queue<int, vector<int>, greater<int>> pq;int K;KthLargest(int k, vector<int>& nums) {K = k;for(int i = 0; i < nums.size(); i++){add(nums[i]);}}int add(int val) {pq.push(val);if(pq.size() > K){pq.pop();}return pq.top();}
};/*** Your KthLargest object will be instantiated and called as such:* KthLargest* obj = new KthLargest(k, nums);* int param_1 = obj->add(val);*/
  1. 二分查找(题目编号704:link)

class Solution {
public:int search(vector<int>& nums, int target) {int left = 0, right = nums.size() - 1, mid;while(left <= right){mid = left + (right - left) / 2;if(nums[mid] == target){return mid;}if(nums[mid] < target){left = mid + 1;}else{right = mid - 1;}}return -1;}
};
  1. 转换成小写字母(题目编号709:link)

class Solution {
public:string toLowerCase(string str) {int dif_lower, dif_higher;for(int i = 0; i < str.size(); ++i){dif_lower = str[i] - 'a';dif_higher = str[i] - 'A';if(dif_lower >= 0 && dif_lower <= 26){continue;}else if(dif_higher >= 0 && dif_higher <= 26){str[i] = 'a' + dif_higher;}}return str;}
};
  1. 1比特与2比特字符(题目编号717:link)

class Solution {
public:bool isOneBitCharacter(vector<int>& bits) {if(bits.size() == 0) return false;bool flag;int idx = 0;while(idx <= bits.size() - 1){if(bits[idx] == 0){flag = true;++idx;}else{flag = false;idx += 2;}}return flag;}
};
  1. 图像渲染(题目编号733:link)

class Solution {
public:vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {vector<vector<int>> visited(image.size(), vector<int>(image[0].size()));queue<vector<int>> q;vector<int> point;point.push_back(sr);point.push_back(sc);q.push(point);int old_color = image[sr][sc], x, y;while(q.size() > 0){point = q.front();q.pop();x = point[0];y = point[1];check_color(x - 1, y, image, old_color, visited, q, newColor);check_color(x + 1, y, image, old_color, visited, q, newColor);check_color(x, y - 1, image, old_color, visited, q, newColor);check_color(x, y + 1, image, old_color, visited, q, newColor);}return image;}bool check_coor(int x, int y, int size_x, int size_y){if(x >= 0 && x < size_x && y >= 0  && y < size_y){return true;}return false;}void check_color(int x, int y, vector<vector<int>>& image, int old_color, vector<vector<int>>& visited, queue<vector<int>>& q, int newColor){if(check_coor(x, y, image.size(), image[0].size()) && (!visited[x][y]) && image[x][y] == old_color){visited[x][y] = 1;vector<int> point;point.push_back(x);point.push_back(y);q.push(point);image[x][y] = newColor;}}
};
  1. 设计哈希集合(题目编号705:link)

class MyHashSet {
private:class Node {public:Node(int value){val = value;next = NULL;}int val;Node* next;};public:/** Initialize your data structure here. */int bucket_num = 769;vector<Node*> buckets;MyHashSet() {buckets = vector<Node*>(bucket_num, NULL);buckets.clear();}int _hash(int value){return value % bucket_num;}void add(int key) {int bucket_id = _hash(key);Node* bucket_head = buckets[bucket_id];if(bucket_head == NULL){buckets[bucket_id] = new Node(key);}else{while(bucket_head != NULL){if(bucket_head->val == key){return;}if(bucket_head->next == NULL){break;}bucket_head = bucket_head->next;}bucket_head->next = new Node(key);}}void remove(int key) {int bucket_id = _hash(key);Node* bucket_head = buckets[bucket_id];if(bucket_head != NULL){if(bucket_head->val == key){Node* to_delete = bucket_head;buckets[bucket_id] = bucket_head->next;delete to_delete;}else{while(bucket_head->next != NULL && bucket_head->next->val != key){bucket_head = bucket_head->next;}if(bucket_head->next != NULL){Node* to_delete = bucket_head->next;bucket_head->next = bucket_head->next->next;delete to_delete;}}}}/** Returns true if this set contains the specified element */bool contains(int key) {int bucket_id = _hash(key);Node* bucket_head = buckets[bucket_id];if(bucket_head == NULL){return false;}else{while(bucket_head != NULL){if(bucket_head->val == key){return true;}bucket_head = bucket_head->next;}return false;}}};/*** Your MyHashSet object will be instantiated and called as such:* MyHashSet* obj = new MyHashSet();* obj->add(key);* obj->remove(key);* bool param_3 = obj->contains(key);*/
  1. 建立哈希映射(题目编号706:link)

class MyHashMap {
private:class Node {public:Node(int value){val = value;next = NULL;}int val;Node* next;};public:/** Initialize your data structure here. */int bucket_num = 769;vector<Node*> buckets;vector<Node*> buckets_value; MyHashMap() {buckets = vector<Node*>(bucket_num, NULL);buckets_value = vector<Node*>(bucket_num, NULL);        }int _hash(int value){return value % bucket_num;}/** value will always be non-negative. */void put(int key, int value) {int bucket_id = _hash(key);Node* bucket_head = buckets[bucket_id];Node* bucket_value_head = buckets_value[bucket_id];if(bucket_head == NULL){buckets[bucket_id] = new Node(key);buckets_value[bucket_id] = new Node(value);}else{while(bucket_head != NULL){if(bucket_head->val == key){bucket_value_head->val = value;return;}if(bucket_head->next == NULL){break;}bucket_head = bucket_head->next;bucket_value_head = bucket_value_head->next;}bucket_head->next = new Node(key);bucket_value_head->next = new Node(value);}}/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */int get(int key) {int bucket_id = _hash(key);Node* bucket_head = buckets[bucket_id];Node* bucket_value_head = buckets_value[bucket_id];if(bucket_head == NULL){return -1;}else{while(bucket_head != NULL){if(bucket_head->val == key){return bucket_value_head->val;}bucket_head = bucket_head->next;bucket_value_head = bucket_value_head->next;}return -1;}}/** Removes the mapping of the specified value key if this map contains a mapping for the key */void remove(int key) {int bucket_id = _hash(key);Node* bucket_head = buckets[bucket_id];Node* bucket_value_head = buckets_value[bucket_id];if(bucket_head != NULL){if(bucket_head->val == key){Node* to_delete = bucket_head;buckets[bucket_id] = bucket_head->next;delete to_delete;to_delete = bucket_value_head;buckets_value[bucket_id] = bucket_value_head->next;delete to_delete;}else{while(bucket_head->next != NULL && bucket_head->next->val != key){bucket_head = bucket_head->next;bucket_value_head = bucket_value_head->next;}if(bucket_head->next != NULL){Node* to_delete = bucket_head->next;bucket_head->next = bucket_head->next->next;delete to_delete;to_delete = bucket_value_head->next;bucket_value_head->next = bucket_value_head->next->next;delete to_delete;}}}}};/*** Your MyHashMap object will be instantiated and called as such:* MyHashMap* obj = new MyHashMap();* obj->put(key,value);* int param_2 = obj->get(key);* obj->remove(key);*/
  1. 翻转字符串里的单词(题目编号151:link)

class Solution {
public:string reverseWords(string s) {int start = -1, len = 0;string res;for(int i = 0; i < s.size(); ++i){if(start == -1){if(s[i] != ' '){start = i;}}else{if(s[i] == ' '){reverse(s, start, i - 1);copy(s, start, i - start, len);len += i - start;s[len] = ' ';++len;start = -1;}}}if(start != -1){reverse(s, start, s.size() - 1);copy(s, start, s.size() - start, len);len += s.size() - start;}else{--len;}reverse(s, 0, len - 1);return s.substr(0, len);}void reverse(string& s, int i, int j){while(i < j){char c = s[i];s[i] = s[j];s[j] = c;++i;--j;}}void copy(string& s, int src, int len, int dest){if(src == dest){return;}while(len > 0){s[dest] = s[src];++dest;++src;--len;}}
};
  1. 面试题 16.07. 最大数值(link)

class Solution {
public:int maximum(int a, int b) {long c = a, d = b, k = 1 + ((c - d) >> 63);return k * a + !k * b;}
};
  1. 在线选举(题目编号911:link)

class TopVotedCandidate {
public:vector<vector<int>> result;TopVotedCandidate(vector<int>& persons, vector<int>& times) {unordered_map<int, int> mp;int count_of_winner = 0, winner = -1;for(int i = 0; i < persons.size(); ++i){++mp[persons[i]];if(mp[persons[i]] >= count_of_winner){if(persons[i] != winner){result.push_back(vector<int>{persons[i], times[i]});}count_of_winner = mp[persons[i]];winner = persons[i];}}}int q(int t) {if(result.size() == 0) return -1;int lo = 1, hi = result.size();while(lo < hi){int mid = lo + (hi - lo) / 2;if(result[mid][1] <= t){lo = mid + 1;}else{hi = mid;}}return result[lo - 1][0];}
};/*** Your TopVotedCandidate object will be instantiated and called as such:* TopVotedCandidate* obj = new TopVotedCandidate(persons, times);* int param_1 = obj->q(t);*/
  1. 前 K 个高频元素(题目编号347:link)

class Solution {
public:static bool cmp(pair<int, int> p, pair<int, int> q){return p.second > q.second;}vector<int> topKFrequent(vector<int>& nums, int k) {unordered_map<int, int> mp;for(int n: nums){mp[n]++;}priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(&cmp)> q(cmp);for(auto& [num, count]: mp){if(q.size() == k){if(q.top().second < count){q.pop();q.emplace(num, count);}}else{q.emplace(num, count);}}vector<int> res;while(!q.empty()){res.emplace_back(q.top().first);q.pop();}return res;}
};
  1. 形成两个异或相等数组的三元组数目(题目编号1442:link)

class Solution {
public:int countTriplets(vector<int>& arr) {int ans = 0;for(int i = 0; i < arr.size() - 1; ++i){int x = arr[i];for(int k = i + 1; k < arr.size(); ++k){x ^= arr[k];if(x == 0){ans += k - i;}}}return ans;}
};
class Solution {
public:int countTriplets(vector<int>& arr) {unordered_map<int, int> cnt = {{0, 1}}, indsum = {{0, 0}};int x = 0, ans = 0;for(int k = 0; k < arr.size(); ++k){x ^= arr[k];if(cnt[x] > 0){ans += cnt[x] * k - indsum[x];}++cnt[x];indsum[x] += (k + 1);}return ans;}
};
  1. 最大数(题目编号179:link)

  • 思路很简单,自定义比较函数,数字两两比较谁放在前边组成的数字大。
  • 非常需要注意的是:比较函数里得是return s1>s2,如果写成s1>=s2,就会报heap overflow,堆溢出错误。
class Solution {
public:string largestNumber(vector<int>& nums) {if(nums.size() == 0) return "0";sort(nums.begin(), nums.end(), cmp);string ans;if(nums[0] == 0) return "0";for(int n: nums){ans += to_string(n);}return ans;}static int cmp(int n1, int n2){string sa = to_string(n1);string sb = to_string(n2);string s1 = sa + sb;string s2 = sb + sa;return s1 > s2;}
};
  1. 不同路径(题目编号62:link)

class Solution {
public:int uniquePaths(int m, int n) {vector<vector<int>> f(m, vector<int>(n));f[0][0] = 1;for(int i = 1; i < m; ++i){f[i][0] = 1;}for(int i = 1; i < n; ++i){f[0][i] = 1;}for(int i = 1; i < m; ++i){for(int j = 1; j < n; ++j){f[i][j] = f[i - 1][j] + f[i][j - 1];}}return f[m - 1][n - 1];// return func(m, n);}int func(int m, int n){if(m == 1 && n == 1){return 1;}if(m == 1 || n == 1){return 1;}return func(m - 1, n) + func(m, n - 1);}
};
  1. 不同路径II(题目编号63:link)

class Solution {
public:int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {int m = obstacleGrid.size(), n = obstacleGrid[0].size();vector<vector<int>> f(m, vector<int>(n));if(obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1){return 0;}f[0][0] = 1;for(int i = 1; i < m; ++i){if(obstacleGrid[i][0] == 1){break;}f[i][0] = 1;}for(int i = 1; i < n; ++i){if(obstacleGrid[0][i] == 1){break;}f[0][i] = 1;}for(int i = 1; i < m; ++i){for(int j = 1; j < n; ++j){if(obstacleGrid[i][j] == 0){f[i][j] = f[i - 1][j] + f[i][j - 1];}}}return f[m - 1][n - 1];}
};
  1. 组合(题目编号77:link)

class Solution {
public:vector<vector<int>> combine(int n, int k) {if(k < 0 || k > n){return vector<vector<int>>{};}if(n == k){vector<int> ans = vector<int>(n);for(int i = 0; i < n; ++i){ans[i] = i + 1;}return vector<vector<int>>{ans};}// vector<vector<int>> ans = combine(n - 1, k);// vector<vector<int>> ans2 = combine(n - 1, k - 1);// for(int i = 0; i < ans2.size(); ++i){//     vector<int> tmp = ans2[i];//     tmp.push_back(n);//     ans.push_back(tmp);// }// return ans;vector<vector<int>> res;vector<int> visited(n);selection(n, k, 0, 0, visited, res);return res;}void selection(int& n, int& k, int used, int cur_idx, vector<int>& visited, vector<vector<int>>& res){if(used == k){vector<int> tmp(k);for(int i = 0; i < visited.size() && used > 0; ++i){if(visited[i]){tmp[k - used] = i + 1;--used;}}res.push_back(tmp);return;}if(n - cur_idx < k - used){return;}visited[cur_idx] = 1;++used;selection(n, k, used, cur_idx + 1, visited, res);visited[cur_idx] = 0;--used;selection(n, k, used, cur_idx + 1, visited, res);}};
  1. 单词搜索(题目编号79:link)

class Solution {
public:bool exist(vector<vector<char>>& board, string word) {int rows = board.size(), cols = board[0].size();if(rows * cols < word.size()){return false;}vector<int> cnt_arr(52);for(int i = 0; i < rows; ++i){for(int j = 0; j < cols; ++j){if(board[i][j] >= 'a' && board[i][j] <= 'z'){cnt_arr[]}}}return search(board, word, vector<vector<int>>(rows, vector<int>(cols)), 0, 0, 0, rows, cols);}bool search(vector<vector<char>>& board, string& word, vector<vector<int>> visited, int cur_idx, int cur_row, int cur_col, int rows, int cols){if(cur_idx == word.size()){return true;}if(cur_row < 0 || cur_row >= rows || cur_col < 0 || cur_col >= cols){return false;}if(cur_idx == 0){for(int i = 0; i < rows; ++i){for(int j = 0; j < cols; ++j){if(board[i][j] == word[0]){visited[i][j] = 1;bool ans = search(board, word, visited, 1, i+1, j, rows, cols) || search(board, word, visited, 1, i-1, j, rows, cols) || search(board, word, visited, 1, i, j+1, rows, cols) || search(board, word, visited, 1, i, j-1, rows, cols);if(ans){return ans;}visited[i][j] = 0;}}}}else{if(visited[cur_row][cur_col] || board[cur_row][cur_col] != word[cur_idx]){return false;}visited[cur_row][cur_col] = 1;int i = cur_row, j = cur_col;return search(board, word, visited, cur_idx + 1, i+1, j, rows, cols) || search(board, word, visited, cur_idx + 1, i-1, j, rows, cols) || search(board, word, visited, cur_idx + 1, i, j+1, rows, cols) || search(board, word, visited, cur_idx + 1, i, j-1, rows, cols);}return false;}
};
  1. 找到K个最接近的元素(题目编号658:link)

priority<pair<int,int>,vector<pair<int,int>>, cmp> q;
struct cmp{bool operator()(pair<int,in> a, pair<int,int> b){return a.second > b.second;}
}
class Solution {
public:vector<int> findClosestElements(vector<int>& arr, int k, int x) {// priority_queue<int> max_heap;// priority_queue<int, vector<int>, greater<int>> min_heap;// for(int n: arr){//     if(n >= x){//         max_heap.push(n);//         if(max_heap.size() > k){//             max_heap.pop();//         }//     }//     else{//         min_heap.push(n);//         if(min_heap.size() > k){//             min_heap.pop();//         }//     }// }priority_queue<pair<int, int>, vector<pair<int, int>>, cmp1> q;for(int n: arr){q.push(pair<int, int>(n, abs(n - x)));if(q.size() > k){q.pop();}}vector<int> ans1, ans2;while(!q.empty()){auto c = q.top();if(c.first >= x){ans1.push_back(c.first);}else{ans2.push_back(c.first);}q.pop();}vector<int> ans;for(int i = 0; i < ans2.size(); ++i){ans.push_back(ans2[i]);}for(int i = ans1.size() - 1; i >= 0; --i){ans.push_back(ans1[i]);}return ans;}static bool cmp(pair<int, int> a, pair<int, int> b){return a.second > b.second;}struct cmp1{bool operator()(pair<int, int> a, pair<int, int> b){if(a.second == b.second){return a.first <= b.first;}else{return a.second < b.second;}}};
};
#include<algorithm>
class Solution {
public:vector<int> findClosestElements(vector<int>& arr, int k, int x) {int pos = bsearch(arr, x);pos = min(pos, int(arr.size() - 1));vector<int> ans1, ans2;if(pos + 1 < arr.size() && abs(arr[pos + 1] - x) < abs(arr[pos] - x)){ans1.push_back(arr[pos + 1]);++pos;}else if(pos - 1 >= 0 && abs(arr[pos - 1] - x) <= abs(arr[pos] - x)){ans2.push_back(arr[pos - 1]);--pos;}else{ans2.push_back(arr[pos]);}int lo = pos - 1, hi = pos + 1;while(ans1.size() + ans2.size() < k && (lo >= 0 || hi < arr.size())){if(lo >= 0 && hi < arr.size()){if(abs(x - arr[lo]) <= abs(arr[hi] - x)){ans2.push_back(arr[lo]);--lo;}else{ans1.push_back(arr[hi]);++hi;}}else if(lo >= 0){ans2.push_back(arr[lo]);--lo;}else{ans1.push_back(arr[hi]);++hi;}}vector<int> ans;for(int i = ans2.size() - 1; i >= 0; --i){ans.push_back(ans2[i]);}for(int i = 0; i < ans1.size(); ++i){ans.push_back(ans1[i]);}return ans;}int bsearch(vector<int> arr, int target){int lo = 0, hi = arr.size() - 1;while(lo <= hi){int mid = lo + (hi - lo) / 2;if(arr[mid] == target){return mid;}else if(arr[mid] < target){lo = mid + 1;}else{hi = mid - 1;}}return lo;}};
  1. 目标和(题目编号494:link,2021.7.7)

class Solution {
public:int findTargetSumWays(vector<int>& nums, int target) {int sum = 0;for(int i = 0; i < nums.size(); ++i){sum += nums[i];}if(target + sum < 0 || ((target + sum) & 1) == 1) return 0;int T = (sum + target) >> 1;vector<int> pre = vector<int>(T + 1);pre[0] = 1;for(int i = 1; i <= nums.size(); ++i){vector<int> cur(T + 1);for(int j = 0; j <= T; ++j){cur[j] = pre[j];if(j - nums[i - 1] >= 0){cur[j] += pre[j - nums[i - 1]];}}pre = cur;}return pre[T];}
};
  1. 随机数索引(题目编号398:link,2021.7.7)

class Solution {
private:vector<int> nums;
public:Solution(vector<int>& nums) {this->nums = nums;}int pick(int target) {int cnt = 0, ind = -1;for(int i = 0; i < this->nums.size(); ++i){if(nums[i] == target){++cnt;if(rand() % cnt == 0){ind = i;}}}return ind;}
};/*** Your Solution object will be instantiated and called as such:* Solution* obj = new Solution(nums);* int param_1 = obj->pick(target);*/
  1. 两数相加II(题目编号445:link)

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {if(!l1){return l2;}if(!l2){return l1;}ListNode* r1 = reverseLinkList(l1);ListNode* r1_bak = r1;ListNode* r2 = reverseLinkList(l2);int carry = 0;ListNode* last;while(r1 || r2){int res = (r1? r1->val: 0) + (r2? r2->val:0) + carry;if(r1){r1->val = res % 10;}else{last->next = new ListNode(res % 10);r1 = last->next;}carry = res / 10;last = r1;if(r1) r1 = r1->next;if(r2) r2 = r2->next;}if(carry){last->next = new ListNode(carry);}return reverseLinkList(r1_bak);}ListNode* reverseLinkList(ListNode* head){if(!head) return head;ListNode *pre = nullptr, *cur = head, *next;while(cur){next = cur->next;cur->next = pre;pre = cur;cur = next;}return pre;}
};
  1. 移动所有球到每个盒子所需的最小操作数(题目编号1769:link)

class Solution {
public:vector<int> minOperations(string boxes) {int presum, precost, n = boxes.size();vector<int> ans = vector<int>(n), lsum = vector<int>(n), rsum = vector<int>(n), lcost = vector<int>(n), rcost = vector<int>(n);for(int i = 0; i < n; ++i){presum = (i == 0) ? 0: lsum[i - 1];precost = (i <= 1) ? 0: lcost[i - 1];if(i > 0 && boxes[i - 1] == '1'){lsum[i] = presum + 1;}else{lsum[i] = presum;}lcost[i] = precost + lsum[i];ans[i] += lcost[i];// reversepresum = (i == 0) ? 0: rsum[n - 1 - (i - 1)];precost = (i <= 1) ? 0: rcost[n - 1 - (i - 1)];if(i > 0 && boxes[n - 1 - (i - 1)] == '1'){rsum[n - 1 - i] = presum + 1;}else{rsum[n - 1 - i] = presum;}rcost[n - 1 - i] = precost + rsum[n - 1 - i];ans[n - 1 - i] += rcost[n - 1 - i];}return ans;}
};
  1. 字母板上的路径(题目编号1138:link)

class Solution {
public:string alphabetBoardPath(string target) {if(target.size() == 0) return "";vector<string> alphabet = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"};string ans;find_next(target, ans, 0, 0, 0);return ans;}void find_next(string target, string& ans, int ind, int x, int y){if(ind == target.size()) return;int sx = int(target[ind] - 'a') / 5, sy = int(target[ind] - 'a') % 5;string vertical = (sx >= x)? "D":"U", horizontal = (sy >= y)? "R":"L";if(x == 5){append_char(ans, abs(sx - x), vertical);append_char(ans, abs(sy - y), horizontal);}else{append_char(ans, abs(sy - y), horizontal);append_char(ans, abs(sx - x), vertical);}ans += "!";find_next(target, ans, ind + 1, sx, sy);}void append_char(string& ans, int num, string s){for(int i = 0; i < num; ++i){ans += s;}}
};
  1. 索引处的解码字符串(题目编号880:link)

  1. 安排电影院座位(题目编号1386:link)

class Solution {
public:int maxNumberOfFamilies(int n, vector<vector<int>>& reservedSeats) {unordered_map<int, int> mp;for(auto v: reservedSeats){if(v[1] == 1 || v[1] == 10) continue;mp[v[0]] |= (1 << (v[1] - 2));} int ans = 2 * (n - mp.size()), left = 0b00001111, middle = 0b11000011, right = 0b11110000;for(unordered_map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it){if((it->second | left) == left || (it->second | middle) == middle || (it->second | right) == right){++ans;}}return ans;}
};
  1. 统计二叉树中好节点的数目(题目编号1448:link)

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:int goodNodes(TreeNode* root) {int ans = 0;midTraverse(root, ans, INT_MIN);return ans;}void midTraverse(TreeNode* root, int& ans, int maxVal){if(!root) return;if(root->val >= maxVal) ++ans;maxVal = max(maxVal, root->val);midTraverse(root->left, ans, maxVal);midTraverse(root->right, ans, maxVal);}
};
  1. 飞机座分配概率(题目编号1227:link)

class Solution {
public:double nthPersonGetsNthSeat(int n) {return n == 1 ? 1:0.5;}
};
  1. 连续的子数组和(题目编号523:link)

class Solution {
public:bool checkSubarraySum(vector<int>& nums, int k) {int n = nums.size();if(n <= 1) return false;vector<int> prefixSum(n);prefixSum[0] = nums[0] % k;unordered_map<int, int> mp;mp[0] = -1;int before = 0;for(int i = 0; i < n; ++i){prefixSum[i] = (before + nums[i]) % k;if(mp.find(prefixSum[i]) != mp.end()){if(i - mp[prefixSum[i]] >= 2) return true;}else{mp[prefixSum[i]] = i;}before = prefixSum[i];}return false;}
};
  1. 整数拆分(题目编号343:link)

class Solution {
public:int integerBreak(int n) {vector<int> dp(n + 1);for(int i = 2; i <= n; ++i){for(int j = 1; j < i; ++j){dp[i] = max(dp[i], j * max(i - j, dp[i - j]));}}return dp[n];}
};
  1. 最少侧跳次数(题目编号1824:link,2021.7.28)

class Solution {
public:int minSideJumps(vector<int>& obstacles) {int n = obstacles.size();if(n <= 1) return 0;vector<vector<int>> dp(n, vector<int>(3, 0x3fffffff));dp[0][0] = dp[0][2] = 1;dp[0][1] = 0;for(int i = 1; i < n; ++i){if(obstacles[i] != 1) dp[i][0] = dp[i - 1][0];if(obstacles[i] != 2) dp[i][1] = dp[i - 1][1];if(obstacles[i] != 3) dp[i][2] = dp[i - 1][2];if(obstacles[i] != 1) dp[i][0] = min(dp[i][0], min(dp[i][1], dp[i][2]) + 1);if(obstacles[i] != 2) dp[i][1] = min(dp[i][1], min(dp[i][0], dp[i][2]) + 1);if(obstacles[i] != 3) dp[i][2] = min(dp[i][2], min(dp[i][0], dp[i][1]) + 1);}return min(dp[n-1][0], min(dp[n-1][1], dp[n-1][2]));}
};
  1. 重新安排行程(题目编号332:link,2021.7.27)

class Solution {
private:unordered_map<string, priority_queue<string, vector<string>, std::greater<string>>> vec;vector<string> stk;
public:void dfs(const string& curr){while(vec.count(curr) && vec[curr].size() > 0){string tmp = vec[curr].top();vec[curr].pop();dfs(move(tmp));}stk.emplace_back(curr);}vector<string> findItinerary(vector<vector<string>>& tickets) {for(auto& it: tickets){vec[it[0]].emplace(it[1]);}dfs("JFK");reverse(stk.begin(), stk.end());return stk;}
};
  1. 将二叉搜索树变平衡(题目编号1382:link,2021.7.28)

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
private:vector<int> vec;
public:void midTraverse(TreeNode* root){if(!root) return;if(root->left){midTraverse(root->left);}vec.emplace_back(root->val);if(root->right){midTraverse(root->right);}}TreeNode* buildTree(vector<int>& vec, int left, int right){if(left > right){return nullptr;}int mid = left + (right - left) / 2;TreeNode* root = new TreeNode(vec[mid]);root->left = buildTree(vec, left, mid - 1);root->right = buildTree(vec, mid + 1, right);return root;}TreeNode* balanceBST(TreeNode* root) {midTraverse(root);return buildTree(vec, 0, vec.size() - 1);}
};
  1. 课程表IV(题目编号1462:link,2021.7.28)

class Solution {
public:vector<bool> checkIfPrerequisite(int numCourses, vector<vector<int>>& prerequisites, vector<vector<int>>& queries) {unordered_map<int, vector<int>> ump;for(auto& p: prerequisites){ump[p[1]].emplace_back(p[0]);}map<pair<int, int>, bool> mem;vector<bool> res(queries.size(), false);for(int i = 0; i < queries.size(); ++i){auto& q = queries[i];res[i] = dfs(ump, mem, q[1], q[0]);}return res;}bool dfs(unordered_map<int, vector<int>>& ump, map<pair<int, int>, bool>& mem, int q, int target){if(mem.find(pair<int, int>{target, q}) != mem.end()){return mem[pair<int, int>{target, q}];}bool ans = false;if(ump.find(q) == ump.end()){ans = false;}else{auto& vec = ump[q];if(find(vec.begin(), vec.end(), target) != vec.end()){ans = true;}else{for(auto& n: vec){if(dfs(ump, mem, n, target)){ans = true;break;}}}}mem[pair<int, int>{target, q}] = ans;return ans;}
};
  1. 具有所有最深节点的最小子树(题目编号865:link,2021.7.28)

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
private:map<TreeNode*, int> depth;int maxDepth = 0;
public:void getDepth(TreeNode* root, int d){if(!root) return;depth[root] = d;maxDepth = max(maxDepth, d);getDepth(root->left, d + 1);getDepth(root->right, d + 1);}TreeNode* answer(TreeNode* root){if(!root) return nullptr;if(depth[root] == maxDepth) return root; TreeNode *left = answer(root->left), *right = answer(root->right);if(left && right) return root;if(left) return left;if(right) return right;return nullptr;}TreeNode* subtreeWithAllDeepest(TreeNode* root) {getDepth(root, 0);return answer(root);}
};
  1. 两个字符串的删除操作(题目编号583:link,2021.7.28)

class Solution {
private:map<pair<int, int>, int> mp;public:int minDistance(string word1, string word2) {return word1.size() + word2.size() - 2 * getLCS(word1, word2, word1.size(), word2.size());}int getLCS(string& word1, string& word2, int len1, int len2){int ans = 0;if(len1 == 0 || len2 == 0) ans = 0;else if(mp[pair<int, int>{len1, len2}] > 0){ans = mp[pair<int, int>{len1, len2}];}else if(word1[len1 - 1] == word2[len2 - 1]){ans = getLCS(word1, word2, len1 - 1, len2 - 1) + 1;}else{ans =  max(getLCS(word1, word2, len1, len2 - 1), getLCS(word1, word2, len1 - 1, len2));}mp[pair<int, int>{len1, len2}] = ans;return ans;}
};
class Solution {
public:int minDistance(string word1, string word2) {int n1 = word1.size(), n2 = word2.size();vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 0));for(int i = 1; i <= n1; ++i){for(int j = 1; j <= n2; ++j){if(word1[i - 1] == word2[j - 1]){dp[i][j] = dp[i - 1][j - 1] + 1; }else{dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);}}}return n1 + n2 - 2 * dp[n1][n2];}
};
class Solution {
public:int minDistance(string word1, string word2) {int n1 = word1.size(), n2 = word2.size();vector<vector<int>> dp(n1 + 1, vector<int>(n2 + 1, 0));for(int i = 0; i <= n1; ++i){for(int j = 0; j <= n2; ++j){if(i == 0 || j == 0){dp[i][j] = i + j;continue;}if(word1[i - 1] == word2[j - 1]){dp[i][j] = dp[i - 1][j - 1]; }else{dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1;}}}return dp[n1][n2];}
};
  1. 剑指Offer II020 回文字符串的个数(链接:link,2021.8.22)

class Solution {
public:int countSubstrings(string s) {int n = s.size(), ans = 0;for(int i = 0; i < n; ++i){ans += count(s, n, i, i);ans += count(s, n, i, i + 1);}return ans;}int count(string& s, int n, int i, int j){int cnt = 0;while(i >= 0 && j < n && s[i] == s[j]){++cnt;--i;++j;}return cnt;}
};
  1. 剑指Offer II没有重复元素集合的全排列(题目编号083,链接:link)

class Solution {
public:vector<vector<int>> permute(vector<int>& nums) {int len = nums.size();vector<int> cur;vector<bool> visited(len, false);vector<vector<int>> ans;dfs(nums, len, 0, cur, visited, ans);return ans;}void dfs(vector<int>& nums, int len, int cur_ind, vector<int> cur, vector<bool> visited, vector<vector<int>>& ans){if(cur_ind == len){ans.push_back(cur);return;}++cur_ind;for(int i = 0; i < len; ++i){if(!visited[i]){visited[i] = true;cur.push_back(nums[i]);dfs(nums, len, cur_ind, cur, visited, ans);visited[i] = false;cur.pop_back();}}}
};
  1. 多次搜索(题目编号:面试题17.17,link,2021.8.22)

class Solution {
public:vector<vector<int>> multiSearch(string big, vector<string>& smalls) {int n = big.size();vector<vector<int>> char_pos(26);for(int i = 0; i < n; ++i){char_pos[big[i] - 'a'].push_back(i);}int m = smalls.size();vector<vector<int>> ans;for(int i = 0; i < m; ++i){int cur_size = smalls[i].size();vector<int> cur_ans;if(cur_size > 0){for(auto pos: char_pos[smalls[i][0] - 'a']){bool match = true;if(pos + cur_size > n){continue;}for(int j = pos; j < pos + cur_size; ++j){if(big[j] != smalls[i][j - pos]){match = false;break;}}if(match){cur_ans.push_back(pos);}}}ans.push_back(cur_ans);}return ans;}
};
class Solution {
public:static const int N = 100000 + 10;int childs[N][26];int values[N];int nodes_cnt;vector<vector<int>> multiSearch(string big, vector<string>& smalls) {vector<vector<int>> ans(smalls.size());memset(childs, 0, sizeof(childs));memset(values, 0xff, sizeof(values));nodes_cnt = 0;for(int i = 0; i < smalls.size(); ++i){insert(smalls[i], i);}for(int i = 0; i < big.size(); ++i){find(big, i, ans);}return ans;}void insert(const string& s, int index_of_s){int r = 0;for(int i = 0; i < s.size(); ++i){int idx = s[i] - 'a';int& next = childs[r][idx];if(next == 0){next = ++nodes_cnt;}r = next;}values[r] = index_of_s;}void find(const string& t, int beg, vector<vector<int>>& ans){int r = 0;for(int j = beg; j < t.size(); ++j){int idx = t[j] - 'a';const int& next = childs[r][idx];if(next == 0){return;}if(values[next] >= 0){ans[values[next]].push_back(beg);}r = next;}}
};
  1. 堆盘子(题目编号:面试题03.03,link)

["StackOfPlates", "push", "push", "push", "push", "push", "push", "popAt", "popAt", "popAt"]
[[2], [1], [2], [3], [4], [5], [6], [0], [0], [1]]
[null,null,null,null,null,null,null,2,3,6]
class StackOfPlates {
public:vector<stack<int>> vec_stk;int stk_cap = 0;StackOfPlates(int cap) {stk_cap = cap;// stack<int> stk;// vec_stk.push_back(stk);}void push(int val) {if(stk_cap == 0) return;if(vec_stk.empty() || vec_stk.back().size() == stk_cap){stack<int> stk;stk.push(val);vec_stk.push_back(stk);}else{vec_stk.back().push(val);}}int pop() {if(stk_cap == 0) return -1;if(vec_stk.empty()){return -1;}int val = vec_stk.back().top();vec_stk.back().pop();if(vec_stk.back().size() == 0) vec_stk.pop_back();return val;}int popAt(int index) {if(stk_cap == 0) return -1;int n = vec_stk.size();if(index < n && vec_stk[index].size() > 0){int val = vec_stk[index].top();vec_stk[index].pop();if(vec_stk[index].empty()){vec_stk.erase(vec_stk.begin() + index);}// reshape_vec_stk();return val;}return -1;}void reshape_vec_stk(){int n = vec_stk.size();int cur_stk_ind = 0;for(int i = 0; i < n; ++i){if(cur_stk_ind == i && vec_stk[i].size() == stk_cap){continue;}if(vec_stk[i].size() == 0){continue;}else{stack<int> tmp;while(vec_stk[i].size() > 0){tmp.push(vec_stk[i].top());vec_stk[i].pop();}while(tmp.size() > 0 && vec_stk[cur_stk_ind].size() < stk_cap){vec_stk[cur_stk_ind].push(tmp.top());tmp.pop();}while(tmp.size() > 0){vec_stk[i].push(tmp.top());tmp.pop();}if(vec_stk[cur_stk_ind].size() == stk_cap){++cur_stk_ind;}}}}
};/*** Your StackOfPlates object will be instantiated and called as such:* StackOfPlates* obj = new StackOfPlates(cap);* obj->push(val);* int param_2 = obj->pop();* int param_3 = obj->popAt(index);*/
  1. 实现一个魔法字典(题目编号676:link,2021.8.22)

class MagicDictionary {unordered_map<string, int> ump;unordered_map<string, int> dict;
public:/** Initialize your data structure here. */MagicDictionary() {}void buildDict(vector<string> dictionary) {char tmp;for(auto& word: dictionary){dict[word] = 1;for(int i = 0; i < word.size(); ++i){tmp = word[i];word[i] = ' ';++ump[word];word[i] = tmp;}}}bool search(string searchWord) {char tmp;string oriWord = searchWord;for(int i = 0; i < searchWord.size(); ++i){tmp = searchWord[i];searchWord[i] = ' ';if(ump[searchWord] > 1 || (ump[searchWord] == 1 && dict[oriWord] == 0)){return true;}searchWord[i] = tmp;}return false;}
};/*** Your MagicDictionary object will be instantiated and called as such:* MagicDictionary* obj = new MagicDictionary();* obj->buildDict(dictionary);* bool param_2 = obj->search(searchWord);*/
  1. 从英文中重建数字(题目编号:423:link,2021.8.27)

class Solution {
public:string originalDigits(string s) {// zero one two three four five six seven eight ninevector<vector<string>> pairs{{"zero", "z", "0"},{"eight", "g", "8"},{"four", "u", "4"},{"five", "f", "5"},{"three", "h", "3"},{"two", "t", "2"},{"six", "x", "6"},{"nine", "i", "9"},{"seven", "v", "7"},{"one", "o", "1"}};vector<int> cnt(26, 0);vector<int> num_cnt(10, 0);for(char c: s){++cnt[c - 'a'];}for(auto item: pairs){char sp_char = item[1][0];int real_num = item[2][0] - '0';num_cnt[real_num] = cnt[sp_char - 'a'];for(char c: item[0]){cnt[c - 'a'] -= num_cnt[real_num];}}string ans;for(int i = 0; i <= 9; ++i){if(num_cnt[i] > 0) ans += string(num_cnt[i], '0' + i);}return ans;}
};
  1. 零钱兑换II(题目编号518:link,2021.8.27)

class Solution {
public:int change(int amount, vector<int>& coins) {vector<int> dp(amount + 1);dp[0] = 1;for(int c: coins){for(int i = c; i <= amount; ++i){dp[i] += dp[i - c];}}return dp[amount];}
};
  1. 剑指Offer II 085. 生成匹配的括号(链接:link,2021.8.27)

class Solution {
public:vector<string> generateParenthesis(int n) {vector<string> ans;dfs(ans, "", 0, n);return ans;}void dfs(vector<string>& ans, string cur, int left_cnt, const int n){if(cur.size() == n * 2){ans.push_back(cur);return;}if(left_cnt < n){cur += "(";dfs(ans, cur, left_cnt + 1, n);cur.pop_back();}if(left_cnt * 2 > cur.size()){cur += ")";dfs(ans, cur, left_cnt, n);}}
};
  1. 吃苹果的最大数目(题目编号1705:link,2021.8.27)

class Solution {
public:int eatenApples(vector<int>& apples, vector<int>& days) {map<int, int> day2cnt;int res = 0;int n = apples.size();for(int i = 0; i < n || !day2cnt.empty(); ++i){day2cnt.erase(i);if(i < n && apples[i] > 0){day2cnt[i + days[i]] += apples[i];}if(!day2cnt.empty()){auto iter = day2cnt.begin();--iter->second;++res;if(iter->second == 0){day2cnt.erase(iter);}}}return res;}
};
  1. 剑指Offer II 046. 二叉树的右侧视图(链接:link,2021.8.27)

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<int> rightSideView(TreeNode* root) {vector<int> ans;int maxDepth = 0;dfs(ans, root, maxDepth, 0);return ans;}void dfs(vector<int>& ans, TreeNode* root, int& maxDepth, int curDepth){if(!root) return;++curDepth;if(curDepth > maxDepth){ans.push_back(root->val);maxDepth = curDepth; }dfs(ans, root->right, maxDepth, curDepth);dfs(ans, root->left, maxDepth, curDepth);}
};
  1. 网络延迟时间(题目编号743:link,2021.8.27)

class Solution {
public:int networkDelayTime(vector<vector<int>>& times, int n, int k) {map<int, vector<int>> node2adj;map<vector<int>, int> conn_cost;vector<int> min_cost(n, INT_MAX);for(auto v: times){node2adj[v[0] - 1].push_back(v[1] - 1);conn_cost[vector<int>{v[0] - 1, v[1] - 1}] = v[2];}vector<int> adj = node2adj[k - 1];vector<bool> visited(n);min_cost[k - 1] = 0;if(adj.size() > 0){visited[k - 1] = true;for(int tar: adj){update(min_cost, node2adj, conn_cost, k - 1, tar, visited);}}int ans = *max_element(min_cost.begin(), min_cost.end());return ans == INT_MAX ? -1: ans;}void update(vector<int>& min_cost, map<int, vector<int>>& node2adj, map<vector<int>, int>& conn_cost, int src, int tar, vector<bool> visited){if(visited[tar]) return;visited[tar] = true;min_cost[tar] = min(min_cost[tar], min_cost[src] + conn_cost[vector<int>{src, tar}]);for(int t: node2adj[tar]){if(t != src && !visited[t]){update(min_cost, node2adj, conn_cost, tar, t, visited);}}}
};
class Solution {
public:int networkDelayTime(vector<vector<int>>& times, int n, int k) {const int inf = INT_MAX / 2;vector<vector<int>> g(n, vector<int>(n, inf));for(auto& t: times){int x = t[0] - 1, y = t[1] - 1;g[x][y] = t[2];}vector<int> dist(n, inf);dist[k - 1] = 0;vector<int> used(n);for(int i = 0; i < n; ++i){int x = -1;for(int y = 0; y < n; ++y){if(!used[y] && (x == -1 || dist[y] < dist[x])){x = y;}}used[x] = true;for(int y = 0; y < n; ++y){dist[y] = min(dist[y], dist[x] + g[x][y]);}}int ans = *max_element(dist.begin(), dist.end());return ans == inf ? -1: ans;}
};
  1. 插入、删除和随机访问都是 O(1) 的容器(题目编号:剑指offerII 面试题30,link)

class RandomizedSet {
private:unordered_map<int, int> ump;vector<int> nums;public:/** Initialize your data structure here. */RandomizedSet() {}/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */bool insert(int val) {if(ump.count(val)){return false;}nums.push_back(val);ump[val] = nums.size() - 1;return true;}/** Removes a value from the set. Returns true if the set contained the specified element. */bool remove(int val) {if(ump.count(val)){int remove_pose = ump[val];nums[remove_pose] = nums.back();ump[nums.back()] = remove_pose;nums.pop_back();ump.erase(val);return true;}return false;}/** Get a random element from the set. */int getRandom() {return nums[rand() % nums.size()];}
};/*** Your RandomizedSet object will be instantiated and called as such:* RandomizedSet* obj = new RandomizedSet();* bool param_1 = obj->insert(val);* bool param_2 = obj->remove(val);* int param_3 = obj->getRandom();*/
  1. 获取好友已观看的视频(题目编号1311:link)

using PSI = pair<string, int>;
class Solution {
public:vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos, vector<vector<int>>& friends, int id, int level) {int n = watchedVideos.size();vector<string> ans;if(id < 0 || id >= n || level < 1 || level > n) return ans;unordered_map<int, bool> visited;queue<int> kfriends;kfriends.push(id);visited[id] = true;int round = 0;while(round++ < level){int sz = kfriends.size();while(sz--){int cur_id = kfriends.front();kfriends.pop();for(auto& k: friends[cur_id]){if(visited[k]){continue;}visited[k] = true;kfriends.push(k);}}}unordered_map<string, int> count;while(kfriends.size()){int cur_id = kfriends.front();kfriends.pop();for(auto& k: watchedVideos[cur_id]){++count[k];}}vector<PSI> videos(count.begin(), count.end());sort(videos.begin(), videos.end(), func);for(auto& item:videos){ans.push_back(item.first);}return ans;}static bool func(PSI a, PSI b){return a.second < b.second || (a.second == b.second && a.first < b.first);}
};
  1. 子字符串突变后可能得到的最大整数(题目编号1946:link)

class Solution {
public:string maximumNumber(string num, vector<int>& change) {int n = num.size();for(int i = 0; i < n; ++i){if(change[num[i] - '0'] > num[i] - '0'){while(i < n && change[num[i] - '0'] >= num[i] - '0'){num[i] = '0' + change[num[i] - '0'];++i;}break;}}return num;}
};
  1. 解码异或后的排列(题目编号1734:link,2021.9.26)

class Solution {
public:vector<int> decode(vector<int>& encoded) {int xor_all = 0, n = encoded.size() + 1;for(int i = 1; i <= n; ++i){xor_all ^= i;}int xor_except_last = 0;for(int i = 0; i < n - 1; i += 2){xor_except_last ^= encoded[i];}vector<int> perm(n);perm[n - 1] = xor_except_last ^ xor_all;for(int i = n - 2; i >= 0; --i){perm[i] = encoded[i] ^ perm[i + 1];}return perm;}
};
  1. 二叉搜索树与双向链表(题目编号:剑指offer36,link,2021.9.26)

/*
// Definition for a Node.
class Node {
public:int val;Node* left;Node* right;Node() {}Node(int _val) {val = _val;left = NULL;right = NULL;}Node(int _val, Node* _left, Node* _right) {val = _val;left = _left;right = _right;}
};
*/
class Solution {
public:Node* treeToDoublyList(Node* root) {if(!root) return root;vector<Node*> ans = func(root);ans[0]->left = ans[1];ans[1]->right = ans[0];return ans[0];}vector<Node*> func(Node* root){vector<Node*> vec{root, root};if(!root) return vec;if(root->left){vector<Node*> tmp_vec = func(root->left);tmp_vec[1]->right = root;root->left = tmp_vec[1];vec[0] = tmp_vec[0];}if(root->right){vector<Node*> tmp_vec = func(root->right);root->right = tmp_vec[0];tmp_vec[0]->left = root;vec[1] = tmp_vec[1];}return vec;}};
/*
// Definition for a Node.
class Node {
public:int val;Node* left;Node* right;Node() {}Node(int _val) {val = _val;left = NULL;right = NULL;}Node(int _val, Node* _left, Node* _right) {val = _val;left = _left;right = _right;}
};
*/
class Solution {
private:Node *pre = nullptr, *head = nullptr;
public:Node* treeToDoublyList(Node* root) {if(!root) return root;dfs(root);pre->right = head;head->left = pre;return head;}void dfs(Node* root){if(!root) return;dfs(root->left);if(!pre){head = root;}else{pre->right = root;}root->left = pre;pre = root;dfs(root->right);}};
  1. 最长连续序列(题目编号:剑指Offer II 119,link,2021.9.26)

class Solution {
public:int longestConsecutive(vector<int>& nums) {unordered_set<int> st;for(auto& n: nums){st.insert(n);}int ans = 0;while(st.size()){int n = *st.begin();int left = n - 1;while(st.count(left)){st.erase(left);--left;}int right = n + 1;while(st.count(right)){st.erase(right);++right;}ans = max(ans, right - left - 1);st.erase(n);}return ans;}
};
  1. 扣分后的最大得分(link,2021.9.29)

class Solution {
public:long long maxPoints(vector<vector<int>>& points) {int m = points.size();if(m == 0) return 0;int n = points[0].size();vector<long long> f(m);for(int i = 0; i < m; ++i){vector<long long> g(n);long long best = LLONG_MIN;for(int j = 0; j < n; ++j){best = max(best, f[j] + j);g[j] = max(g[j], best + points[i][j] - j);}best = LLONG_MIN;for(int j = n - 1; j >= 0; --j){best = max(best, f[j] - j);g[j] = max(g[j], best + points[i][j] + j);}f = move(g);}return *max_element(f.begin(), f.end());}
};
  1. 火柴拼正方形(题目编号473:link,2021.9.29)

class Solution {
public:bool makesquare(vector<int>& matchsticks) {int sum = 0;for(auto& num: matchsticks){sum += num;}if(sum == 0 || sum % 4 != 0) return false; //剪枝vector<int> adds(4, 0);sort(matchsticks.begin(), matchsticks.end(), greater<int>());return dfs(0, adds, matchsticks, sum);}bool dfs(int beg, vector<int>& adds, vector<int>& matchsticks, int sum){if(*max_element(adds.begin(), adds.end()) > sum / 4) return false;if(beg == matchsticks.size()){if(adds[0] == adds[1] && adds[1] == adds[2] && adds[2] == adds[3]){return true;}return false;}for(int i = 0; i < 4; ++i){if(adds[i] + matchsticks[beg] > sum / 4 || (i > 0 && adds[i] == adds[i - 1])) continue; //关键的剪枝adds[i] += matchsticks[beg];if(dfs(beg+1, adds, matchsticks, sum)) return true;adds[i] -= matchsticks[beg];}return false;}
};
  1. 设计地铁系统(题目编号1396:link,2021.9.29)

class UndergroundSystem {
private:map<int, pair<string, int>> mp_in;map<pair<string, string>, pair<double, int>> mp_time;
public:UndergroundSystem() {}void checkIn(int id, string stationName, int t) {mp_in[id] = pair<string, int>(stationName, t);}void checkOut(int id, string stationName, int t) {string startStationName = mp_in[id].first;int startTime = mp_in[id].second;double meanTime = mp_time[pair<string, string>(startStationName, stationName)].first;int count = mp_time[pair<string, string>(startStationName, stationName)].second;pair<double, int> new_result = pair<double, int>(count * 1.0 / (count + 1) * meanTime + (t - startTime) * 1.0 / (count + 1), count + 1);mp_time[pair<string, string>(startStationName, stationName)] = new_result;}double getAverageTime(string startStation, string endStation) {return mp_time[pair<string, string>(startStation, endStation)].first;}
};/*** Your UndergroundSystem object will be instantiated and called as such:* UndergroundSystem* obj = new UndergroundSystem();* obj->checkIn(id,stationName,t);* obj->checkOut(id,stationName,t);* double param_3 = obj->getAverageTime(startStation,endStation);*/
  1. 爱吃香蕉的珂珂(题目编号875:link,2021.10.6)

class Solution {
public:int minEatingSpeed(vector<int>& piles, int h) {int lo = 1, hi = pow(10, 9);while(lo < hi){int mid = lo + (hi - lo) / 2;if(check(piles, h, mid)){hi = mid;}else{lo = mid + 1;}}return hi;}bool check(vector<int>& piles, int h, int K){int time = 0;for(auto& p: piles){time += (p - 1) / K + 1;}return time <= h;}
};
  1. 单词长度的最大乘积(题目编号:剑指Offer II 005,link,2021.10.6)

class Solution {
public:int maxProduct(vector<string>& words) {int n = words.size();if(n <= 1) return 0;vector<int> hash(n);for(int i = 0; i < n; ++i){for(auto& c: words[i]){hash[i] |= 1 << (c - 'a');}}int ans = 0;for(int i = 0; i < n - 1; ++i){for(int j = i + 1; j < n; ++j){int tmp = words[i].size() * words[j].size();if(tmp > ans && ((hash[i] & hash[j]) == 0)){ans = tmp;}}}return ans;}
};
  1. 将字符串拆成递减的连续值(题目编号:1849,link,2021.10.6)

  • 这道题目耗费了很长时间,主要在解决各种特殊用例和整形数越界的问题。
  • 思路是先确定递减数列的第一个数num,然后后续依次搜索n-1,n-2,等等。
  • 特殊情况包括:
    降序后某一个数变成0,后续只需全0即可,不需再寻找-1。
    第一个数就是0,这种情况是不符合题目要求的。
class Solution {
public:bool splitString(string s) {int n = s.size();for(int digit = 1; digit <= n - 1; ++digit){unsigned long long num = 0;for(int i = 0; i < digit; ++i){num = num * 10 + (s[i] - '0');}if(num == 0) continue; //解决第一个数字就是0的问题int ind = digit;bool ans_flag = true, complete_flag = false;while(ind < n){unsigned long long t_num = 0;complete_flag = false;while(ind < n){if(num == 0){while(ind < n && s[ind] == '0'){++ind;}//解决降序数变为0的特殊情况,之后只要全是0就行。if(ind == n){return true;}ans_flag = false;break;}t_num = t_num * 10 + (s[ind] - '0');++ind;if(t_num == num - 1){complete_flag = true;break;}else if(t_num > num - 1){ans_flag = false;break;}}if(!ans_flag){break;}num = t_num;}if(ans_flag && complete_flag){return true;}}return false;}
};
  1. 删除最短的子数组使剩余数组有序(题目编号1574:link,2021.10.23)

class Solution {
public:int findLengthOfShortestSubarray(vector<int>& arr) {int left = 1, n = arr.size(), right = n - 1;while(left < n && arr[left - 1] <= arr[left]) ++left;while(right >= 1 && arr[right - 1] <= arr[right]) --right;if(left > right) return 0;int ans = right;for(int i = 0; i < left; ++i){int e = arr[i];int pos = lower_bound(arr.begin() + right, arr.end(), e) - arr.begin();ans = min(ans, pos - i - 1);}return ans;}
};
  1. 所有子集(题目编号:剑指Offer II 079,link,2021.10.23)

class Solution {
public:vector<vector<int>> subsets(vector<int>& nums) {vector<vector<int>> ans;int n = nums.size();traverse(ans, nums, n, 0, vector<int>());return ans;}void traverse(vector<vector<int>>& ans, vector<int>& nums, int& n, int cur_ind, vector<int> cur_subset){if(cur_ind == n){ans.push_back(cur_subset);return;}traverse(ans, nums, n, cur_ind + 1, cur_subset);cur_subset.push_back(nums[cur_ind]);traverse(ans, nums, n, cur_ind + 1, cur_subset);}
};
  1. 到达目的地的方案数(题目编号:1976,link,2021.10.23)

  • 题目的解决分为几步,首先是寻找起点到终点的最短距离,使用dijstra算法,然后构造有向图g,如果满足dist[0][j]-dist[0][i]=dist[i][j],那么存在从i到j的边(可证明是无环的),接着采用动态规划,求解最终的方案数,用数组f表示,f[n-1]=1,倒序前推,对于i,遍历g中有边连接的下一个节点j,对方案数求和,最终的f[0]即为答案。
  • 这道题综合了最短路径和动态规划,适合面试。
  • 下面解法涉及到了function模板和lambda函数,都是知识点。参考此博客
class Solution {
private:int mod = pow(10, 9) + 7;
public:int countPaths(int n, vector<vector<int>>& roads) {vector<vector<long long>> dist(n, vector<long long>(n, LLONG_MAX / 2));for(int i = 0; i < n; ++i) dist[i][i] = 0;for(auto&& info: roads){int a = info[0], b = info[1], d = info[2];dist[a][b] = dist[b][a] = d;}//Dijstravector<bool> used(n);for(int _ = 0; _ < n; ++_){int u = -1;for(int i = 0; i < n; ++i){if(!used[i] && (u == -1 || dist[0][i] < dist[0][u])){u = i;}}used[u] = true;for(int i = 0; i < n; ++i){dist[0][i] = min(dist[0][i], dist[0][u] + dist[u][i]);}}//构造图Gvector<vector<int>> g(n);for(auto&& info: roads){int a = info[0], b = info[1], d = info[2];if(dist[0][b] - dist[0][a] == d){g[a].push_back(b);}else if(dist[0][a] - dist[0][b] == d){g[b].push_back(a);}}//最终结果vector<int> f(n, -1);function<int(int)> dfs = [&](int u){if(u == n - 1) return 1;if(f[u] != -1) return f[u];f[u] = 0;for(auto& v: g[u]){f[u] += dfs(v);if(f[u] > mod){f[u] -= mod;}}return f[u];};return dfs(0);}
};

leetcode刷题记录2:进度64, 2021.10.23相关推荐

  1. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  2. LeetCode刷题记录14——257. Binary Tree Paths(easy)

    LeetCode刷题记录14--257. Binary Tree Paths(easy) 目录 前言 题目 语言 思路 源码 后记 前言 数据结构感觉理论简单,实践起来很困难. 题目 给定一个二叉树, ...

  3. LeetCode刷题记录13——705. Design HashSet(easy)

    LeetCode刷题记录13--705. Design HashSet(easy) 目录 LeetCode刷题记录13--705. Design HashSet(easy) 前言 题目 语言 思路 源 ...

  4. LeetCode刷题记录12——232. Implement Queue using Stacks(easy)

    LeetCode刷题记录12--232. Implement Queue using Stacks(easy) 目录 LeetCode刷题记录12--232. Implement Queue usin ...

  5. LeetCode刷题记录11——290. Word Pattern(easy)

    LeetCode刷题记录11--290. Word Pattern(easy) 目录 LeetCode刷题记录11--290. Word Pattern(easy) 题目 语言 思路 源码 后记 题目 ...

  6. LeetCode刷题记录10——434. Number of Segments in a String(easy)

    LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...

  7. LeetCode刷题记录9——58. Length of Last Word(easy)

    LeetCode刷题记录9--58. Length of Last Word(easy) 目录 LeetCode刷题记录9--58. Length of Last Word(easy) 题目 语言 思 ...

  8. LeetCode刷题记录8——605. Can Place Flowers(easy)

    LeetCode刷题记录8--605. Can Place Flowers(easy) 目录 LeetCode刷题记录8--605. Can Place Flowers(easy) 题目 语言 思路 ...

  9. LeetCode刷题记录7——824. Goat Latin(easy)

    LeetCode刷题记录7--824. Goat Latin(easy) 目录 LeetCode刷题记录7--824. Goat Latin(easy) 题目 语言 思路 后记 题目 题目需要将一个输 ...

  10. LeetCode刷题记录6——696. Count Binary Substrings(easy)

    LeetCode刷题记录6--696. Count Binary Substrings(easy) 目录 LeetCode刷题记录6--696. Count Binary Substrings(eas ...

最新文章

  1. Nginx开启GZIP来压缩网页
  2. css中的一些常用选择器
  3. idea 代码第一次上传git_如何使用IDEA将项目代码首次上传至GitHub,并持续推送?...
  4. sqlserver查询补全时间_mssql 按日期分组(group by)查询统计的时候,没有数据补0的解决办法...
  5. vrp 节约算法 c++_滴滴技术:浅谈滴滴派单算法
  6. c语言字 字符串转换成数组_C语言学习教程之详解C语言中的字符串数组
  7. power bi可视化表_滚动器可视化功能,用于Power BI Desktop中的股价变动
  8. 安装双系统window +ubuntu
  9. 《FLUENT 14流场分析自学手册》——第1章 流体力学基础 1.1 流体力学基本概念
  10. pta平台c语言作业答案,C语言PTA平台习题与答案.pdf
  11. HikariCP 创建连接
  12. MAC关闭SIP:system integrity protection is on, some features will not be available
  13. 历史周期律 多久_什么是“历史周期律”?
  14. 手动解除fail2ban的ip
  15. 几款我比较满意的在线脑图工具
  16. 【python】pandas库pd.DataFrame.corr、numpy库np.corrcoef求解相关系数
  17. 【H5】h5文件之——深度学习训练样本制作
  18. Pytorch之Rot旋转
  19. 大数据平台执行报错,权限问题。
  20. mipi白皮书 在5G下 IOT

热门文章

  1. 【微信小程序开发】(三)首页banner组件使用swiper
  2. ural 1998 The old Padawan (模拟+二分)
  3. 计算机科学与技术专业有哪些课程,计算机科学与技术专业课程有哪些
  4. testng单元测试-diagnose依赖compet找不到compet的类
  5. 详述H5微信棋牌游戏域名防封实现原理
  6. 汤家凤高等数学基础手写笔记-导数与微分
  7. 美团2017年CodeM资格赛题解
  8. ASP第一章IIS发布站点
  9. Android 经典 Crash 分析,Android Crash 原理分析
  10. 关于《管理:技艺之精髓》一书中提到的管理任务和工具