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 ...
随机推荐
- android 学习第一步
今天是2015年7月24号,今年下半年的主要学习方向是android,学习的目标是做出3个或以上的有实用价值的app.
- 关于ueditor1.4.3版复制section标签丢失class和style样式问题
在复制微信的文章格式到ueditor时发现section标签中的style和class属性丢失,严重影响美观. 原文格式,排版清晰段落分明赏心悦目: 复制到ueditor后的格式...这跟原文是没法比 ...
- bt种子文件文件结构
估计80%以上接触互联网的人都知道bt是什么东西,任何一个用bt下载的人都知道这样一个概念,种子.bt种子就是记录了p2p对等网络中tracker, nodes, files等信息,也就是说,这个 ...
- hibernate 一张数据表的流程
1. 写一个domain类来映射数据库表 2. 写一个*.hbm.xml文件来配置映射 <?xml version="1.0"?> <!DOCTYPE hiber ...
- Promise 让异步更优
每个异步方法都返回一个Promise 更优雅. then方法 每一个Promise 都有一个叫then 的方法, 接受一对callback 被解决时调用,resolve, 被拒绝 reje ...
- codeforces C. Valera and Tubes
http://codeforces.com/contest/441/problem/C 题意:有n×m个方格,然后把这些方格分成k部分,每个部分内的方格的坐标满足|xi - xi + 1| + |yi ...
- Spring Framework Reference,Documentation,spring英文文档.pdf 官方文档
直接上链接:http://files.cnblogs.com/files/kongkaikai/spring-framework-reference.pdf 官网链接:http://docs.spri ...
- android 随手记 广播通知栏 二
关于通知栏的使用: Notification及NotificationManager的使用详解 相关类: import android.app.NotificationManager; import ...
- Qt入门(9)——Qt中的线程支持
Qt对线程提供了支持,基本形式有独立于平台的线程类.线程安全方式的事件传递和一个全局Qt库互斥量允许你可以从不同的线程调用Qt方法.警告:所有的GUI类(比如,QWidget和它的子类),操作系统核心 ...
- jQuery DOM操作之结点转移复制
jQuery DOM操作之结点转移复制 $('div').append($('p'))这样即可把p标签移动到div标签里 $('div').append( $('p').html() )是把p标签里的 ...