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. ELK学习实验011:Logstash工作原理

    Logstash事件处理管道包括三个阶段:输入→过滤器→输出.输入会生成事件,过滤器会对其进行修改,输出会将它们发送到其他地方.输入和输出支持编解码器,使您可以在数据进入或退出管道时对其进行编码或解码 ...

  2. 计算机专业如何高质量的走完大学四年?毕业成为Offer收割机

    前言:迷茫本就是青春该有的模样,但不要让未来的你讨厌现在的自己. "就要毕业了. 回头看自己所谓的大学生活, 我想哭,不是因为离别,而是因为什么都没学到. 我不知,简历该怎么写,若是以往我会 ...

  3. 【小技巧】在PS中测量图层间的边距

    今天学到了一个小技巧,前端切页面时会很方便,就是测量间距margin的. 在ps中,选中某个图层,然后按住ctrl键,再移动鼠标,就可以出现这个图层距其他元素的边距,这个太方便了.在此记录一下,免的以 ...

  4. 洛谷$P$2518 计数 $[HAOI2010]$ 数位$dp$

    正解:数位$dp$ 解题报告: 传送门$w$ 感觉省选的数位$dp$还是比较有质量的辣,,,至少有一定的思维难度是趴$QwQ$ 这题要考虑到一个,我认为比较关键的点,就,对于一个位数不满的数,可以理解 ...

  5. 图解leetcode279 —— 完全平方数

    每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于  ...

  6. python的一些高阶用法

    map的用法 def fn(x): return x*2 L1 = [1,2,3,4,5,6] L2 = list(map(fn,L1)) L2 [2, 4, 6, 8, 10, 12] 通过上面的运 ...

  7. Django简介、安装和入门

    python三大主流Web框架 Django 优点:大而全,自身携带的组件和功能特别特别多,类似于航空母舰 缺点:过于笨重,所需功能不多时,Django依然提供这些功能,占据内存 Flask 优点:小 ...

  8. Python 处理Excel内的数据(案例介绍*2)

    (一)案例一介绍 现在有一匹电商产品跟当日销量的数据,如下,总共有上万笔的数据,现在需要统计每个品牌当日的销售量,比如美宝莲今天总共卖出了多少的商品,另外需要统计每个品牌下面的每个子品类当日销售量(品 ...

  9. red note8 pro谷歌套件

    谷歌核心Apps(即Google官方应用“全家桶”),包括YouTube,Google Now,Google Play store,Google Play Games,Google Maps等: 基于 ...

  10. 《图解机器学习-杉山将著》读书笔记---CH4

    CH4 带有约束条件的最小二乘法 重点提炼 提出带有约束条件的最小二乘学习法的缘故:   左图中可见:一般的最小二乘学习法有个缺点----对于包含噪声的学习过程经常会过拟合 右图:有了空间约束之后,学 ...