zoukankan      html  css  js  c++  java
  • 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 = 0, end = 0;//设定输出格式中的开始值和结束值
                    string str = "";
                    List<string> mList = new List<string>();//实例化一个List对象
                    if (nums.Length != 0)//如果数组长度不为0,也就是数组不为空
                    {
                        start = nums[0];//设置开始值的初值
                        end = nums[0];//设置结束值的初值
                        for (int i = 0; i < nums.Length; i++)
                        {
                            if (i == nums.Length - 1)//如果是最后一次循环
                            {
                                if (start != end)//当开始值和结束值不相等的时候
                                {
                                    str = start + "->" + end;
                                    mList.Add(str);//在list列表中加入str
                                }
                                else
                                {
                                    mList.Add(start.ToString());//直接将开始值加入list
                                }
                            }
                            else//如果不是最后一次循环
                            {
                                if (nums[i] + 1 == nums[i + 1])//如果相邻两个数相差为1
                                {
                                    end = nums[i + 1];//将nums[i+1]赋值给结束值
                                }
                                else//如果两个数不是相邻的整数
                                {
                                    if (start != end)//当开始值和结束值不相等的时候
                                    {
                                        str = start + "->" + end;
                                        mList.Add(str);
                                        end = nums[i + 1];
                                        start = nums[i + 1];
                                    }
                                    else//当开始值和结束值相等的时候
                                    {
                                        mList.Add(start.ToString());
                                        end = nums[i + 1];
                                        start = nums[i + 1];
                                    }
                                }
                            }
                        }
                    }
                    return mList;
                }
            }

     在discuss中看到了一个代码结果Accepted,512ms

    public class Solution 
    {
        public IList<string> SummaryRanges(int[] nums)
        {
            List<string> list =new List<string>();
          for(int i=0;i<nums.Length;i++)
          {
              int a=nums[i];
              while(i+1<nums.Length&&nums[i]+1==nums[i+1])
              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的版本,虽然这个版本写的很罗嗦,废话很多,远远没有别人写的精炼,但是我要向着更加精炼的算法代码前进,向前人学习!

  • 相关阅读:
    *Basic Calculator
    今天周末20190616,分享句喜欢的话,常用架构图
    Python 面向对象 --- 类和对象
    Python 面向对象(OOP)基本概念
    Python 高级变量类型 --- 函数的进阶
    Python 高级变量类型 --- 变量进级
    Python 高级变量类型 --- 综合应用
    Python 高级变量类型 --- 公共方法
    Python 高级变量类型 --- 字典
    Python 高级变量类型 --- 元组
  • 原文地址:https://www.cnblogs.com/xiaohua92/p/5259190.html
Copyright © 2011-2022 走看看