本文主要是介绍leetcode记录——Count and Say,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
char* countAndSay(int n) {char s1[10000]="1", s2[10000]="1";//动态数组不会,c++的话直接用string push_back(),刚开始用的100,1000 提示run time,
//char *s 指向常量字符串,内容不能修改,初始化后即确定大小,出现了stack around the variable 's' was corrupted数组越界问题;char s[] 指向栈空间;int i = 0, j, k, m;int temp;
// *(s1 + 0) = '1';
// *(s1 + 1) = '\0';while (i < n-1)//判断是否迭代了Nth{j = 0;//遍历s1k = 0;//生成s1 的s2while (*(s1 + j) != '\0'){m = 1;//s1数字重复个数temp = *(s1 + j)-48;//字符和数字的关系 小心忘记j++;while ((*(s1 + j)-48) == temp){m++;j++;}if (m){*(s2 + k) = m + 48;k++;}*(s2 + k) = temp+48;k++;}*(s2 + k) = '\0';k = 0;/* while (*(s2 + k) != '\0'){*(s1 + k) = *(s2 + k);k++;}k++;*(s1 + k) = '\0';一堆废话*/strcpy(s1,s2);i++; }return s1;
}
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.
首先,推理一下规律,很简单,就是重复的数字的个数+重复的数字。但是只能写出脑残的程序。
这篇关于leetcode记录——Count and Say的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!