[leetcode]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.

 题意大致意思是根据每个数字的多少构成下一个数字,解题关键在于对每一位的数字进行标记,明确每个数字出现次数,这样 次数+数字构成下一次数字。

该代码思路思路比较直接,通过统计数字的个数进行输出。

class Solution {
public:
    string read2sayArray(int n)
    {
          string s="1";
          string strtemp;
           char temp;
        for(int i=0;i<n-1;i++)
        {
            int len=1;
           for(size_t i = 0;i<s.size();)
           {
               if(s[i+len]!=‘\0‘)//不是最后一个字符
               {
                 if(s[i]==s[i+len])//同一个字符
                  {
                       len=len+1; //继续查看下一个字符
                       continue;
                   }
                 else//不等 输出统计结果
                   {
                      temp=len+‘0‘;
                      strtemp+=temp;
                       strtemp+=s[i];
                     i=i+len;//继续下面的查找
                     len=1;
                   }
               }
               else//结束
               {
                    temp=len+‘0‘;
                    strtemp+=temp;
                     strtemp+=s[i];
                   len=1;
                   break;
               }
           }
           s=strtemp;
           strtemp="";
        }
      return s;
    }
};

  在leetcode上面看到直接用STL函数的,简洁不少。

class Solution {
public:
  string countAndSay(int n)
        {
            string s("1");
             while (--n)
                s = getNext(s);
               return s;
        }
    string getNext(const string &s) {
               stringstream ss;
               for (auto i = s.begin(); i != s.end(); ) {
                auto j = find_if(i, s.end(), bind1st(not_equal_to<char>(), *i));
//bind1st找到不相等的位置输出
                ss << distance(i, j) << *i;                //distance 返回二者迭代器之间距离
                i = j;
                 }
               return ss.str();
            }
};

  两种思路的验证结果是相同的。

结论:利用STL提高代码可读性和效率;

   find_if(i, s.end(), bind1st(not_equal_to<char>(), *i));查找返回和迭代器i不相等的位置

时间: 2024-10-03 14:55:48

[leetcode]Count and Say的相关文章

LeetCode: Count and Say [037]

[题目] 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. G

LeetCode: 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

[LeetCode]Count Primes

题目:Count Primes 统计1-n的素数的个数. 思路1: 通常的思想就是遍历(0,n)范围内的所有数,对每个数i再遍历(0,sqrt(i)),每个除一遍来判断是否为素数,这样时间复杂度为O(n*sqrt(n)). 具体实现不在贴代码,过程很简单,两重循环就可以解决.但是效率很差,n较大时甚至会花几分钟才能跑完. 思路2: 用埃拉特斯特尼筛法的方法来求素数,时间复杂度可以达到O(nloglogn). 首先开一个大小为n的数组prime[],从2开始循环,找到一个质数后开始筛选出所有非素数

[LeetCode]Count of Range Sum

题目:Count of Range Sum Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive. Note:A naive algorithm

LeetCode Count Univalue Subtrees

原题链接在这里:https://leetcode.com/problems/count-univalue-subtrees/ Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of the subtree have the same value. For example:Given binary tree, 5 / 1 5 / \ 5 5 5 retur

[LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99]) Hint: A direct

leetcode Count of Smaller Numbers After Self

题目连接 https://leetcode.com/problems/count-of-smaller-numbers-after-self/ Count of Smaller Numbers After Self Description You are given an integer array nums and you have to return a new counts array. The counts array has the property where $counts[i]$

LeetCode -- Count Digit One

题目描述: Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 本题目纯粹是找规律求解. 该实现参考了:

[LeetCode] Count Univalue Subtrees 计数相同值子树的个数

Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of the subtree have the same value. For example:Given binary tree, 5 / 1 5 / \ 5 5 5 return 4. 这道题让我们求相同值子树的个数,就是所有节点值都相同的子树的个数,之前有道求最大BST子树的题Largest BST

LeetCode Count and Say 数数字

1 class Solution { 2 public: 3 string countAndSay(int n) { 4 if(n==1) return "1"; 5 string str0="",str1="1"; 6 int i,t,count; 7 char c='*'; 8 for(i=0;i<n-1;i++){ //一共要数n-1次,假如n=2,那么只要数str1这一次就行了 9 count=1; 10 if(i%2!=0){ /