Scramble each digit of the int a and print out the biggest possible integer

后端 未结 4 490
孤独总比滥情好
孤独总比滥情好 2021-01-29 15:15

I’m stuck here. Do I just keep making new strings and turn them to int or us there a faster better way?

public void biggest(int a){
       int random;
       Stri         


        
4条回答
  •  感情败类
    2021-01-29 15:39

    Another option would be to count how many 0, 1, 2, ..., 9 values you have and then assemble them back together into a number knowing the digits will always be in descending order (9, 8, 7, ..., 0). The easy way to do this is with an array. Since this is a homework assignment the hard way (without using an array as per the requirement you added in a comment) is to use a variable counter per digit.

    public class so64125767 {
        public static int biggestBuckets(int a) {
            int[] buckets = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    
            while (a > 0) {
                buckets[a % 10]++;
                a /= 10;
            }
    
            int num = 0;
            for (int i = 9; i >= 0; i--) {
                for (int j = 0; j < buckets[i]; j++) {
                    num *= 10;
                    num += i;
                }
            }
    
            return num;
        }
    
        public static int biggestBucketsVar(int a) {
            int zero = 0;
            int one = 0;
            int two = 0;
            int three = 0;
            int four = 0;
            int five = 0;
            int six = 0;
            int seven = 0;
            int eight = 0;
            int nine = 0;
    
            while (a > 0) {
                switch (a % 10) {
                case 0:
                    zero++;
                    break;
                case 1:
                    one++;
                    break;
                case 2:
                    two++;
                    break;
                case 3:
                    three++;
                    break;
                case 4:
                    four++;
                    break;
                case 5:
                    five++;
                    break;
                case 6:
                    six++;
                    break;
                case 7:
                    seven++;
                    break;
                case 8:
                    eight++;
                    break;
                case 9:
                    nine++;
                    break;
                }
                a /= 10;
            }
    
            int num = 0;
            
            for (int j = 0; j < nine; j++) {
                num *= 10;
                num += 9;
            }
            
            for (int j = 0; j < eight; j++) {
                num *= 10;
                num += 8;
            }
    
            for (int j = 0; j < seven; j++) {
                num *= 10;
                num += 7;
            }
    
            for (int j = 0; j < six; j++) {
                num *= 10;
                num += 6;
            }
    
            for (int j = 0; j < five; j++) {
                num *= 10;
                num += 5;
            }
    
            for (int j = 0; j < four; j++) {
                num *= 10;
                num += 4;
            }
    
            for (int j = 0; j < three; j++) {
                num *= 10;
                num += 3;
            }
    
            for (int j = 0; j < two; j++) {
                num *= 10;
                num += 2;
            }
    
            for (int j = 0; j < one; j++) {
                num *= 10;
                num += 1;
            }
    
            for (int j = 0; j < zero; j++) {
                num *= 10;
                // num += 0;
            }
    
            return num;
        }
    
        public static void main(String[] args) {
            System.out.println(biggestBuckets(237428379));
            System.out.println(biggestBucketsVar(237428379));
            -- 987743322
        }
    }
    

    I'm also going to bet if you benchmark these results along with the other suggestions (using String or Collections) you'll find this method scales the best (imagine if you accepted numbers beyond the size of an int).

提交回复
热议问题