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. Kafka 集群在马蜂窝大数据平台的优化与应用扩展

    马蜂窝技术原创文章,更多干货请订阅公众号:mfwtech Kafka 是当下热门的消息队列中间件,它可以实时地处理海量数据,具备高吞吐.低延时等特性及可靠的消息异步传递机制,可以很好地解决不同系统间数 ...

  2. 洛谷$P5446\ [THUPC2018]$绿绿和串串 $manacher$

    正解:$manacher$ 解题报告: 传送门$QwQ$ 考虑这个操作的实质是啥$QwQ$?其实就,变成以最后一个节点为回文中心的回文子串嘛$QwQ$.显然就先跑个马拉车再说呗$QwQ$. 然后接着考 ...

  3. 「SP25784」BUBBLESORT - Bubble Sort 解题报告

    SP25784 BUBBLESORT - Bubble Sort 题目描述 One of the simplest sorting algorithms, the Bubble Sort, can b ...

  4. 「CF86D」Powerful array 解题报告

    题面 给出一个\(n\)个数组成的数列\(a\),有\(t\)次询问,每次询问为一个\([l,r]\)的区间,求区间内每种数字出现次数的平方×数字的值 的和 思路: 直接上莫队咯 然后就T了 没学过莫 ...

  5. web前端安全——常见的web攻击方法

    面试题:你所了解的web攻击? 1.xss攻击 2.CSRF攻击 3.网络劫持攻击 4.控制台注入代码 5.钓鱼 6.DDoS攻击 7.SQL注入攻击 8.点击劫持 一.xss攻击 XSS攻击:跨站脚 ...

  6. 手写vue observe数据劫持

    实现代码: class Vue { constructor(options) { //缓存参数 this.$options = options; //需要监听的数据 this.$data = opti ...

  7. 扫描器是如何判定有xss漏洞的

    这个问题,看似简单,实则触及到很多人的知识盲区 我们都知道,弹窗就能判定这个页面存在xss, 那么扫描器是怎么判断的呢,或者说扫描器是怎么判断是否弹窗的呢 测试发现 当响应的头中content-typ ...

  8. scratch3.0二次开发scratch3.0基本介绍(第一章)

    为什么要自己开发而不使用官方版本? 这个问题要看我们的做少儿编程教育的需求是怎么样的. scratch本身提供了离线版本以及官网在线平台供我们使用,这足以满足我们对于编程教学模块的需求.但是对于一些教 ...

  9. 简单介绍HTTP的请求(get请求和post请求)以及对应的响应的内容

    链接解析: https://oa.hbgf.net.cn/login.jsp;jsessionid=47084322738F8DB18D60752944DFD1AA http或者https表示使用的是 ...

  10. 打包一份py给大家用!!!

    好不容易写完了代码,却发现对面无法使用自己的python代码,其无奈可想而知 在这里就给大家介绍一下pyinstaller的简单用法 你所需要的就是在电脑上安装好 https://blog.csdn. ...