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. Python中三大框架各自的应用场景(DJango,flask,Tornado)

    django:主要是用来搞快速开发的,他的亮点就是快速开发,节约成本,正常的并发量不过10000,如果要实现高并发的话,就要对django进行二次开发,比如把整个笨重的框架给拆掉,自己写socket实 ...

  2. 洛谷$P2805\ [NOI2009]$植物大战僵尸 网络流

    正解:网络流 解题报告: 传送门$QwQ$ 题面好长昂,,,我大概概括下$QwQ$?有个$n\cdot m$的网格,每个格子有一株植物,击溃一株植物$(x,y)$需要付出$S_{(x,y)}$的代价( ...

  3. Http请求头安全策略

    今天在网上浪了许久,只是为了找一个很简单的配置,却奈何怎么都找不到. 好不容易找到了,我觉得还是记录下来的好,或许省得许多人像我一样浪费时间. 1.X-Frame-Options 如果网站可以嵌入到I ...

  4. 1087 有多少不同的值 (20 分)C语言

    当自然数 n 依次取 1.2.3.--.N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x 的最大自然数,即 x 的整数部分.) 输入格式: ...

  5. 1063 计算谱半径 (20 分)C语言

    在数学中,矩阵的"谱半径"是指其特征值的模集合的上确界.换言之,对于给定的 n 个复数空间的特征值 { a1+b​1​​ i,⋯,a​n​​ +b​n​​ i },它们的模为实部与 ...

  6. 6.python在windows下用批处理文件在运行中输入程序名直接运行的方法

    最近由于平时自由时间比较多,在看一本python入门书籍,在里面学习了一种用windows下的批处理文件在电脑运行界面中直接输入程序名称就可运行的方法,现将其详细说明如下: 1.首先编写一个教程上的程 ...

  7. schedule of 2016-09-26~2016-10-02(Monday~Sunday)——1st semester of 2nd Grade

    2016/9/26 Monday 1.make ppt for this afternoon's group meeting 2.ask teacher Xiqi&Liu some probl ...

  8. Shell脚本实现DB2数据库表导出到文件

    该Shell脚本用于实现将DB2数据库表导出到文件,将在另一篇博文<Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件>中通过Java代码实现调用该脚本并传入参数. #! ...

  9. GitHub项目绑定自己的域名

    github博客搭建:https://blog.csdn.net/walkerhau/article/details/77394659?utm_source=debugrun&utm_medi ...

  10. 一个DNS数据包的惊险之旅

    踏上旅程 “小子,快去查一下www.paypal.com的IP地址,我急用,晚了我弄你!”,暴躁老哥一把关上了门,留我一个DNS数据包在冷冰冰的房间. 过了一会儿,一位大叔打开了门,带着我来到了一座叫 ...