国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

leecode 解題總結(jié):38 Count and Say

2019-11-10 17:04:54
字體:
供稿:網(wǎng)友
#include <iostream>#include <stdio.h>#include <vector>#include <sstream>using namespace std;/*問題:The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211.Given an integer n, generate the nth sequence.Note: The sequence of integers will be rePResented as a string.分析:此題本質(zhì)就是按照順序數(shù)出每個(gè)數(shù)的出現(xiàn)次數(shù),然后翻譯層字符串。1211:One 1,  One 2, Two 1,得到 111221111221:Three 1,Two 2,One 1,       31,22,11得到: 312211312211:One 3,Two 2,Two 1       13,22,21得到132221總結(jié)規(guī)律:將字符串分成多個(gè)部分,每一部分的值相同,數(shù)出每一部分對應(yīng)元素個(gè)數(shù)num,組成對應(yīng)值val新的子串: num val例如有11個(gè)2,那么變成112132221:11133211輸入:123456數(shù)出:111211211111221312211關(guān)鍵:1總結(jié)規(guī)律:將字符串分成多個(gè)部分,每一部分的值相同,數(shù)出每一部分對應(yīng)元素個(gè)數(shù)num,組成對應(yīng)值val新的子串: num val2 		for(int i = 1 ; i < len ; i++)		{			//找到相同元素			if(str.at(i) == value)			{				count++;			}			else			{				Result result(count , value);				results.push_back(result);				value = str.at(i);				count = 1;//計(jì)數(shù)器變成1,當(dāng)前數(shù)出現(xiàn)1次			}		}		//最后一個(gè)元素還沒有統(tǒng)計(jì),因?yàn)橹爸皇谴娣派弦淮蔚慕Y(jié)果		Result result(count , value);		results.push_back(result);*/typedef struct Result{	Result(int times , char value):_times(times), _value(value){}	int _times;	char _value;}Result;class Solution {public:	string getNum(string str)	{		if(str.empty())		{			return "";		}		int len = str.length();		//開始進(jìn)行數(shù)		char value = str.at(0);		int count = 1;		vector<Result> results;		for(int i = 1 ; i < len ; i++)		{			//找到相同元素			if(str.at(i) == value)			{				count++;			}			else			{				Result result(count , value);				results.push_back(result);				value = str.at(i);				count = 1;//計(jì)數(shù)器變成1,當(dāng)前數(shù)出現(xiàn)1次			}		}		//最后一個(gè)元素還沒有統(tǒng)計(jì),因?yàn)橹爸皇谴娣派弦淮蔚慕Y(jié)果		Result result(count , value);		results.push_back(result);		//拼接結(jié)果		int size = results.size();		stringstream resultStream;		for(int i = 0 ; i < size ; i++)		{			resultStream << results.at(i)._times << results.at(i)._value;		}		string realResult = resultStream.str();		return realResult;	}	//注意這里的n為第幾個(gè),初始元素為1    string countAndSay(int n) {		if(n <= 0)		{			return "";		}        //需要將整數(shù)轉(zhuǎn)換成字符串		string num("1");		if(1 == n)		{			return num;		}		string result;		for(int i = 2 ; i <= n ; i++)		{			result = getNum(num);			num = result;		}		return result;    }};void process(){	int n;	while(cin >> n)	{		Solution solution;		string result = solution.countAndSay(n);		cout << result << endl;	}}int main(int argc , char* argv[]){	process();	getchar();	return 0;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临西县| 兰坪| 尼勒克县| 庐江县| 右玉县| 芦山县| 恭城| 临西县| 南安市| 桃园市| 阿拉善右旗| 五河县| 平罗县| 唐海县| 华宁县| 汉中市| 陆丰市| 昆山市| 邮箱| 阜城县| 万州区| 峨边| 郑州市| 科尔| 西畴县| 会东县| 南召县| 霍州市| 福建省| 长子县| 车险| 大余县| 开鲁县| 云浮市| 会泽县| 白朗县| 太仓市| 太仆寺旗| 湖州市| 赤峰市| 阆中市|