lullu_Go

contains the code of leetCode 179
 avatar
bruteCoder
java
2 years ago
1.1 kB
7
Indexable
class Solution {
    public String largestNumber(int[] nums) {
         int n = nums.length;

         

        for(int i = 0 ;i<n ;i++){
            for(int j = i+1;j<n;j++){

                if(nums[i] == 0) swap(nums,i,j);

                else{
                long com1 = comBine(nums[i],nums[j]);
                long com2 = comBine(nums[j],nums[i]);
               
               if(com2 > com1) swap(nums,i,j);
                }

            }
        }

        StringBuilder sb = new StringBuilder();
         boolean flag = false;
        for(int el : nums){
            if(el != 0) flag = true;
            sb.append(el);
        }

        if(!flag) return "0";

        return sb.toString();

    }

    public static void swap(int[] arr , int i , int j)
    {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    public static long comBine(int num1 , int num2){
        long digit = (int) (Math.log10(num2)+1);
        long offset = 1;
        while(digit-->0){
           offset *= 10;
        }
       
        
        long res = num1*offset + num2;

        return res;
    }
}
Editor is loading...
Leave a Comment