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. jquery中获取ajax请求返回数据的方法

    function getPageTotalAndDataTotal(page) { //设置一个变量用于接收ajax返回的值 var pageTotal = 0; // 获取页数与数据总数 $.aja ...

  2. Java程序员必备:异常的十个关键知识点

    前言 总结了Java异常十个关键知识点,面试或者工作中都有用哦,加油. 一. 异常是什么 异常是指阻止当前方法或作用域继续执行的问题.比如你读取的文件不存在,数组越界,进行除法时,除数为0等都会导致异 ...

  3. 使用svndumpfilter exclude来清理svn库的废弃文件实现差别备份

      先啰嗦下为什么要使用svndumpfilter… svn库用久了以后就会越来越大,进行整体文件打包备份的时候,发现压力山大…尤其是美术团队也在使用svn进行重要美术资源管理的时候…….几百g的资源 ...

  4. 从零开始のcocos2dx生活(四)ActionManager

    文章目录 初始化构造函数 析构函数 删除哈希元素 分配存放动作对象的空间 通过索引移除动作 暂停动作 恢复动作 暂停所有的动作 恢复所有的动作 添加动作 移除所有的动作 移除target中的所有动作 ...

  5. $Noip2018/Luogu5021$ 赛道修建 二分+树形

    $Luogu$ $Sol$ 一直以为是每个点只能经过一次没想到居然是每条边只能经过一次$....$ 首先其实这题$55$分的部分分真的很好写啊,分别是链,数的直径和菊花图,这里就不详细说了. 使得修建 ...

  6. $Luogu2680/NOIp2015$ 运输计划

    传送门 $Sol$ 最暴力的做法就是枚举最长链上的边,然后再算一次所有的链长,更新$ans$. 这里要求最大的最小,容易想到二分答案.对于二分的值$mid$,扫一遍所有的链,若链长小于等于$mid$, ...

  7. Java 迭代器须知 | “for each”与迭代器的关系

    Iterator接口包含4个方法: 通过反复调用next方法就可以逐个访问集合中的每个元素.需要注意,如果到达了集合的末尾,再次调用next方法将会抛出一个NoSuchElementException ...

  8. Python 打包——过去、现在与未来

    英文 | Python packaging - Past, Present, Future[1] 原作 | BERNAT GABOR 译者 | 豌豆花下猫 声明 :本文获得原作者授权翻译,转载请保留原 ...

  9. 「CodeForces 546B」Soldier and Badges 解题报告

    CF546B Soldier and Badges 题意翻译 给 n 个数,每次操作可以将一个数 +1,要使这 n 个数都不相同, 求最少要加多少? \(1 \le n \le 3000\) 感谢@凉 ...

  10. 1029 旧键盘 (20 分)C、Java、python

    题目描述 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及实际被输入的文字,请你列出 肯定坏掉的那些键. 输入描述: 输入在2行中分别给出应该输入的文 ...