C++实现LeetCode(228.总结区间)
[LeetCode] 228.Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges.
Example 1:
Input: [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.
Example 2:
Input: [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
这道题给定我们一个有序数组,让我们总结区间,具体来说就是让我们找出连续的序列,然后首尾两个数字之间用个“->"来连接,那么我只需遍历一遍数组即可,每次检查下一个数是不是递增的,如果是,则继续往下遍历,如果不是了,我们还要判断此时是一个数还是一个序列,一个数直接存入结果,序列的话要存入首尾数字和箭头“->"。我们需要两个变量i和j,其中i是连续序列起始数字的位置,j是连续数列的长度,当j为1时,说明只有一个数字,若大于1,则是一个连续序列,代码如下:
class Solution { public: vector<string> summaryRanges(vector<int>& nums) { vector<string> res; int i = 0, n = nums.size(); while (i < n) { int j = 1; while (i + j < n && (long)nums[i + j] - nums[i] == j) ++j; res.push_back(j <= 1 ? to_string(nums[i]) : to_string(nums[i]) + "->" + to_string(nums[i + j - 1])); i += j; } return res; } };
类似题目:
Data Stream as Disjoint Intervals
参考资料:
https://leetcode.com/problems/summary-ranges/
https://leetcode.com/problems/summary-ranges/discuss/63451/9-lines-c%2B%2B-0ms-solution
https://leetcode.com/problems/summary-ranges/discuss/63219/Accepted-JAVA-solution-easy-to-understand
到此这篇关于C++实现LeetCode(228.总结区间)的文章就介绍到这了,更多相关C++实现总结区间内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
相关推荐
-
C++实现LeetCode(163.缺失区间)
[LeetCode] 163. Missing Ranges 缺失区间 Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges. Example: Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99, Output: ["2&q
-
C++实现LeetCode(157.用Read4来读取N个字符)
[LeetCode] 157. Read N Characters Given Read4 用Read4来读取N个字符 Given a file and assume that you can only read the file using a given method read4, implement a method to read n characters. Method read4: The API read4 reads 4 consecutive characters from t
-
C++实现LeetCode(161.一个编辑距离)
[LeetCode] 161. One Edit Distance 一个编辑距离 Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 possiblities to satisify one edit distance apart: Insert a character into s to get t Delete a character from s
-
C++实现LeetCode(162.求数组的局部峰值)
[LeetCode] 162.Find Peak Element 求数组的局部峰值 A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index. The array may contain multiple peaks, in that c
-
C++实现LeetCode(158.用Read4来读取N个字符之二 - 多次调用)
[LeetCode] 158. Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用 Given a file and assume that you can only read the file using a given method read4, implement a method read to read n characters. Your method read may be ca
-
C++实现LeetCode(904.水果装入果篮)
[LeetCode] 904. Fruit Into Baskets 水果装入果篮 In a row of trees, the `i`-th tree produces fruit with type `tree[i]`. You start at any tree of your choice, then repeatedly perform the following steps: Add one piece of fruit from this tree to your baskets.
-
C++实现LeetCode(159.最多有两个不同字符的最长子串)
[LeetCode] 159. Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串 Given a string s , find the length of the longest substring t that contains at most 2 distinct characters. Example 1: Input: "eceba" Output: 3 Explanation: ti
-
C++实现LeetCode(160.求两个链表的交点)
[LeetCode] 160.Intersection of Two Linked Lists 求两个链表的交点 Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1
-
C++实现LeetCode(228.总结区间)
[LeetCode] 228.Summary Ranges 总结区间 Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: [0,1,2,4,5,7] Output: ["0->2","4->5","7"] Explanation: 0,1,2 form a continuous ran
-
C++实现LeetCode(57.插入区间)
[LeetCode] 57. Insert Interval 插入区间 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Input: int
-
C++实现LeetCode(56.合并区间)
[LeetCode] 56. Merge Intervals 合并区间 Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into
-
C++实现LeetCode(132.拆分回文串之二)
[LeetCode] 132.Palindrome Partitioning II 拆分回文串之二 Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. Example: Input: "aab" Output: 1 Explan
-
C++实现LeetCode(131.拆分回文串)
[LeetCode] 131.Palindrome Partitioning 拆分回文串 Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Example: Input: "aab" Output: [ ["aa","b"
-
C++实现LeetCode(5.最长回文子串)
[LeetCode] 5. Longest Palindromic Substring 最长回文子串 Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: "babad" Output: "bab" Note: "aba" is als
-
C++实现LeetCode(36.验证数独)
[LeetCode] 36. Valid Sudoku 验证数独 Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: Each row must contain the digits 1-9without repetition. Each column must contain the digits 1-9 wi
-
C++实现LeetCode(37.求解数独)
[LeetCode] 37. Sudoku Solver 求解数独 Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must
-
C++实现LeetCode(87.搅乱字符串)
[LeetCode] 87. Scramble String 搅乱字符串 Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / \ gr eat / \ / \
随机推荐
- C#提取PPT文本和图片的实现方法
- Python中的Numpy入门教程
- 整理的比较全的一句话后门代码(方面大家查找后门)
- AngularJS 作用域详解及示例代码
- iOS如何裁剪圆形头像
- iOS中Block的回调使用和解析详解
- python脚本实现xls(xlsx)转成csv
- 跨浏览器通用、可重用的选项卡tab切换js代码
- Wordpress php 分页代码
- mac系统下为 php 添加 pcntl 扩展
- php 无限级分类 获取顶级分类ID
- js实现消息滚动效果
- 系统时间改为2000年 修改系统时间病毒
- Android中实现淘宝购物车RecyclerView或LIstView的嵌套选择的逻辑
- Android Listview上下拉动刷新tab滑动切换功能
- MySQL中的基本查询语句学习笔记
- Java使用JDBC向MySQL数据库批次插入10W条数据(测试效率)
- document.documentElement和document.body区别介绍
- SREng用法简要说明图文教程(如何获得日志/删启动项目/服务/驱动/BHO等)
- jQuery获得内容和属性方法及示例