<LeetCode OJ> 38. Count and Say(测试案例或有错)

38. Count and Say

My Submissions
Total Accepted: 66138  Total Submissions: 243196  Difficulty: Easy

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.

Subscribe to see which companies asked this question

Hide Tags
  String
Hide Similar Problems
  (M) Encode and Decode Strings

分析:

这题太凶残了,实在没能一下理解题目意思

别人的分析:每一个数字串,从最高位开始读起,对应每个数字的个数x个,其值为y;则记为xy;且每个数字串与其前一个数字串相关;比如111221就应该读成312211那么既然题意理解了,这个问题就简单了先将数字转换出字符,再将翻译(相邻的字符是否一样)成对应规则的字符串即可

class Solution {
	string numToString(int num)
	{
		string str = "";
		int carry = 0;
		while (num)
		{
			carry = num % 10;
			str += to_string(carry);
			num /= 10;
		}
		reverse(str.begin(),str.end());
		return str;
	}
public:
	string countAndSay(int n) {
		if (n == 0)
			return "";
		string str = numToString(n);//转换成相同顺序的字符串数字
		string ans = "";
		int i = 0;
		int curCount = 1;
		char curChar = str[i];
		i++;
		while (i<str.size())
		{
			if (str[i] == curChar)//一样
			{
				curCount++;
				i++;
			}
			else//不一样时就获取结果
			{
				ans += to_string(curCount) + curChar;
				curChar = str[i];
				curCount = 1;
				i++;
			}
		}
		ans += to_string(curCount) + curChar;//末尾字符串的追加
		return ans;
	}
};

LeetCode出错证据:

明明题目中说了1应该读作“11”的,这里却读成了1



我的程序的运行结果:



完整的测试程序:

#include "vector"  
#include <iostream>  
#include "fstream"
#include "algorithm"  
#include <stdio.h>
#include "string"
#include <cmath>
#include <cstdlib>
#include <unordered_set>

using namespace std;

class Solution {
	string numToString(int num)
	{
		string str = "";
		int carry = 0;
		while (num)
		{
			carry = num % 10;
			str += to_string(carry);
			num /= 10;
		}
		reverse(str.begin(),str.end());
		return str;
	}
public:
	string countAndSay(int n) {
		if (n == 0)
			return "";
		string str = numToString(n);//转换成相同顺序的字符串数字
		string ans = "";
		int i = 0;
		int curCount = 1;
		char curChar = str[i];
		i++;
		while (i<str.size())
		{
			if (str[i] == curChar)//一样
			{
				curCount++;
				i++;
			}
			else//不一样时就获取结果
			{
				ans += to_string(curCount) + curChar;
				curChar = str[i];
				curCount = 1;
				i++;
			}
		}
		ans += to_string(curCount) + curChar;//末尾字符串的追加
		return ans;
	}
};

int main()
{
	int n = 221;
	Solution s;
	while (n != 0)
	{
		cin >> n;
		cout << s.countAndSay(n) << endl;
	}
	return 0;
}


注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50460485

原作者博客:http://blog.csdn.net/ebowtang


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值