C#解leetcode 228. Summary Ranges Easy
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"].
我的代码,结果Accepted,507ms:
public class Solution
{
public IList<string> SummaryRanges(int[] nums)
{
int start = , end = ;//设定输出格式中的开始值和结束值
string str = "";
List<string> mList = new List<string>();//实例化一个List对象
if (nums.Length != )//如果数组长度不为0,也就是数组不为空
{
start = nums[];//设置开始值的初值
end = nums[];//设置结束值的初值
for (int i = ; i < nums.Length; i++)
{
if (i == nums.Length - )//如果是最后一次循环
{
if (start != end)//当开始值和结束值不相等的时候
{
str = start + "->" + end;
mList.Add(str);//在list列表中加入str
}
else
{
mList.Add(start.ToString());//直接将开始值加入list
}
}
else//如果不是最后一次循环
{
if (nums[i] + == nums[i + ])//如果相邻两个数相差为1
{
end = nums[i + ];//将nums[i+1]赋值给结束值
}
else//如果两个数不是相邻的整数
{
if (start != end)//当开始值和结束值不相等的时候
{
str = start + "->" + end;
mList.Add(str);
end = nums[i + ];
start = nums[i + ];
}
else//当开始值和结束值相等的时候
{
mList.Add(start.ToString());
end = nums[i + ];
start = nums[i + ];
}
}
}
}
}
return mList;
}
}
在discuss中看到了一个代码结果Accepted,512ms
public class Solution
{
public IList<string> SummaryRanges(int[] nums)
{
List<string> list =new List<string>();
for(int i=;i<nums.Length;i++)
{
int a=nums[i];
while(i+<nums.Length&&nums[i]+==nums[i+])
i++;
if(a==nums[i])
list.Add(a.ToString());
else
list.Add(a+"->"+nums[i]);
}
return list;
}
}
其中Ilist<string>可以理解为一个只能存放string类型的Arraylist.
总结,这是我在leetcode上做的第一道题,虽然很简单,但是按照我这个渣渣水平依然想了1个半小时,其中出现了n多bug,不过最终终于有了一个Accept的版本,虽然这个版本写的很罗嗦,废话很多,远远没有别人写的精炼,但是我要向着更加精炼的算法代码前进,向前人学习!
C#解leetcode 228. Summary Ranges Easy的更多相关文章
- [LeetCode] 228. Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- (easy)LeetCode 228.Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- LeetCode 228. Summary Ranges (总结区间)
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- Java for LeetCode 228 Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- LeetCode(228) Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- Java [Leetcode 228]Summary Ranges
题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...
- [leetcode]228. Summary Ranges区间统计
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
随机推荐
- iOS 取得CGimage字节数据的方法
通过我在网上搜索和总结,目前看来,我发现两种比较方便的方式. 1. CGImage -> CGDataProvider -> CFData -> xx * CGDataProvide ...
- 实战EntityFramework
删除对象一定要在同一个context 我尝试这在两个方法中使用两个context(Container)实例来进行一个获得一个删除,结果我获得的”The object cannot be deleted ...
- Rust语言:安全地并发
http://www.csdn.net/article/2014-02-26/2818556-Rust http://www.zhihu.com/question/20032903 Rust是近两年M ...
- ios开发学习--歌词处理--解析lrc文件
我觉得要想解析lrc 首先大家应该了解一下lrc文件的结构,大家可以去看一下**百科 我这里粗略的写一下: ■ 时间标签(Time-tag) 形式为"[mm:ss]"(分钟数:秒数 ...
- POJ3087 Shuffle'm Up(模拟)
题目链接. AC代码如下; #include <iostream> #include <cstdio> #include <cstring> #include &l ...
- 【离线】【深搜】【树】Codeforces 707D Persistent Bookcase
题目链接: http://codeforces.com/problemset/problem/707/D 题目大意: 一个N*M的书架,支持4种操作 1.把(x,y)变为有书. 2.把(x,y)变为没 ...
- Subsets —— LeetCode
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- mac下的改装人生——制作mac os 启动盘
我即将开始对我的mac进行彻底的改造,现在还需要的是一个mac的启动盘或者启动光盘.由于没钱买mac的安装光盘或者安装盘,只能网上下了一个镜像自己做启动盘~ 需要:装有Mac Os的电脑,至少8g的u ...
- HDU2059(龟兔赛跑)
龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 终止imp/exp和expdp/impdp进程运行的方法
一.停止EXP/IMP优化速度 可以直接KILL 进程,但先要KILL 父进程,然后KILL子进程,只KILL子进程,EXP/IMP还会在后台执行的 样例:ps -ef |grep imp 查询到pi ...