标签:
称号:
Missing Ranges
Given a sorted integer array where the range of elements are [0, 99]inclusive, return its missing ranges.
For example, given [0, 1, 3, 50, 75], return[“2”, “4->49”, “51->74”, “76->99”]
void generate_string(int l, int r, vector<string> &res) { if (l>r || l < 0 || r>99) return; if (l == r) res.push_back(to_string(l)); else { string s1 = to_string(l),s2="->",s3=to_string(r); res.push_back(s1+s2+s3); } } vector<string> MissingRanges(const vector<int> &nums) { vector<string> res; if (nums.empty()) { generate_string(0, 99, res); return res; } if (nums.size() == 1) { generate_string(0, nums[0]-1, res); generate_string(nums[0]+1, 99, res); return res; } generate_string(0, nums[0] - 1, res); for (int i = 1; i < nums.size(); ++i) { generate_string(nums[i-1]+1, nums[i] - 1, res); } generate_string(nums.back()+1, 99, res); return res; }
版权声明:本文博主原创文章,博客,未经同意不得转载。
标签:
原文地址:http://www.cnblogs.com/yxwkf/p/4831991.html