zoukankan      html  css  js  c++  java
  • Missing Ranges -- LeetCode

    Given a sorted integer array where the range of elements are [lowerupper] inclusive, return its missing ranges.

    For example, given [0, 1, 3, 50, 75]lower = 0 and upper = 99, return ["2", "4->49", "51->74", "76->99"].

     1 class Solution {
     2 public:
     3     string printRange(int lower, int upper) {
     4         string low = std::to_string(lower);
     5         string up = std::to_string(upper);
     6         if (lower == upper) return low;
     7         return low + "->" + up;
     8     }
     9     vector<string> findMissingRanges(vector<int>& nums, int lower, int upper) {
    10         vector<string> res;
    11         if (nums.size() == 0) res.push_back(printRange(lower, upper));
    12         else if (nums.front() > lower) res.push_back(printRange(lower, nums.front() - 1));
    13         for (int i = 1, n = nums.size(); i < n; i++)
    14             if (nums[i] > nums[i-1] + 1)
    15                 res.push_back(printRange(nums[i-1] + 1, nums[i] - 1));
    16         if (nums.size() > 0 && nums.back() < upper)
    17             res.push_back(printRange(nums.back() + 1, upper));
    18         return res;
    19     }
    20 };
  • 相关阅读:
    xfire for web-Service
    如何使用 XSD
    XSD
    一个 XSD 实例
    RE:转:一些不常用的html代码
    <base target="_self"/>标签的用法
    C#有关日期的使用方法
    GridView 高亮某一行
    DropDownList绑定数据库
    Request.Querystring中文乱码问题解决
  • 原文地址:https://www.cnblogs.com/fenshen371/p/5779167.html
Copyright © 2011-2022 走看看