麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

leecode 解題總結:38 Count and Say

2019-11-10 19:15:07
字體:
來源:轉載
供稿:網友
#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.分析:此題本質就是按照順序數出每個數的出現次數,然后翻譯層字符串。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總結規律:將字符串分成多個部分,每一部分的值相同,數出每一部分對應元素個數num,組成對應值val新的子串: num val例如有11個2,那么變成112132221:11133211輸入:123456數出:111211211111221312211關鍵:1總結規律:將字符串分成多個部分,每一部分的值相同,數出每一部分對應元素個數num,組成對應值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;//計數器變成1,當前數出現1次			}		}		//最后一個元素還沒有統計,因為之前只是存放上一次的結果		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();		//開始進行數		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;//計數器變成1,當前數出現1次			}		}		//最后一個元素還沒有統計,因為之前只是存放上一次的結果		Result result(count , value);		results.push_back(result);		//拼接結果		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為第幾個,初始元素為1    string countAndSay(int n) {		if(n <= 0)		{			return "";		}        //需要將整數轉換成字符串		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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲码无人客一区二区三区 | 欧美一级无毛 | 亚洲伊人色欲综合网 | 激情亚洲一区二区 | 日本精品黄色 | 激情宗合 | 91av大片| 日韩黄色av网站 | 亚洲欧美一区二区三区在线观看 | 国产乱色精品成人免费视频 | 精国品产一区二区三区有限公司 | 麻豆传传媒久久久爱 | 久久国产精品电影 | 亚洲人成网站免费播放 | 黄色成人小视频 | 毛片视频网站在线观看 | 国产精品久久久久久久hd | 国产精品久久久久久影视 | 极品xxxx欧美一区二区 | 中国产一级毛片 | 大奶一级片| 午夜小网站 | 黄色网络免费看 | 黄视频免费在线观看 | 亚洲精品a级 | 国产一精品一av一免费爽爽 | 在线成人一区二区 | 欧美zoofilia杂交videos | 亚洲午夜天堂吃瓜在线 | 91嫩草丨国产丨精品入口 | 国产88久久久国产精品免费二区 | 色蜜桃av | 亚洲成人黄色片 | 久久99国产精品久久 | 成码无人av片在线观看网站 | 91成人免费在线观看 | 久久精品观看 | 一边吃奶一边插下面 | 欧美成人精品h版在线观看 国产一级淫片在线观看 | 视频一区二区中文字幕 | xxxxhdhdhdhd日本 |