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(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(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(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(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(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(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(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(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 / \ / \
随机推荐
- Oracle 11g如何清理数据库的历史日志详解
- jQuery scroll事件实现监控滚动条分页示例
- javascript 写类方式之八
- Python数据结构与算法之图结构(Graph)实例分析
- readonly和disabled的区别
- aspx 页面弹出窗口代码大全
- Docker基本命令使用详解(推荐)
- node.js操作mongodb学习小结
- Java游戏服务器之数据库表存取封装
- JavaScript的递归之递归与循环示例介绍
- Java面试题目集锦
- 详解Django中的ifequal和ifnotequal标签使用
- 详解有关easyUI的拖动操作中droppable,draggable用法例子
- jquery 动态合并单元格的实现方法
- 全面了解JavaScript对象进阶
- C#中如何使用 XmlReader 读取XML文件
- Android开发笔记之:用Enum(枚举类型)取代整数集的应用详解
- Java selenium处理极验滑动验证码示例
- Vue.js表单标签中的单选按钮、复选按钮和下拉列表的取值问题
- Python使用requests发送POST请求实例代码