zoukankan      html  css  js  c++  java
  • 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"].

     1 class Solution {
     2 public:
     3     vector<string> summaryRanges(vector<int>& nums) {
     4         int n=nums.size();
     5         int i;
     6         int left,right;
     7         char str[100];
     8         left=0;
     9         right=0;
    10         vector<string> res;
    11         if(n<1)
    12           return res;
    13         for(i=0;i<n;i++)
    14         {
    15             if(i==0)
    16             {
    17                 left=nums[i];
    18                 right=left;
    19             }
    20             else if(right+1==nums[i])
    21             {
    22                 right++;
    23             }
    24             else
    25             {
    26                 if(left!=right)
    27                 {
    28                 sprintf(str,"%d->%d",left,right);
    29                 string tmp(str);
    30                 res.push_back(tmp);
    31                 }
    32                 else
    33                 {
    34                     sprintf(str,"%d",left);
    35                     string tmp(str);
    36                     res.push_back(tmp);
    37                 }
    38                 left=nums[i];
    39                 right=left;
    40                 
    41             }
    42         }
    43          if(left!=right)
    44                 {
    45                 sprintf(str,"%d->%d",left,right);
    46                 string tmp(str);
    47                 res.push_back(tmp);
    48                 }
    49                 else
    50                 {
    51                     sprintf(str,"%d",left);
    52                     string tmp(str);
    53                     res.push_back(tmp);
    54                 }
    55         return res;
    56     }
    57 };
  • 相关阅读:
    偶然闪退的排查
    ANTI-INFLAMMATORY FOODS
    健康餐
    PSORIASIS-7 THINGS TO AVOID
    eczema: improve or reverse your eczema
    VL10 or VL10A前台操作
    装修设计
    春运高速记录 1/15/2020
    rman 备份并异机恢复
    RMAN冷备份、一致性备份脚本
  • 原文地址:https://www.cnblogs.com/zl1991/p/4670118.html
Copyright © 2011-2022 走看看