Java solution


  • 0
    M
    public static List<Integer> lexicalOrder(int n) {
            ArrayList<String> tmpList = new ArrayList();
            ArrayList<Integer> tmpList2 = new ArrayList();
            for (int c = 1; c <= n; c++)
                tmpList.add(c + "");
            String[] tmpArray = new String[tmpList.size()];
            tmpArray = tmpList.toArray(tmpArray);
    
            for (int i = 0; i < tmpArray.length - 1; i++) {
                for (int j = 0; j < tmpArray.length - 1; j++) {
    
                    if (tmpArray[j].compareTo(tmpArray[j + 1]) > 0) {
                        String temp = tmpArray[j];
                        tmpArray[j] = tmpArray[j + 1];
                        tmpArray[j + 1] = temp;
                    }
                }
            }
            for (int i = 0; i < tmpArray.length; i++)
                tmpList2.add(Integer.parseInt(tmpArray[i]));
            return tmpList2;
        }
    

Log in to reply
 

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