'commit'
This commit is contained in:
55
C++专题课程/到LeedCode刷题/leetcode基础篇/38.报数.cpp
Normal file
55
C++专题课程/到LeedCode刷题/leetcode基础篇/38.报数.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
/*/*******************************************************************************
|
||||
** **
|
||||
** Jiedi(China nanjing)Ltd. **
|
||||
** 创建:丁宋涛 夏曹俊,此代码可用作为学习参考 **
|
||||
*******************************************************************************/
|
||||
|
||||
/*****************************FILE INFOMATION***********************************
|
||||
**
|
||||
** Project : 算法设计与编程实践---基于leetcode的企业真题库
|
||||
** Contact : xiacaojun@qq.com
|
||||
** 博客 : http://blog.csdn.net/jiedichina
|
||||
** 视频课程 : 网易云课堂 http://study.163.com/u/xiacaojun
|
||||
腾讯课堂 https://jiedi.ke.qq.com/
|
||||
csdn学院 https://edu.csdn.net/course/detail/25037
|
||||
** 51cto学院 http://edu.51cto.com/lecturer/index/user_id-100013755.html
|
||||
** 老夏课堂 http://www.laoxiaketang.com
|
||||
**
|
||||
** 算法设计与编程实践---基于leetcode的企业真题库 课程群 :296249312 加入群下载代码和交流
|
||||
** 微信公众号 : jiedi2007
|
||||
** 头条号 : 夏曹俊
|
||||
**
|
||||
*****************************************************************************
|
||||
//!!!!!!!!! 算法设计与编程实践---基于leetcode的企业真题库 课程 QQ群:296249312 下载代码和交流*/
|
||||
/*
|
||||
* @lc app=leetcode.cn id=38 lang=cpp
|
||||
*
|
||||
* [38] 报数
|
||||
* 1211:1个1,1个2,2个1
|
||||
* 111221:3个1,2个2,1个1
|
||||
* 对于前一个数,找出相同元素的个数
|
||||
*/
|
||||
#include <string>
|
||||
using namespace std;
|
||||
class Solution {
|
||||
public:
|
||||
string countAndSay(int n) {
|
||||
if(n<=0)
|
||||
return "";
|
||||
string res = "1";
|
||||
while(--n){
|
||||
string cur ="";
|
||||
for(int i =0;i<res.size();i++){
|
||||
int cnt = 1;
|
||||
while(i+1<res.size() && res[i] == res[i+1]){
|
||||
cnt++;
|
||||
i++;
|
||||
}
|
||||
cur += to_string(cnt)+res[i];
|
||||
}
|
||||
res = cur;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user