public class Solution {
    public int nextGreaterElement(int n) {
        int m = n, size = 0, peak = 1, list[] = new int[10];
        while (m != 0) {
            list[size++] = m % 10;
            m /= 10;
        }

        while (peak < size) {
            if (list[peak] < list[peak - 1]) {
                int cursor = 0, result = 0;
                while (list[cursor] <= list[peak]) cursor++;
                swap(list, cursor, peak);
                for (int i = size - 1; i >= peak; i--) result = result * 10 + list[i];
                for (int j = 0; j < peak; j++) result = result * 10 + list[j];
                if (result > n) return result;
                swap(list, cursor, peak);
            }
            peak++;
        }
        return -1;
    }

    private void swap(int[] list, int m, int n) {
        int tmp = list[m];
        list[m] = list[n];
        list[n] = tmp;
    }
}

@@ -415,32 +415,107 @@
     
-swap(list, cursor, peak)
+int tmp = list%5Bcursor%5D;%0A                list%5Bcursor%5D = list%5Bpeak%5D;%0A                list%5Bpeak%5D = tmp
 ;%0A  
@@ -698,276 +698,102 @@
     
-if (result %3E n) return result;%0A                swap(list, cursor, peak);%0A            %7D%0A            peak++;%0A        %7D%0A        return -1;%0A    %7D%0A%0A    private void swap(int%5B%5D list, int m, int n) %7B%0A        int tmp = list%5Bm%5D;%0A        list%5Bm%5D = list%5Bn%5D;%0A        list%5Bn%5D = tmp
+return result %3E n ? result : -1;%0A            %7D%0A            peak++;%0A        %7D%0A        return -1
 ;%0A  

optimize #556