Share my csharp solution 117ms


  • 1
    Y
    public class Solution{
        public string LargestNumber(int[] num)
        {
            //This is awkward ;( just for test case
            if (Array.TrueForAll(num, x => x == 0))
                return "0";
    
            Array.Sort(num,CompareNum);
            return String.Concat(num);
        }
    
        private static int CompareNum(int a, int b)
        {
            string sa = b.ToString();
            string sb = a.ToString();
            return (sa + sb).CompareTo(sb + sa);
        }
    

    }


  • 0
    S

    Cool!
    You could probably optimize it by checking num[0] == 0 after sorting, instead of using TrueForAll to check all elements for the "0" case.


Log in to reply
 

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