当前位置:网站首页>[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
边栏推荐
- Pytorch learning record (7): skills in processing data and training models
- Preparedstatement prevents SQL injection
- MySQL best practices for creating tables
- Example of ticket selling with reentrant lock
- Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention
- Event listener
- Linear algebra Chapter 2 - matrices and their operations
- Problems and solutions of database migration
- Kingdee EAS "general ledger" system calls "de posting" button
- PyTorch入门小笔记——利用简单例子观察前向传播各个层输出的size
猜你喜欢
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
A general U-shaped transformer for image restoration
Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising
去噪论文——[Noise2Void,CVPR19]Noise2Void-Learning Denoising from Single Noisy Images
Guaba and Computational Geometry
Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
编程记录——图片旋转函数scipy.ndimage.rotate()的简单使用和效果观察
Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison
MySQL table constraints and table design
随机推荐
7.Domino piling
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi
lambda expressions
Event listener
Implementation of displaying database pictures to browser tables based on thymeleaf
Programming training
[transfer] MySQL: how many rows of data can InnoDB store in a B + tree?
Algèbre linéaire chapitre 1 - déterminants
线性代数第一章-行列式
线性代数第二章-矩阵及其运算
Gaussian processes of sklearn
Failure to deliver XID in Seata distributed transaction project
In depth source code analysis servlet first program
Kalman filter and inertial integrated navigation
Integration and induction of knowledge points of automatic control principle (Han min version)
Pytorch learning record (III): structure of neural network + using sequential and module to define the model
You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it
How does MySQL convert stored seconds into dates
Kingdee EAS "general ledger" system calls "de posting" button