【LeetCode】C# 38、Count and Say
来源:互联网 发布:大数据的特点包含 编辑:程序博客网 时间:2024/06/17 04:12
The count-and-say sequence is the sequence of integers with the first five terms as following:
- 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 term of the count-and-say sequence.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
Input: 1
Output: “1”
Example 2:
Input: 4
Output: “1211”
思路:利用StringBuilder.Append() 将prev字符串读写出来。完成一个循环后curr 给prev,进入下一个循环。
public class Solution { public string CountAndSay(int n) { StringBuilder curr=new StringBuilder("1"); StringBuilder prev; int count; char say; for (int i=1;i<n;i++){ prev=curr; curr=new StringBuilder(); count=1; say=prev[0]; for (int j=1;j<prev.Length;j++){ if (prev[j]!=say){ curr.Append(count).Append(say); count=1; say=prev[j]; } else count++; } curr.Append(count).Append(say); } return curr.ToString(); }}
阅读全文
0 0
- 【LeetCode】C# 38、Count and Say
- [LeetCode 38] Count and Say
- LeetCode(38) Count and Say
- [leetcode 38] Count and Say
- [Leetcode] 38 - Count and Say
- leetcode-38 count and say
- [LeetCode 38]Count and Say
- LeetCode 38 Count and Say
- leetcode-38 Count and Say
- Leetcode 38 Count and Say
- Leetcode---(38)Count and Say
- leetcode 38: Count and Say
- Leetcode# 38 Count and Say
- leetcode[38]:Count and Say
- leetcode-38 Count And Say
- [Leetcode]#38 Count and Say
- LeetCode 38 Count and Say
- leetcode 38:Count and Say
- layout-main
- 推荐给IT运维工程师必须学习的4本Linux书籍
- 文章标题
- NIO Selector源码分析
- HDU3038 How Many Answers Are Wrong 解题报告【带权并查集】
- 【LeetCode】C# 38、Count and Say
- layout-item_bottom
- 类找不到总结java.lang.ClassNotFoundException
- 染色问题的俩解法
- 深度摄像头测距原理简介
- sudo 命令直接添加密码到脚本
- 统计查询两列相同的sql语句
- Android自定义注解与注解器实现点击事件绑定
- clone的一知半解-Java