当前位置:网站首页>Summary ranges of leetcode topic resolution
Summary ranges of leetcode topic resolution
2022-06-23 08:39:00 【ruochen】
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".
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
public List<String> summaryRanges(int[] nums) {
List<String> rt = new ArrayList<String>();
if (nums == null || nums.length == 0) {
return rt;
}
for (int i = 0; i < nums.length; i++) {
int st = nums[i];
int ed = st;
while (i + 1 < nums.length && nums[i + 1] - ed == 1) {
i++;
ed++;
}
if (ed == st) {
rt.add(st + "");
} else {
rt.add(st + "->" + ed);
}
}
return rt;
}边栏推荐
- 如何评价代码质量
- Single core driver module
- 1-gradients, shadows, and text
- How can I handle the "unable to load" exception when easyplayer plays webrtcs?
- Keng dad's "dedication blessing": red packet technology explosion in Alipay Spring Festival Gala
- 史上最污技术解读,60 个 IT 术语我居然秒懂了......
- 4- draw ellipse, use timer
- Go data types (II) overview of data types supported by go and Boolean types
- Code quality level 3 - readable code
- 7-palette-calayer and touch
猜你喜欢

Analysis of JMeter pressure measurement results

Point cloud library PCL from introduction to mastery Chapter 10

观察者模式

Multi-scale feature combination in target detection

After reading five books, I summarized these theories of wealth freedom

The first day of employment more than ten years ago

最常用的5中流ETL模式

高通9x07两种启动模式

走好数据中台最后一公里,为什么说数据服务API是数据中台的标配?

The rtsp/onvif protocol video platform easynvr startup service reports an error "service not found". How to solve it?
随机推荐
There are some limitations in cluster expansion and contraction
你有一串代码,但是不支持低版本Go时;判断Go版本号,您值得拥有!
Basic use of check boxes and implementation of select all and invert selection functions
Map接口及其子实现类
Lightweight UI control library worth collecting
Qualcomm 9x07 two startup modes
How to sort a dictionary by value or key?
Android kotlin coroutines KTX extension
Moodle e-learning platform fixes the session hijacking error that leads to pre authorized rce
6-shining laser application of calayer
TDesign update weekly report (the first week of January 2022)
C # advanced learning -- virtual method
9 ways in which network security may change in 2022
Generate code 39 extension code in batch through Excel file
走好数据中台最后一公里,为什么说数据服务API是数据中台的标配?
Go data types (II) overview of data types supported by go and Boolean types
3-progressbar and secondary cropping
Object. Defineproperty() and data broker
Hongmeng reads the resource file
List interface three sub implementation classes