class Solution { public: int racecar(int target) { queue<pair<int,int>>que; set<pair<int,int>>vis; que.push({0,1}); int cnt=0; while(!que.empty()){ for(int sz=que.size();sz>0;sz--){ auto[curX,curV]=que.front();que.pop(); //cout<<curX<<endl; if(vis.count({curX,curV}))continue; vis.insert({curX,curV}); if(abs(curV)>target*2+5)continue; if(curX==target)return cnt; que.push({curX+curV,curV*2}); if(curX!=1&&curX!=-1)que.push({curX,curV>0?-1:1}); } cnt++; } return -1; } };
用isalpha来判断空格
class Solution { public: string mostCommonWord(string paragraph, vector<string>& banned) { unordered_set<string> b; for (string bb : banned) { b.insert(bb); } string curr; int maxCnt = 0; string res; unordered_map<string, int> str2cnt; for (char c : paragraph) { if (isalpha(c)) { curr += tolower(c); } else if (curr.size() > 0) { // 排序被禁用的单词 if (b.find(curr) == b.end()) { ++str2cnt[curr]; if (str2cnt[curr] > maxCnt) { maxCnt = str2cnt[curr]; res = curr; } } curr = ""; } } // 需要考虑最后一个单词 if (curr.size() > 0) { // 排序被禁用的单词 if (b.find(curr) == b.end()) { ++str2cnt[curr]; if (str2cnt[curr] > maxCnt) { maxCnt = str2cnt[curr]; res = curr; } } curr = ""; } return res; } };
前缀树+逆序存储即可
class TrieNode{ TrieNode* children[26]; public: int count; TrieNode() { for (int i = 0; i < 26; ++i) children[i] = NULL; count = 0; } TrieNode* get(char c) { if (children[c - 'a'] == NULL) { children[c - 'a'] = new TrieNode(); count++; } return children[c - 'a']; } }; class Solution { public: int minimumLengthEncoding(vector<string>& words) { TrieNode* trie = new TrieNode(); unordered_map<TrieNode*, int> nodes; for (int i = 0; i < (int)words.size(); ++i) { string word = words[i]; TrieNode* cur = trie; for (int j = word.length() - 1; j >= 0; --j) cur = cur->get(word[j]); nodes[cur] = i; } int ans = 0; for (auto& [node, idx] : nodes) { if (node->count == 0) { ans += words[idx].length() + 1; } } return ans; } };
采用返回数组存储索引
class Solution { public: vector<int> shortestToChar(string s, char c) { int n = s.size(); vector<int> res(n, n-1); // 从左往右开始遍历 res[0] = s[0] == c ? 0 : res[0]; for (int i = 1; i < n; ++i) { res[i] = s[i] != c ? res[i-1] + 1 : 0; } // 从右往左开始遍历 res[n-1] = s[n-1] == c ? 0 : res[n-1]; for (int i = n-2; i >= 0; --i) { // 这里需要去 res[i] = min(res[i], s[i] != c ? res[i+1] + 1 : 0); } return res; } };
所有正反面相同的都不可以,双面不一致的则统计最小值即可。
class Solution { public: int flipgame(vector<int>& fronts, vector<int>& backs) { unordered_set<int>cannotNums; for(int i=0;i<fronts.size();i++){ if(fronts[i]==backs[i])cannotNums.insert(fronts[i]); } int ans=INT_MAX; for(int i=0;i<fronts.size();i++){ if(cannotNums.count(fronts[i])==0){ ans=min(ans,fronts[i]); } if(cannotNums.count(backs[i])==0){ ans=min(ans,backs[i]); } } return ans==INT_MAX?0:ans; } };
定义
d[i]表示对于序号i的组合的数量
初始化
每个数字自己至少是一颗二叉树,所以 d[i]=1
计算
因为叶子必然比树小,所以我们做排序,然后按照每个点从小到大去找左右节点
左右节点用一个数组记录是否存在,便于快速查找
一旦存在左右子树l和r(序号),那么d[i] =d[i]+dp[l]*d[r]
class Solution { public: int numFactoredBinaryTrees(vector<int>& arr) { int n = arr.size(); long d[n]; for (int i = 0; i < n; ++i) { d[i] = 1; } int base = 1000000007; sort(arr.begin(), arr.end()); // 构建数字到序号的映射 unordered_map<int, int> num2idx; for (int i = 0; i < n; ++i) { num2idx[arr[i]] = i; } for (int i = 1; i < n; ++i) { // 找比它更小的数字 for (int j = 0; j < i; ++j) { if (arr[i] % arr[j] == 0) { // 找到另外一个结点 if (num2idx.find(arr[i] / arr[j]) != num2idx.end()) { d[i] = (d[i] + d[j] * d[num2idx[arr[i] / arr[j]]]) % base; } } } } long res = 0; for (int i = 0; i < n; ++i) { res = (res + d[i]) % base; } return res; } };
遍历做一下即可
class Solution { public: string toGoatLatin(string s) { string str=""; bool tag; char shuzu[]={'a','e','i','o','u','A','E','I','O','U'}; //元音字母放进数组中 vector<string> shuzu_str; for(int i=0;i<s.length();i++){ if(s[i]!=' '){ str+=s[i]; } else{ shuzu_str.push_back(str); str=""; } } shuzu_str.push_back(str); str=""; for(int i=0;i<shuzu_str.size();i++){ tag=false; for(char x:shuzu){ if(shuzu_str[i][0]==x){ tag=true; shuzu_str[i]+="ma"; break; } } if(!tag){ shuzu_str[i]+=shuzu_str[i][0]; shuzu_str[i]+="ma"; shuzu_str[i]=shuzu_str[i].substr(1,shuzu_str[i].length()-1); } } for(int i=0;i<shuzu_str.size();i++){ int j=i+1; while(j--){ shuzu_str[i]+="a"; } str+=shuzu_str[i]; if(i!=shuzu_str.size()-1) str+=" "; } return str; } };