【Leetcode】【Easy】Count and Say
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.
Note: The sequence of integers will be represented as a string.
解题思路:
第一个string为1,从第二个string开始,对前一个string的每一位进行操作,一位一位遍历,前后位相同,则记录当前数字数量,遇到前后不同,则对新的string进行写入。循环n-1次。
注意:
1、C++中string类型的正确操作;
2、C++中string和char*的区别和转换;
3、数字和字符Ascii码的转换方法;
特别注意:
代码中使用 n(int) + '0' 来表示'n',当且仅当n(int)<10时有效,因此如果sameDigCount超过10,也就是连续出现10个相同数字时,程序失败。不过可以轻松证明(证明略),连续出现的数字最多3个,也就是string串中最大的数字是3,不会超过4。因此代码是没有问题的。
class Solution {
public:
string countAndSay(int n) {
string currentStr = "";
if (n<=)
return "";
while (--n) {
string nextStr = "";
int sameDigCount = ;
int strlen = currentStr.length();
char preDigital = currentStr[];
for (int i=; i<strlen; ++i) {
if (currentStr[i] == preDigital) {
++sameDigCount;
} else {
nextStr.push_back(sameDigCount+'');
nextStr.push_back(preDigital);
preDigital = currentStr[i];
sameDigCount = ;
}
}
nextStr.push_back(sameDigCount+'');
nextStr.push_back(preDigital);
currentStr = nextStr;
}
return currentStr;
}
};
附录:
C++ string 操作总结
【Leetcode】【Easy】Count and Say的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode题意分析&解答】38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 【leetcode刷题笔记】Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
随机推荐
- signed char型内存位bit表示
signed char型内存 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9 ...
- ScriptManager.RegisterStartupScript()方法和Page.ClientScript.RegisterStartupScript() 方法详解
ScriptManager.RegisterStartupScript()方法 如果页面中不用Ajax,cs中运行某段js代码方式可以是: Page.ClientScript.RegisterStar ...
- Python学习-基础知识-2
目录 Python基础知识2 一.二进制 二.文字编码-基础 为什么要有文字编码? 有哪些编码格式? 如何解决不同国家不兼容的编码格式? unicode编码格式的缺点 如何既能全球通用还可以规避uni ...
- oracle系统包——DBMS_PIPE用法
DBMS_PIPE包用于在同一例程(实例)的不同会话之间进行通信:注意,如果用户要执行包dbms_pipe中的过程和函数,则必须要为用户授权. sql>conn sys/oracle as sy ...
- JavaScript自适应调整文字大小
JavaScript自适应调整文字大小 今天有个任务,发现页面上的数字由于太长而与其他数字重叠了.这个数字还不能像文字那样只显示一部分,必须全部显示.想了一些办法都不行,最后把超过1000变成1K,大 ...
- MySQL使用内置函数来进行模糊搜索(locate()等)
常用的一共有4个方法,如下: 1. 使用locate()方法 1.1.普通用法: SELECT `column` from `table` where locate('keyword', `condi ...
- <数据挖掘导论>读书笔记2
1.频率和众数 frequency(vi)=具有属性值vi的对象数/m 分类属性的众数mode是具有最高频率的值. 2.百分位数 3.位置度量:均值和中位数 4.散布度量:极差和方差 绝对平均偏差 A ...
- Linux修改BASH命令提示符
Shell命令提示符及颜色是由PS1来配置: 1.其中PS1常用的参数含义如下: \d :#代表日期,格式为weekday month date,例如:"Mon Aug 1" \H ...
- 巧用hover改变css样式和背景
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 利用WebBrowser控件实现百度自动搜索
(1)新建一个MFC对话框项目 (2)对话框中添加WebBrower控件,添加方法:点击菜单栏工具->选择工具箱项->在弹出的选择工具箱项对话框选择COM组件->Microsoft ...