Java solution by extending number


  • 0
    L
    public String largestNumber(int[] nums) {
    	int len = nums.length;
    	char[][] chars= new char[len][10];
    	int k=0;
    	for(int i=0;i<len; ++i){
    		k=0;
    		for(int j=0; j<10; ++j){
    			String s = String.valueOf(nums[i]);
    			if(j<s.length()){
    				chars[i][j] = s.charAt(j);
    			}else{
    				chars[i][j] = s.charAt(k);
    				++k;
    				if(k==s.length()){
    					k=0;
    				}
    			}
    		}
    	}
    	long[] numsL = new long[len];
    	Map<Long,String> mp = new HashMap<Long,String>();
    	for(int i=0;i<len; ++i){
    		numsL[i] = Long.valueOf(String.valueOf(chars[i]));
    		if(mp.get(numsL[i])==null){
    			mp.put(numsL[i], String.valueOf(nums[i]));
    		}else{
    			mp.put(numsL[i], mp.get(numsL[i])+nums[i]);
    		}
    	}
    	Arrays.sort(numsL);
    	StringBuilder sb = new StringBuilder();
    	for(int j=len-1; j>=0; --j){
    		if(j+1<len && numsL[j]==numsL[j+1]){
    			continue;
    		}
    		sb.append(mp.get(numsL[j]));
    	}
    	String res = sb.toString();
    	int i=0;
    	for(;i<res.length()&&res.charAt(i)=='0';++i){
    	}
    	if(i==res.length()){
    	    return "0";
    	}else{
    	    return res.substring(i,res.length());
    	}
    }

Log in to reply
 

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