当前位置:网站首页>[leetcode 228] summary interval
[leetcode 228] summary interval
2022-04-23 06:23:00 【Don't steal my energy】
Title Description
Given a No repeating elements Of Orderly An array of integers nums .
return Just over all the numbers in the array Of Minimum order List of ranges . in other words ,nums Every element of is just covered by an interval , And there's no existence that belongs to a certain range but doesn't belong to nums The number of x .
Each range in the list [a,b] It should be output in the following format :
1. "a->b" , If a != b
2. "a" , If a == b
Example 1:
Input :nums = [0,1,2,4,5,7]
Output :["0->2","4->5","7"]
explain : The range is :
[0,2] --> "0->2"
[4,5] --> "4->5"
[7,7] --> "7"
Example 2:
Input :nums = [0,2,3,4,6,8,9]
Output :["0","2->4","6","8->9"]
explain : The range is :
[0,0] --> "0"
[2,4] --> "2->4"
[6,6] --> "6"
[8,9] --> "8->9"
Using double pointers to achieve , What needs to be noted is the condition of crossing the boundary .
vector<string> summaryRanges(vector<int>& nums) {
vector<string> ans;
string s;
int len=nums.size();
for(int i=0,j=0;j<len;j++){
// Double pointer We need to consider the problem of cross-border
if(j+1<len && nums[j]!=nums[j+1]-1){
if(j-i==0){
s=to_string(nums[i]);
}
else{
s=to_string(nums[i])+"->"+to_string(nums[j]);
}
ans.push_back(s);
s="";
i=j+1;
}
else if(j+1==len){
// The border has been reached
if(j-i==0){
s=to_string(nums[i]);
}
else{
s=to_string(nums[i])+"->"+to_string(nums[j]);
}
ans.push_back(s);
}
}
return ans;
}
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617012176.html
边栏推荐
猜你喜欢

Addition, deletion, modification and query of MySQL table
![Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images](/img/9d/487c77b5d25d3e37fb629164c804e2.png)
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images

Pyqt5 learning (I): Layout Management + signal and slot association + menu bar and toolbar + packaging resource package
![图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi](/img/1b/4eea05e2634780f45b44273d2764e3.png)
图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi

线代第四章-向量组的线性相关

LDCT图像重建论文——Eformer: Edge Enhancement based Transformer for Medical Image Denoising

Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)

St table template

检测技术与原理

Substring Inversion (Easy Version)
随机推荐
7.Domino piling
JDBC connection database
4. Print form
Implementation of displaying database pictures to browser tables based on thymeleaf
Paper on Image Restoration - [red net, nips16] image restoration using very deep revolutionary encoder decoder networks wi
H. Are You Safe? Convex hull naked problem
3. Continuous integer
Development environment EAS login license modification
线代第四章-向量组的线性相关
Explain of MySQL optimization
Delete and truncate
PHP processing JSON_ Decode() parses JSON stringify
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising
Protected (members modified by protected are visible to this package and its subclasses)
Gaussian processes of sklearn
The attendance client date of K / 3 wise system can only be selected to 2019
Stability building best practices
Guaba and Computational Geometry
线性代数第一章-行列式