Java Why is it time limit exceeded for my answer?


  • -1
    Y

    '''
    public class Solution {
    public List<Integer> lexicalOrder(int n) {
    TreeSet<String> ts = new TreeSet<String>();
    for (int i = 1; i <= n; i++) {
    ts.add(String.valueOf(i));
    }
    List<Integer> al = new ArrayList<Integer>();
    for (String str : ts) {
    al.add(Integer.valueOf(str));

    }
    return al;
    }
    

    }
    I am not sure about the time it takes. Anyone helps to explain? Thank you.


  • 0
    U

    Seems your algorithm will take O(n log n) for Time. Try to solve this in O(n). TreeSet sort algorithm takes


Log in to reply
 

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