38. Count and Say
来源:互联网 发布:pdf锐化软件 编辑:程序博客网 时间:2024/05/22 15:47
转载出处不记得了,因为是前几天网上找到的代码,在VS里面运行过,今天重新翻出来学习。刚才试着搜索了一下,没找到原始链接。感谢原创者。
public string CountAndSay(int n) { if (n <= 0) { return string.Empty; } if (n == 1) { return "1"; } n = n - 1; var result = "1"; for (var i = 0; i < n; i++) { var r = string.Empty; var len = result.Length; var count = 1; for (var j = 1; j < len + 1; j++) { if (j < len && result[j] == result[j - 1]) { count++; } else { r += string.Format("{0}{1}", count, result[j - 1]); count = 1; } } result = r; } return result; }
Soulution 2:http://www.cnblogs.com/etcow/archive/2012/08/29/2661290.html
Soulution 3:https://segmentfault.com/a/1190000003849544
Soulution 4:根据3的方法微量调整
public class Solution { public string CountAndSay(int n) { if(n<=0) return ""; if(n==1) return "1"; string s=CountAndSay(n-1); char last=s[0]; int cnt=1; StringBuilder sb=new StringBuilder();; for(int i=1;i<s.Length;i++) { if(s[i]==last) cnt++; else { sb.Append(cnt); sb.Append(last); last=s[i]; cnt=1; } } sb.Append(cnt); sb.Append(last); return sb.ToString(); }}
阅读全文
0 0
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- 38. Count and Say
- poj3020-Antenna Placement(最小路径覆盖问题,最大匹配)
- Dubbo+Zookeeper+Spring整合应用篇
- codechef November Challenge 2017解题报告
- 算法基础之基础数据结构
- 笔记(三)
- 38. Count and Say
- 文件的存取
- HDU-2577 How to Type (线性状态dp)
- 数据结构--单向链表2
- 转载logback简要用法 原文地址https://www.cnblogs.com/warking/p/5710303.html
- Kafka
- 【STL】vector小结
- HDU-1241 Oil Deposits 【dfs】
- hdu 2141