C# implementation


  • 0
    C

    C# implementation

            public IList<string> SummaryRanges(int[] nums)
            {
                List<string> res = new List<string>();
    
                for (int i = 0; i < nums.Length; i++)
                {
                    int start = nums[i];
    
                    while (i + 1 < nums.Length && (nums[i + 1] - nums[i] == 1))
                    {
                        i++;
                    }
    
                    if (start != nums[i])
                    {
                        res.Add(string.Format("{0}->{1}", start, nums[i]));
                    }
                    else
                    {
                        res.Add(start.ToString());
                    }
                }
    
                return res;
            }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.