GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/algorithms/cpp/summaryRanges/SummaryRanges.cpp at master · weimo/leetcode Expand file tree/ Copy pathSummaryRanges.cppMore file actions 55 lines (45 loc) · 1.62 KB / Copy pathSummaryRanges.cppFile metadata and controls55 lines (45 loc) · 1.62 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 // Source : https://leetcode.com/problems/summary-ranges/ // Author : Hao Chen // Date : 2015-07-03 /********************************************************************************** * * Given a sorted integer array without duplicates, return the summary of its ranges. * * For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. * * Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. * **********************************************************************************/ class Solution { public: string makeRange(int start, int end) { ostringstream oss; if (start != end) { oss << start << "->" << end; } else { oss << start; } return oss.str(); } vector<string> summaryRanges(vector<int>& nums) { vector<string> result; int len = nums.size(); if (len == 0) return result; // we have two pointer for range-starter and range-ender int start=nums[0], end=nums[0]; for (int i=1; i<len; i++) { // if it is continous number, move the end pointer; if (nums[i] == end + 1) { end = nums[i]; continue; } //if the number is not continous, push the range into result //and reset the start and end pointer result.push_back(makeRange(start, end)); start = end = nums[i]; } //for the last range result.push_back(makeRange(start, end)); return result; } }; |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |