解题思想:进行遍历然后对字符进行描述即可
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 = 1
for j in range(1,len(temp)):
if temp[j] == temp_num:
count +=1
else:
current = current+str(count)+str(temp_num)
temp_num = temp[j]
count = 1
current = current+str(count)+str(temp_num)
print(current)
temp = current
return 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;
}
};