# preface

Follow brother hero to punch in on the 31st day

[Topic jump - > 100 lectures on zero fundamentals of algorithms]

[ten thousand people and one thousand questions community jump]

# 1, Judgment subsequence

Jump force buckle: 392. Judgment subsequence

Difficulty: ★☆☆☆

Description: double pointer solution, s s s represents a short string, t t t represents a long string, i i i record the subscript of the short string, j j j record long string subscript, loop traversal t t t ， s [ i ] = = t [ j ] s[i]==t[j] s[i]==t[j] indicates that the same character is searched. At this time, the short string moves one step backward. When the long string traversal is completed, the pointer i i i also came to the end s s s is t t Subsequence of t

The code is as follows (example):

class Solution { public: bool isSubsequence(string s, string t) { int n = s.size(), m = t.size(); int i, j; for (i = 0, j = 0; j < m; j ++) { if (s[i] == t[j]) i ++; } return i == n; } };

# 2, Search two-dimensional matrix II

Jump force buckle: 240. Search two-dimensional matrix II

Difficulty: ★☆☆☆

Note: for optimization, because each column of the array is incremented to the right and each row is incremented downward, the upper right corner is used as the starting point for searching t a r g e t target target, if greater than t a r g e t target target, search to the left, if less than t a r g e t target target, then look down

The code is as follows (example):

class Solution { public: bool searchMatrix(vector<vector<int>>& matrix, int target) { int m = matrix.size(), n = matrix[0].size(); int x = 0, y = n - 1; while (x < m && y >= 0) { if (matrix[x][y] > target) y --; else if (matrix[x][y] < target) x ++; else return true; } return false; } };

# 3, Number of pairs whose absolute value of difference is K

Jump force buckle: 2006. Number of pairs whose absolute value of difference is K

Difficulty: ★☆☆☆

Note: the hash table records the number of occurrences of a number. Finally, find the product of the number of occurrences of each number and the number of occurrences of a value greater than k in the data range from small to large

The code is as follows (example):

class Solution { public: int countKDifference(vector<int>& nums, int k) { unordered_map<int, int> hash; int ans = 0; for (auto &n: nums) hash[n] ++; // Record the number of times in the list for (int i = 1; i + k < 105; i ++) { ans += hash[i] * hash[i + k]; // Given the range of num[i]: 1-100 // The enumeration number is from 1 to 100, // The hash value product indicates how many values are selected for comparison } return ans; } };

# 4, Find different

Jump force buckle: 389. Find a different

Difficulty: ★☆☆☆

Description: hash table, m s ms ms representation s s The number of occurrences of each character in s, m t mt mt representation t t The number of occurrences of each character in t, and returns the element c, where c is in the hash table m t mt mt medium ratio m s ms ms is large, that is, the added element

The code is as follows (example):

class Solution { public: char findTheDifference(string s, string t) { unordered_map<char, int> ms, mt; for (auto &c: s) ms[c] ++; for (auto &c: t) { mt[c] ++; if (mt[c] > ms[c]) return c; } return ' '; } };

# 5, Children with the most candy

Jump force buckle: 1431. Children with the most candy

Difficulty: ★☆☆☆

Note: greedy, first find the child with the largest number of initial candy, and then traverse each child, if there are all candy e x t r a C a n d i e s extraCandies If the number of extraCandies given to this child is less than that of the child with the largest number of candy initially, false is returned; otherwise, true is returned

The code is as follows (example):

class Solution { public: vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) { int maxn = 0; vector<bool> ans; for (int i = 0; i < candies.size(); i ++) maxn = max(maxn, candies[i]); for (int i = 0; i < candies.size(); i ++) { if (candies[i] + extraCandies < maxn) ans.push_back(false); else ans.push_back(true); } return ans; } };

# 6, Sum of all odd length subarrays

Jump force buckle: 1588. Sum of all odd length subarrays

Difficulty: ★☆☆☆

Description: mathematics, calculate the number of occurrences of each number in all sub arrays that meet the requirements, traverse each number, and calculate how many numbers are on the left of the number l e f t C o u n t leftCount leftCount, how many numbers are there on the right r i g h t C o u n t rightCount rightCount, and then calculate the number of groups of continuous odd and even subarrays on the left and right sides respectively to get the result

The code is as follows (example):

class Solution { public: int sumOddLengthSubarrays(vector<int>& arr) { int sum = 0; int n = arr.size(); for (int i = 0; i < n; i++) { int leftCount = i, rightCount = n - i - 1; // Number of elements on the left number of elements on the right int leftOdd = (leftCount + 1) / 2; // How many sub arrays are there on the left? The number is odd // If [1,2,3,4] is the element on the left, then [4] and [2,3,4] meet the requirements, i.e. 2 int rightOdd = (rightCount + 1) / 2; int leftEven = leftCount / 2 + 1; // How many subarrays on the left are even // If [1,2,3,4] is the element on the left, then [3,4] and [1,2,3,4] meet the requirements, i.e. 2 int rightEven = rightCount / 2 + 1; sum += arr[i] * (leftOdd * rightOdd + leftEven * rightEven); } return sum; } };

# 7, Count the triples

Jump force buckle: 1534. Count triples

Difficulty: ★☆☆☆

Note: if the first number and the second number do not meet the requirements, skip directly to save time

The code is as follows (example):

class Solution { public: int countGoodTriplets(vector<int>& arr, int a, int b, int c) { int n = arr.size(), ans = 0; for (int i = 0; i < n; i ++) { for (int j = i + 1; j < n; j ++) { if (abs(arr[i] - arr[j]) > a) continue; for (int k = j + 1; k < n; k ++) { if (abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c) ans ++; } } } return ans; } };

# 8, Gemstones and stones

Jump force buckle: 771. Gemstones and stones

Difficulty: ★★☆☆

Description: set, directly translate the meaning of the title into code. If there are gemstones in the traversal stone, it is OK + 1 +1 +1

The code is as follows (example):

class Solution { public: int numJewelsInStones(string jewels, string stones) { int jewelsCount = 0; unordered_set<char> jewelsSet; for (auto &jewel: jewels) { jewelsSet.insert(jewel); } for (auto &stone: stones) { if (jewelsSet.count(stone)) { jewelsCount++; } } return jewelsCount; } };

# 9, Create the target array in the given order

Jump force buckle: 1389. Create the target array in the established order

Difficulty: ★★☆☆

Array, directly call the array method i n s e r t ( p o s , n u m ) insert(pos, num) insert(pos,num) inserts the value num in the specified subscript pos

The code is as follows (example):

class Solution { public: vector<int> createTargetArray(vector<int>& nums, vector<int>& index) { vector<int> ans; for (int i = 0; i < nums.size(); i ++) { ans.insert(ans.begin() + index[i], nums[i]); } return ans; } };

# 10, Longest common prefix

Jump force buckle: 14. Longest common prefix

Difficulty: ★★☆☆

Description: hash table, with the first string f i r s t first first as the base, hash each character, traverse from the second string, and calculate each string and f i r s t first The prefix of first is the same maximum value, and then find the smallest of the maximum prefixes calculated by all strings as the answer

The code is as follows (example):

class Solution { public: string longestCommonPrefix(vector<string>& strs) { if (strs.size() == 1 ) return strs[0]; int hash[205], cnt = 0, ans = 205; string first = strs[0]; while (cnt <= first.size()) { hash[cnt ++] = first[cnt] - 'a'; } for (int i = 1; i < strs.size(); i ++) { int maxn = 0; for (int j = 0; j < cnt && j < strs[i].size(); j ++) { if (strs[i][j] - 'a' == hash[j]) maxn = max(maxn, j + 1); else break; } ans = min(maxn, ans); } if (ans == 205) return ""; return first.substr(0, ans); } };

# 11, Count the number of square sum triples

Jump force buckle: 1925. Count the number of square sum triples

Difficulty: ★★☆☆

Note: violence enumeration optimizes the three-layer cycle. After enumerating two numbers, calculate the third number that meets the square requirement. If the third number is also n n Within n, all requirements are met, counter + 1

The code is as follows (example):

class Solution { public: int countTriples(int n) { int res = 0; // Enumerating a and b for (int a = 1; a <= n; ++a){ for (int b = 1; b <= n; ++b){ // Judge whether it meets the requirements int c = int(sqrt(a * a + b * b)); if (c <= n && c * c == a * a + b * b){ ++res; } } } return res; } };