字符串处理:动态规划,双指针,栈,计数,字符串流
本题要将一个格式化的字符串容器作为答案,可利用双指针和stringstream来进行操作
class Solution { public: vector<string> summaryRanges(vector<int>& nums) { int n = nums.size(); int l = 0, r = 0; vector<string> res; stringstream ss; while(r < n) { while(r + 1 < n && nums[r] + 1 == nums[r + 1]) r ++ ; if(l != r) { ss << nums[l] << "->" << nums[r]; res.push_back(ss.str()); ss.str(""); //每次操作完记得清空流内容 } else { res.push_back(to_string(nums[r])); } r ++ ; l = r; } return res; } };
标签:nums,int,stringstream,汇总,vector,228,字符串 From: https://www.cnblogs.com/zk6696/p/17658713.html