38. Count and Say

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 1s".注意输入的是一个整数,输出的是字符串。 但是在qt编译时不能使用pop_back () ,back(),所以只能用尾指针来做了。

 string Solution::countAndSay(int n)
{
string result,str_temp;
if (n==)
{return NULL;}
if(n==)
{result.push_back(+''); return result;}
while (n)
{
char temp_a;
temp_a=n%+'';
str_temp.push_back(temp_a);
n=n/;
}
int count;
count=;
string::iterator str_back=str_temp.end()-;
char temp_char;
temp_char=*str_back;
str_back=str_temp.erase(str_back)-;
while(!str_temp.empty())
{
if (*str_back!=temp_char || str_temp.empty())
{result.push_back(count+'');result.push_back(temp_char);count=;temp_char=*str_back;}
else
{count++;}
str_back=str_temp.erase(str_back)-;
}
result.push_back(count+'');
result.push_back(temp_char);
return result;
}

代码敲进去之后发现题意理解错误^o^. 题意是让我们产生一个序列,这个序列是输入1,然后读1个1,得到11,然后读两个1,得到21,然后读1个2,一个1,得到1211,然后读1个1,一个2,两个1 得到111221...... 重新来过:输入n的含义是,给定整数n输出第n个序列,如果n=3,那么先读1个3,得到13,然后读1个1,1个3 ,得到1113,输出即可。

 string Solution::countAndSay(int n) {
string result,str_temp;
if (n==){return NULL;}
if(n==){result.push_back(+'');return result;}
int n_copy;
n_copy=n;
while (n)
{
    string temp_a;
    temp_a.push_back(n%+'');
     str_temp.insert(,temp_a);
    n=n/;
}
  cout<<"str_temp"<<str_temp<<endl;
  int count;
  char temp_char;
  while(n_copy>)
 {
    result.clear();
    count=;
    temp_char=str_temp.at();
    str_temp.erase( str_temp.begin());
    while(!str_temp.empty())
    {
     if (str_temp.at()!=temp_char )
      {result.push_back(count+'');result.push_back(temp_char);count=;temp_char=str_temp.at();}
      else
       {count++;}
     str_temp.erase(str_temp.begin());
    }
    result.push_back(count+'');
    result.push_back(temp_char);
   str_temp=result;
n_copy--;
}
return result; }

结果又理解错误,应该是输入一定是1的序列然后读取第n个而已。稍作更改:

string Solution::countAndSay(int n) {
string result,str_temp;
if (n==)
{return NULL;}
if(n==)
{
result.push_back(+'');
return result;
}
int n_copy;
n_copy=n;
//while (n)
//{
// string temp_a;
// temp_a.push_back(n%10+'0');
// str_temp.insert(0,temp_a);
//n=n/10;
//}
str_temp.push_back(+'');
cout<<"str_temp"<<str_temp<<endl;
int count;
char temp_char;
while(n_copy>)
{
result.clear();
count=;
temp_char=str_temp.at();
str_temp.erase( str_temp.begin());
while(!str_temp.empty())
{
if (str_temp.at()!=temp_char )
{result.push_back(count+'');result.push_back(temp_char);count=;temp_char=str_temp.at();}
else
{count++;}
str_temp.erase(str_temp.begin());
}
result.push_back(count+'');
result.push_back(temp_char);
str_temp=result;
n_copy--;
}
return result; }

Leetcode 题目整理-8 Count and Say的更多相关文章

  1. Leetcode 题目整理 climbing stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  2. Leetcode 题目整理-3 Palindrome Number & Roman to Integer

    9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. clic ...

  3. Leetcode 题目整理-1

    1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...

  4. 【leetcode题目整理】数组中找子集

    368. Largest Divisible Subset 题意:找到所有元素都不同的数组中满足以下规则的最大子集,规则为:子集中的任意两个元素a和b,满足a%b=0或者b%a=0. 解答:利用动态规 ...

  5. Leetcode 题目整理 Sqrt && Search Insert Position

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...

  6. Leetcode 题目整理-7 Remove Element & Implement strStr()

    27. Remove Element Given an array and a value, remove all instances of that value in place and retur ...

  7. Leetcode 题目整理-6 Swap Nodes in Pairs & Remove Duplicates from Sorted Array

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  8. Leetcode 题目整理-5 Valid Parentheses & Merge Two Sorted Lists

    20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  9. Leetcode 题目整理-4 Longest Common Prefix & Remove Nth Node From End of List

    14. Longest Common Prefix Write a function to find the longest common prefix string amongst an array ...

随机推荐

  1. Java_地铁购票系统

    定义了两个类,在Subway类中定义三个私有数据变量,线路号,经过站点,换乘站.以及4个方法分别实现从txt文件中导入线路信息:输出线路信息:查询两个站点经过站点数,并输出经过站点以及在某站换乘几号线 ...

  2. 003eop常见问题设置

  3. mysql:数据库与实例的区别

    题记:最近想更深入的了解mysql,所以买了一些书在学习,趁着这个机会开个坑,整理一下一些我认为重要的知识点. 刚工作那会经常能听到组长提到实例这个词,一开始我以为是服务器... 数据库(databa ...

  4. 从零开始のcocos2dx生活(十)ScrollView

    目录 简介 基础变量 ScrollViewDelegate Direction _dragging _container _touchMoved _bounceable _touchLength 方法 ...

  5. $CH$3801 $Rainbow$的信号 期望+位运算

    正解:位运算 解题报告: 传送门! 其实就是个位运算,,,只是顺便加了个期望的知识点$so$期望的帕并不难来着$QwQ$ 先把期望的皮扒了,就直接分类讨论下,不难发现,答案分为两个部分 $\left\ ...

  6. Python 打包的现状:包的三种类型

    英文 | The state of Python Packaging[1] 原作 | BERNAT GABOR 译者 | 豌豆花下猫 声明 :本文获得原作者授权翻译,转载请保留原文出处,请勿用于商业或 ...

  7. 【Python3爬虫】反反爬之解决前端反调试问题

    一.前言 在我们爬取某些网站的时候,会想要打开 DevTools 查看元素或者抓包分析,但按下 F12 的时候,却出现了下面这一幕: 此时网页暂停加载,也就没法运行代码了,直接中断掉了,难道这就能阻止 ...

  8. Redis内存碎片清理

    当Redis中清理了大量的Key之后原先Redis申请的内存(used_memory_rss)将继续持有而不会释放,此时查看内存信息将会看到存在大量的内存碎片.那么,Redis的内存碎片可以清理么,该 ...

  9. GPU图形绘制管线简介

    (阅读GPU+编程与CG+语言之阳春白雪下里巴人所得总结) GPU图形绘制管线是描述GPU渲染(把三维世界显示为屏幕上的二维图像)的流程,主要分为三个主要阶段应用程序阶段.几何阶段.光栅阶段. 1.应 ...

  10. 如何利用Map2Shp进行快速格式转换

    有时,用户仅需要进行GIS数据格式的简单转换,对文字注记.制图表达.投影信息无特别要求,可进行快速格式转换.做为MapGIS文件与Shape文件间的格式转换工具,Map2Shp软件操作过程十分简单,只 ...