C# Code Beats 98 Percents


  • 0
    R
    public IList<string> SummaryRanges(int[] nums)
    {
    	int start = 0, end = 0;
    	var result = new List<string>();
    	if (nums.Length == 0)
    		return result;
    	for (int i = 0; i < nums.Length - 1; i++)
    	{
    		if (nums[i] + 1 == nums[i + 1])
    		{ end++; continue; }
    		else
    		{
    			if (start == end)
    				result.Add(nums[start].ToString());
    			else
    				result.Add(nums[start] + "->" + nums[end]);
    			start = i + 1; end = i + 1;
    		}
    	}
            // handle the tail element
    	if (start == end)
    		result.Add(nums[start].ToString());
    	else
    		result.Add(nums[start] + "->" + nums[end]);
    	return result;
    }
    

  • 0

    nice solution, pretty straight forward. But, interesting part for me is that it "beats 98%", so I had to see what you did differently than mine because I only be 5%, also C#. My solution was basically the same as yours so I was pretty curious to find out what was going on. Just for kicks I pasted your code and submitted it and...... it beats only 10%!!! Anyhow, it's pretty clear that the time based results from OJ are not indicators of which solution is better than another. Just thought I'd pass that along to anyone out there that was wondering the same thing.


Log in to reply
 

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