文章目录
- z字形变幻
- 外观数列
- 数青蛙
题目均来自于力扣
z字形变幻
class Solution {
public:string convert(string s, int numRows) {int n = s.size();if(n <= numRows || numRows == 1) return s;int d = 2 * numRows - 2;string res;for(int j = 0; j < n; j += d){res += s[j]; }for(int i = 1; i < numRows - 1; ++i){ int left = d - 2 * i;int righ = d - left;bool flag = true;for(int j = i; j < n;){res += s[j];if(flag) j += left;else j+= righ;flag = !flag;}}for(int j = numRows - 1; j < n; j += d){res += s[j]; }return res;}
};
外观数列
class Solution {
public:string countAndSay(int n) {string res = "1";for(int i = 1; i < n; ++i){string tmp;int left = 0, right = 0;for(; right < res.size(); ++right){if(res[right] != res[left]){tmp += to_string(right - left) + res[left];left = right;}}tmp += to_string(right - left) + res[left];res = tmp;}return res;}
};
数青蛙
class Solution {
public:int minNumberOfFrogs(string cF) {int hash[130];for (int i = 0; i < cF.size(); i++){if(cF[i] != 'c'){if ((cF[i] == 'r' && hash['c'] > 0) ||(cF[i] == 'o' && hash['r'] > 0) ||(cF[i] == 'a' && hash['o'] > 0) ||(cF[i] == 'k' && hash['a'] > 0)){ // crookif(cF[i] == 'r') hash['c']--;if(cF[i] == 'o') hash['r']--;if(cF[i] == 'a') hash['o']--;if(cF[i] == 'k') hash['a']--;hash[cF[i]]++;}else return -1;}else{if(hash['k'] > 0)hash['k']--;hash['c']++;}}for(int ch = 0; ch < 130; ch++){if(ch != 'k' && hash[ch] != 0) return -1;}return hash['k'];}
};