纪念一下C++ 11中的to_string()
来源:互联网 发布:arduino软件 编辑:程序博客网 时间:2024/06/10 15:47
今天发现C++ 11中推出了一个非常有用的函数 to_string() , 非常有效的解决了C++中其他类型(数字)转换成string。咱们再也不用麻烦的使用stringstream去转换了。贴上一个code,Leetcode中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.
class Solution {public: string countAndSay(int n) { int i=1; string val=to_string(1); while(i<n){ val=getSequence(val); i++; } return val; } string getSequence(string val){ int cnt=1; string newstr=""; int i; for(i=0;i<val.length()-1;i++){ if(val[i+1]==val[i]) cnt++; else{ newstr=newstr+to_string(cnt)+val[i]; //有木有看到这里闪亮的 to_string() ! cnt=1; } } newstr=newstr+to_string(cnt)+val[i]; return newstr; }};注意这里,nth个串意味着需要转换n-1次,而不是n次!
0 0
- 纪念一下C++ 11中的to_string()
- C++11中的to_string
- C++11中的string - to_string/stoi
- C++中的to_string()函数[C++11支持]
- 纪念一下,
- 纪念一下
- 纪念一下
- 纪念一下
- 纪念一下
- 纪念一下
- 纪念一下~
- 纪念一下
- 纪念一下
- 纪念一下
- 纪念一下
- 纪念一下
- 纪念一下
- 纪念一下
- 百分点内存数据库架构演变
- Oracle和Mysql分别生成sequence序列
- VLOOKUP函数的使用方法(高级篇)
- 面试经典(23)--圆圈中剩下的最后数字
- 百度定位API使用方法
- 纪念一下C++ 11中的to_string()
- ?(>_o)!
- Excel用得好,审计工作量至少可以降低20%以上啊。
- 最新Android 4.x 搭建开发环境
- FastReport 遇到的问题
- 红黑树的经典介绍
- 虚拟机ubuntu上安装JDK,及配置
- This function has none of DETERMINISTIC, NO SQL解决办法
- AIX5.3 上安装 python 2.7