本文主要是介绍Leetcode 038 Count and Say (模拟),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目连接:Leetcode 038 Count and Say
解题思路:模拟题目的思路,对每个串生成它的下一个串。
class Solution {public:string countAndSay(int n) {string s = "1";for (int i = 1; i < n; i++) {int num = 1;char c = s[0], tmp_c;string t = "", tmp_s;for (int j = 1; j < s.size(); j++) {if (s[j] != c) {tmp_s = "";while (num) {tmp_c = '0' + num % 10;tmp_s = tmp_s + tmp_c;num /= 10;}t += tmp_s + c;num = 1;c = s[j];} elsenum++;}tmp_s = "";while (num) {tmp_c = '0' + num % 10;tmp_s = tmp_s + tmp_c;num /= 10;}t += tmp_s + c;s = t;}return s;}
};
这篇关于Leetcode 038 Count and Say (模拟)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!