My simple C# solution based on SortedSet


  • 1
    S
    public class Solution {
        public string LargestNumber(int[] nums) 
        {
            var ss = new SortedSet<Tuple<string,int>>(Comparer<Tuple<string,int>>.Create((a,b) => string.Compare(a.Item1+b.Item1,b.Item1+a.Item1)==0?a.Item2-b.Item2:string.Compare(a.Item1+b.Item1,b.Item1+a.Item1)));
            var count = 0;
            
            for(int i =0; i<nums.Count(); i++)
            {
                ss.Add(new Tuple<string,int>(Convert.ToString(nums[i]),count++));
            }
            
            var sb = new StringBuilder();
            while(ss.Count()>0)
            {
                sb.Append(ss.Max.Item1);
                ss.Remove(ss.Max);
            }
            var result = sb.ToString();
            if(result[0] == '0')
                return "0";
            return result;
        }
    }
    
    

Log in to reply
 

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