A slow but Entirely Different Approach


  • 0
    S

    Consider the below logic in the picture.
    ![0_1500955895377_LargestNumber.png](Uploading 100%)

    import java.math.*;
    public class Solution {
        public String largestNumber(int[] nums) {
            ArrayList<Integer> a = new ArrayList<Integer>();
            for (int i = 0 ; i < nums.length; i++)
            {
                a.add(nums[i]);
            }
            return largestNumber(a);
        }
        public String largestNumber(ArrayList<Integer> a)
    	{
    		int digits_in_max = Collections.max(a).toString().length();
    		ArrayList<shammy> sha = new ArrayList<shammy>();
    		for (int i = 0 ; i < a.size(); i++)
    		{
    			sha.add(new shammy(a.get(i), digits_in_max));
    		}
    		Collections.sort(sha, (p1, p2) -> (int)(p2.modifiedValue-p1.modifiedValue));
    		StringBuilder sb = new StringBuilder();
    		for (int i = 0; i < sha.size(); i++)
    		{
    			sb.append(new StringBuilder(Long.toString(sha.get(i).originalValue)));
    		}
            BigInteger bi = new BigInteger(sb.toString());
    		return bi.toString();
    	}
    }
    class shammy
    {
    	int originalValue;
    	long modifiedValue;
    	public shammy(int originalValue, int digits_in_max)
    	{
    		this.originalValue = originalValue;
    		String s = Integer.toString(originalValue);
    		StringBuilder sb = new StringBuilder(s);
    		StringBuilder ans = new StringBuilder();
    		while(ans.length() <= digits_in_max + 1)
    		{
    			ans.append(sb);
    		}
    		s = ans.toString().substring(0, digits_in_max + 1);
    		modifiedValue = Long.parseLong(s);
    	}
    	public String toString()
    	{
    		return "[" + modifiedValue + ", " + originalValue + "]";
    	}
    }
    

Log in to reply
 

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