LeetCode
String
2011. 执行操作后的变量值
class Solution {public: int finalValueAfterOperations(vector& operations) { int num = 0; for(string i : operations){ if(i[1] == '+') num++; else num--; } return num; }};
2114. 句子中的最多单词数
class Solution {public: int mostWordsFound(vector& sentences) { vectorv; for(string str : sentences){ int count = 0; for(int i = 0; i < str.size(); i++){ if(str[i] == ' '){ count++; } } v.push_back(count); } sort(v.begin(), v.end()); return v.back() + 1;; }};
1689. 十-二进制数的最少数目
class Solution {public: int minPartitions(string n) { return *max_element(n.begin(), n.end()) - '0'; }};
2194. Excel 表中某个范围内的单元格
class Solution {public: vector cellsInRange(string s) { vector ans; for (char a = s[0]; a <= s[3]; a++) { for (char b = s[1]; b <= s[4]; b++) { string tmp = {a, b}; ans.push_back(tmp); } } return ans; }};
剑指 Offer II 085. 生成匹配的括号
class Solution {public: vector res; vector generateParenthesis(int n) { backtrack(n,0,""); return res; } void backtrack(int left,int right,string path){ if(!left&&!right){res.push_back(path);return;} if(left)backtrack(left-1,right+1,path+'('); if(right)backtrack(left,right-1,path+')'); }};
771. 宝石与石头
class Solution {public: int numJewelsInStones(string jewels, string stones) { int count = 0; for(int i = 0; i < jewels.size(); i++){ for(int j = 0; j < stones.size(); j++){ if(jewels[i] == stones[j]){ count++; } } } return count; }};
1614. 括号的最大嵌套深度
class Solution {public: int maxDepth(string s) { int ans = 0, size = 0; for (char ch : s) { if (ch == '(') { ++size; ans = max(ans, size); } else if (ch == ')') { --size; } } return ans; }};