小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

leetcode試題1

 _小女子_ 2015-06-28

原題:https:///problems/summary-ranges/


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"].

C++答案:


#include "stdafx.h"
using namespace std;
#include "vector"
#include <iostream> 

class Sloution{
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> result;
string s = "";
int j = 0;
int end_flag = 0;
int beg_flag = 0;
bool f = false;
while (beg_flag<nums.size()){
if (nums.size()>1 && beg_flag+1<nums.size()){
char t[16];
if ((nums.at(beg_flag + 1) - nums.at(beg_flag) == 1)){
sprintf_s(t, "%d->", nums.at(beg_flag));
s = t;
}
else{
sprintf_s(t, "%d", nums.at(beg_flag));
result.push_back(t);
f = true;
beg_flag++;
end_flag++;
continue;
}
}
for (int i = beg_flag; i < nums.size(); i++){
f = false;
if (i + 1 >= nums.size()){
if (!f){
char t2[16];
sprintf_s(t2, "%d", nums.at(beg_flag));
s.append(t2);
result.push_back(s);
s = "";
f = false;
}
beg_flag++;
break;
}
if ((nums.at(i + 1) - nums.at(i) == 1)){
f = false;
end_flag ++;
beg_flag = end_flag ;
}
else{
j++;
//numsArray.resize(j);
if (!f){
char t1[16];
sprintf_s(t1, "%d", nums.at(end_flag));
s.append(t1);
result.push_back(s);
s = "";
f = false;
}
//numsArray.push_back({ nums.at(beg_flag), nums.at(end_flag) });
beg_flag = end_flag+1;
end_flag = beg_flag;
break;
}
}
}
return result;
}

};
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> nums = { 1 ,3,4,5,7};
vector<int> nums1 = { 0,1};
vector<int> nums2 = { 1 };
Sloution s;
vector<string> result=s.summaryRanges(nums);
for (int i = 0; i < result.size(); i++){
printf("%s", result.at(i).c_str());
//cout << result.at(i);
}
int i = 0;
cin >> i;
return 0;
}


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多