Can someone help me to modify my code? Thx a lot.


  • 0

    Inspired by the solution 179 Largest Number, here comes my solution, but I got a TLE when n == 23489. Can someone tell me if this way of thinking may work? Thanks a lot.

    public List<Integer> lexicalOrder(int n) {
            List<Integer> ret = new ArrayList<Integer>();
            if(n < 1) return ret;
            
            Integer[] nums = new Integer[n];
            for(int i = 0; i < n; i ++){
                nums[i] = i + 1;
            }    
            
            Comparator<Integer> comp = new Comparator<Integer>(){
                @Override
                public int compare(Integer num1, Integer num2){
                    String str1 = String.valueOf(num1);
                    String str2 = String.valueOf(num2);
                    return str1.compareTo(str2);
                }
            };
            Arrays.sort(nums, comp);
            for(Integer num : nums){
                ret.add(num);
            }
            return ret;
        }
    

  • 0

    @传说选手 The time complexity here is O(nlogn), while the optimal way should be O(n), so you get TLE here.


Log in to reply
 

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