Neat sorting solution in Java

  • 1
    	public static boolean sortingSolution(int [] A) {
    	if (A == null || A.length == 0) return false;
    	int last = A[0];
    	for (int i = 1; i < A.length; i ++) {
    		if (A[i] == last) return true;
    		last = A[i];
    	return false;

    Just to highlight a habit. If checking two consecutive array elements causing out-of-bound exception bothers you, consider caching one element and starting the loop from the 1st element instead of the 0th.

Log in to reply

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