leecode 解题总结:38 Count and Say
来源:互联网 发布:汽车修理软件破解版 编辑:程序博客网 时间:2024/06/07 21:14
#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;}
0 0
- leecode 解题总结:38 Count and Say
- Leecode- 38. Count and Say
- count and say 解题
- [38]Count and Say
- 38Count and Say
- 38Count and Say
- 38、Count and Say
- [38] Count and Say
- 38-Count And Say
- LeetCode Count and Say 解题报告
- LeetCode解题报告-- Count and Say
- [leetcode] 38. Count and Say 解题报告
- [Leetcode] 38. Count and Say 解题报告
- 【LeetCode】Count and Say 解题报告
- 38. Count and Say解题思路
- leetcode解题方案--038--count and say
- [LeetCode 38] Count and Say
- LeetCode(38) Count and Say
- 简单字符串比较 (sdut oj)
- Docker -- 系统整洁之道 -- 0
- PAT甲级.1012. The Best Rank (25)
- linux crontab使用和查询
- 机器学习经典算法之KNN及python实现
- leecode 解题总结:38 Count and Say
- iOS真机调试
- java--01--final
- Shader(固定管线着色器)
- JSON数据格式(一)
- webpack 入门
- C++ STL中Map的按Key排序和按Value排序
- 文章标题
- linux下使用find xargs grep查找文件及文件内容