Sword Finger Offer Interview Question 49: Ugly Numbers

For this question, my idea at the beginning was to multiply each generated ugly number by 2, 3, and 5. Add to the total generated list in order, but there is a problem with this method, that is, each number is multiplied by 2. After, 3 and 5, there will be other ungenerated ugly numbers between the obtained numbers. Therefore, the key point of this question is to generate ugly numbers in order.

The answer is that in the generated array, the ugly numbers are from small to large. For the number to be multiplied by two, the number to be multiplied by three and the number to be multiplied by five, set three int quantities and record their positions in the array. . These three quantities are the first element of the array at the beginning, and each time you need to add another element to the array, multiply the corresponding numbers of the three quantities by 2, 3, and 5 respectively, and then the smaller ones are added. , And the corresponding amount is shifted to the right. In this way, the numbers in the array are arranged in ascending order without adjustment when they are added.

 

My error code time exceeded

class Solution {
    public int nthUglyNumber(int n) {
        if(n==1){
            return 1;
        }
        int count = 1;
        List<Integer> cur_u_nums = new LinkedList<Integer>();
        List<Integer> pre_u_nums = new LinkedList<Integer>();
        List<Integer> total_u_nums = new LinkedList<Integer>();

        Set<Integer> total_nums_set = new HashSet();
        Set<Integer> pre_total_nums_set = new HashSet();
        pre_u_nums.add(1);
        total_nums_set.add(1);
        total_u_nums.add(1);
        int t_length = 0;
        while(t_length <= n){
            
            for(Integer i : pre_u_nums){
                cur_u_nums.add(i*2);
                cur_u_nums.add(i*3);
                cur_u_nums.add(i*5);
            }

            int count_result = total_u_nums.size();
            int max_2 = 0;
            for(Integer i : cur_u_nums){
                int larger_mark = 0;
                for(int count_inner = 0;count_inner<count_result;count_inner++){
                    if(total_u_nums.get(count_inner)>i){
                        if(max_2==0){
                            t_length = count_inner;
                            max_2 = 1;
                        }
                        total_u_nums.add(count_inner,i);
                        count_result++;
                        larger_mark = 1;
                        break;
                    }else if(total_u_nums.get(count_inner)<i){
                        continue;
                    }else if(total_u_nums.get(count_inner)==i){
                        larger_mark = 1;
                        break;
                    }
                }
                if(larger_mark == 0){
                    count_result++;
                    total_u_nums.add(i);
                }
            }

            pre_u_nums = cur_u_nums;
            cur_u_nums = new LinkedList<Integer>();
            
        }
        
        
        return total_u_nums.get(n-1);

    }
}

Answer code (there is no direct judgment as to which index is increasing, it is smaller than the newly added element)

    public int nthUglyNumber(int n) {
        int index_2 = 0;
        int index_3 = 0;
        int index_5 = 0;
        int[] nums_u  = new int[n];
        int end = 0;
        nums_u[0] = 1;

        for(;end<n-1;){
            int min_num = nums_u[index_2]*2 > nums_u[index_3]*3? nums_u[index_3]*3:nums_u[index_2]*2;
            min_num = min_num > nums_u[index_5]*5 ? nums_u[index_5]*5 : min_num;

            nums_u[++end] = min_num;
            while(nums_u[index_2]*2 <= nums_u[end]){
                index_2++;
            }

            while(nums_u[index_3]*3 <= nums_u[end]){
                index_3++;
            }

            while(nums_u[index_5]*5 <= nums_u[end]){
                index_5++;
            }
        }

        return nums_u[n-1];
    }

 

Guess you like

Origin blog.csdn.net/qq_40473204/article/details/114632280