【Count and Say】cpp

题目:

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 countAndSay(int n) {
            string tmp1 = "1";
            string tmp2 = "";
            for ( size_t i = 1; i < n; ++i )
            {
                int digit_count = 1;
                for ( size_t j = 1; j < tmp1.size(); ++j )
                {
                    if ( tmp1[j]==tmp1[j-1] )
                    {
                        ++digit_count;
                    }
                    else
                    {
                        tmp2 += digit_count+‘0‘;
                        tmp2 += tmp1[j-1];
                        digit_count = 1;
                    }
                }
                tmp2 += digit_count+‘0‘;
                tmp2 += tmp1[tmp1.size()-1];
                tmp1 = tmp2;
                tmp2 = "";
            }
            return tmp1;
    }
};

tips:

这个题意不太好理解。

简单说就是:第n组字符串是第n-1组字符串的读法;读法的准则就是‘连续出现个数+数字’。

其余的就是处理一下边界case。

时间: 2024-12-11 07:58:37

【Count and Say】cpp的相关文章

【Pascal&#39;s Triangle】cpp

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 代码: class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector

【Single Num II】cpp

题目: Given an array of integers, every element appears three times except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 代码: class Solution { public: int s

【Jump Game II 】cpp

题目: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of j

【Path Sum II】cpp

题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 代码: /** * Definition f

【Longest Consecutive Sequence】cpp

题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run

【Linked List Cycle】cpp

题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 代码: 用hashmap版 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next

【Power of Two】cpp

题目: Given an integer, write a function to determine if it is a power of two. 代码: class Solution { public: bool isPowerOfTwo(int n) { if ( n<=0 ) return false; int countOne = 0; for ( int i=0; i<sizeof(n)*8 && countOne<=1; ++i ) { countOne

【Spiral Matrix II】cpp

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 代码: class Solution { public: vector<vector<

【Minimum Path Sum】cpp

题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 代码: class Solution { public: