本文主要是介绍力扣题:数字与字符串间转换-12.18,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
力扣题-12.18
[力扣刷题攻略] Re:从零开始的力扣刷题生活
力扣题1:38. 外观数列
解题思想:进行遍历然后对字符进行描述即可
class Solution(object):def countAndSay(self, n):""":type n: int:rtype: str"""if n==1:return "1"temp = "1"for i in range(n-1):current = ""temp_num = temp[0]count = 1for j in range(1,len(temp)):if temp[j] == temp_num:count +=1else:current = current+str(count)+str(temp_num)temp_num = temp[j]count = 1current = current+str(count)+str(temp_num)print(current)temp = currentreturn current
class Solution {
public:string countAndSay(int n) {if (n == 1) {return "1";}std::string temp = "1";for (int i = 0; i < n - 1; ++i) {std::string current = "";char temp_num = temp[0];int count = 1;for (int j = 1; j < temp.length(); ++j) {if (temp[j] == temp_num) {count += 1;} else {current += std::to_string(count) + temp_num;temp_num = temp[j];count = 1;}}current += std::to_string(count) + temp_num;std::cout << current << std::endl;temp = current;}return temp;}
};
这篇关于力扣题:数字与字符串间转换-12.18的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!