A 2-Line C# code


  • 3
    L
    public string LargestNumber(int[] nums){
        Array.Sort(nums, (a, b) => (b + "" + a).CompareTo(a + "" + b));
        return nums[0] == 0 ? "0" : string.Join("", nums);
    }

  • 0

    Pretty cool, not much different from Python or Ruby. You don't need && nums[nums.Length - 1] == 0, though. How could that be false if nums[0] == 0 is true?


  • 0
    L

    hi Stefan, that's to handle case of {0, 0, 0, 0} so I think it needs it.
    You question is to ask what if nums is null? I tested the test cases here hasn't such case :D
    If there is, we can simply add one more condition "nums.Length > 0 && " and string.Join() will return "".


  • 0
    L

    I got you. We don't need it.


Log in to reply
 

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