LeetCode Count and Say
2015-06-25 21:00
447 查看
Description:
The count-and-say sequence is the sequence of integers beginning as follows:
Given an integer n, generate the nth sequence.
Solution:
Just simply simulate this procedure.
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1is read off as
"one 1"or
11.
11is read off as
"two 1s"or
21.
21is read off as
"one 2, then
one 1"or
1211.
Given an integer n, generate the nth sequence.
Solution:
Just simply simulate this procedure.
public class Solution { public String countAndSay(int n) { String ans = "1", temp; char pre; int num; for (int i = 1; i < n; i++) { temp = ""; ans = ans + " "; pre = ans.charAt(0); num = 1; for (int j = 1; j < ans.length(); j++) { if (ans.charAt(j) != pre) { temp = temp + num + pre; num = 1; pre = ans.charAt(j); } else { num++; } } ans = temp; } return ans; } }
相关文章推荐
- C++头文件定义规则
- 【转载】图论 500题——主要为hdu/poj/zoj
- WP8.1发送Post或Get请求顺带文件上传
- 理解Flux机制和应用
- caffe windows Matlab
- 回调函数 作业2
- 初探CSS 4选择器
- 计算某月某日为一年中第几天
- poj 2457 Part Acquisition 【spfa最短路 + STL路径输出】
- 字符串的排列组合算法合集
- Xcode6.x如何不使用storyboard
- 关于“被密码保护”的文章
- [Tyvj 2869]血缘关系
- Linux源文件夹结构呈现
- 字符串的全排列问题
- 定制类自己的的new_handler
- JSON 数组格式
- 晨间日记的奇迹
- win7删除无效的Ubuntu选项
- 第1天-sql基本概念及安装与添加用户