标签:剑指offer
#include<cstdio> #include<vector> #include<algorithm> using namespace std; class Solution { public: bool IsContinuous( vector<int> numbers ) { if(numbers.size()<1) return false; int m=0;//记录间隔 int cnt=0; sort(numbers.begin(),numbers.end()); int i; for(i=0;i<numbers.size();i++) { if(numbers[i]==0){cnt++;} else break; } for(i=i+1;i<numbers.size();i++) { if(numbers[i]-numbers[i-1]==0) return false; int t=numbers[i]-numbers[i-1]-1; if(t) m+=t; } if(cnt>=m) return true; else return false; } }; int main() { Solution so; vector<int> arr; arr.push_back(2); arr.push_back(5); arr.push_back(7); arr.push_back(9); arr.push_back(0); arr.push_back(0); arr.push_back(0); if(so.IsContinuous(arr)) printf("true\n"); else printf("false\n"); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:剑指offer
原文地址:http://blog.csdn.net/u010579068/article/details/49047399