C# solution


  • 0
    Q

    C# solution

    public class Solution {
        public IList<string> SummaryRanges(int[] nums) {
            var result = new List<string>();
            if(nums.Length == 0) return result; 
            var endI = 0;
            for(int i = 0; i< nums.Length; i++){
                // last item
                if(i != nums.Length -1) 
                    if(nums[i+1]-nums[i] ==1) continue;
                
                var range = GetRange(endI, i, nums);
                endI = i+1;
                result.Add(range);
            }
            return result;
        }
        
        public string GetRange(int start, int end, int[] nums){
            if(start==end) return nums[start].ToString();
            return nums[start].ToString() + "->" + nums[end].ToString();
        }
    }
    

Log in to reply
 

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