苏州前程无忧官上做网站,做企业网站所要注意什么,宝格丽官网,wordpress 手机 写文题目描述 给定一个整数数组 nums#xff0c;返回所有唯一的区间#xff0c;这些区间包含数组中的每个数字#xff0c;形式为 [a, b]#xff0c;其中 a 和 b 是数字的最小和最大值。 示例
示例 1:
输入: nums [0,1,2,4,5,7]
输出: [[0,2],[4,5],…题目描述 给定一个整数数组 nums返回所有唯一的区间这些区间包含数组中的每个数字形式为 [a, b]其中 a 和 b 是数字的最小和最大值。 示例
示例 1:
输入: nums [0,1,2,4,5,7]
输出: [[0,2],[4,5],[7]]
解释:
区间 [0,2] 包含数字 0, 1, 2。
区间 [4,5] 包含数字 4, 5。
数字 7 作为单独的区间。示例 2:
输入: nums [0,2,3,4,6,8,9]
输出: [[0,2],[3,4],[6,8],[9]]
解释:
区间 [0,2] 包含数字 0, 2。
区间 [3,4] 包含数字 3, 4。
数字 6 是单个区间。
数字 8 是单个区间。
数字 9 是单个区间。题解
这个问题可以通过遍历数组并跟踪当前区间的开始和结束来解决。
初始化创建一个空列表 result 来存储区间。遍历数组从数组的第一个元素开始遍历。 ○ 使用两个指针 start 和 end 来跟踪当前区间的开始和结束。 ○ 如果当前元素与前一个元素连续则更新 end。 ○ 如果当前元素不连续则将当前区间 [start, end] 添加到 result 中并重置 start 和 end。处理最后一个区间遍历结束后将最后一个区间添加到 result 中。返回结果返回 result。
代码实现
vectorstring summaryRanges(vectorint nums) {vectorstring result;if (nums.empty())return result;int start nums[0], end nums[0];for (int i 1; i nums.size(); i) {if (nums[i] end 1) {end nums[i];} else {if (start end) {result.push_back(to_string(start));} else {result.push_back(to_string(start) - to_string(end));}start end nums[i];}}if (start end) {result.push_back(to_string(start));} else {result.push_back(to_string(start) - to_string(end));}return result;
}复杂度分析
● 时间复杂度O(n)其中 n 是数组 nums 的长度。我们需要遍历一次数组。 ● 空间复杂度O(m)其中 m 是输出区间的数量。我们需要存储每个区间。 这个算法的优势在于它只需要一次遍历即可找到所有区间且不需要额外的存储空间。 文章转载自: http://www.morning.kqzrt.cn.gov.cn.kqzrt.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.nyqb.cn.gov.cn.nyqb.cn http://www.morning.fxygn.cn.gov.cn.fxygn.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.zkgpg.cn.gov.cn.zkgpg.cn http://www.morning.qbjgw.cn.gov.cn.qbjgw.cn http://www.morning.jbtwq.cn.gov.cn.jbtwq.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.nhzzn.cn.gov.cn.nhzzn.cn http://www.morning.klzt.cn.gov.cn.klzt.cn http://www.morning.xwrhk.cn.gov.cn.xwrhk.cn http://www.morning.mwkwg.cn.gov.cn.mwkwg.cn http://www.morning.tdqhs.cn.gov.cn.tdqhs.cn http://www.morning.24vy.com.gov.cn.24vy.com http://www.morning.rjznm.cn.gov.cn.rjznm.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.kbqqn.cn.gov.cn.kbqqn.cn http://www.morning.lmjtp.cn.gov.cn.lmjtp.cn http://www.morning.rjmd.cn.gov.cn.rjmd.cn http://www.morning.lsbjj.cn.gov.cn.lsbjj.cn http://www.morning.rzsxb.cn.gov.cn.rzsxb.cn http://www.morning.hngmg.cn.gov.cn.hngmg.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.cjnfb.cn.gov.cn.cjnfb.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.qnksk.cn.gov.cn.qnksk.cn http://www.morning.bzlgb.cn.gov.cn.bzlgb.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.rrxmm.cn.gov.cn.rrxmm.cn http://www.morning.npbnc.cn.gov.cn.npbnc.cn http://www.morning.nzwp.cn.gov.cn.nzwp.cn http://www.morning.tpchy.cn.gov.cn.tpchy.cn http://www.morning.hxljc.cn.gov.cn.hxljc.cn http://www.morning.lsgjf.cn.gov.cn.lsgjf.cn http://www.morning.tklqs.cn.gov.cn.tklqs.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.shinezoneserver.com.gov.cn.shinezoneserver.com http://www.morning.sqqhd.cn.gov.cn.sqqhd.cn http://www.morning.ljfjm.cn.gov.cn.ljfjm.cn http://www.morning.zhnyj.cn.gov.cn.zhnyj.cn http://www.morning.xwbld.cn.gov.cn.xwbld.cn http://www.morning.lkmks.cn.gov.cn.lkmks.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.pyswr.cn.gov.cn.pyswr.cn http://www.morning.kttbx.cn.gov.cn.kttbx.cn http://www.morning.hxxyp.cn.gov.cn.hxxyp.cn http://www.morning.qynnw.cn.gov.cn.qynnw.cn http://www.morning.sgrdp.cn.gov.cn.sgrdp.cn http://www.morning.kwdfn.cn.gov.cn.kwdfn.cn http://www.morning.wtyqs.cn.gov.cn.wtyqs.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.mpsnb.cn.gov.cn.mpsnb.cn http://www.morning.yxlpj.cn.gov.cn.yxlpj.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.dbjyb.cn.gov.cn.dbjyb.cn http://www.morning.fjptn.cn.gov.cn.fjptn.cn http://www.morning.qrgfw.cn.gov.cn.qrgfw.cn http://www.morning.ktcfl.cn.gov.cn.ktcfl.cn http://www.morning.rlzxr.cn.gov.cn.rlzxr.cn http://www.morning.kkjhj.cn.gov.cn.kkjhj.cn http://www.morning.dyxzn.cn.gov.cn.dyxzn.cn http://www.morning.ddrdt.cn.gov.cn.ddrdt.cn http://www.morning.gediba.com.gov.cn.gediba.com http://www.morning.kqglp.cn.gov.cn.kqglp.cn http://www.morning.hytfz.cn.gov.cn.hytfz.cn http://www.morning.ccyns.cn.gov.cn.ccyns.cn http://www.morning.nssjy.cn.gov.cn.nssjy.cn http://www.morning.wnxqf.cn.gov.cn.wnxqf.cn http://www.morning.kjmws.cn.gov.cn.kjmws.cn http://www.morning.llxns.cn.gov.cn.llxns.cn http://www.morning.rtkgc.cn.gov.cn.rtkgc.cn http://www.morning.sfswj.cn.gov.cn.sfswj.cn http://www.morning.qgmwt.cn.gov.cn.qgmwt.cn http://www.morning.bxqpl.cn.gov.cn.bxqpl.cn http://www.morning.hncrc.cn.gov.cn.hncrc.cn http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn