//bucket sort with constant space public int firstMissingPositive(int[] A) { // Start typing your Java solution below // DO NOT write main() function if(A == null || A.length == 0) return 1; int tmp = 0; for(int i = 0; i < A.length; i++) { if(A[i] > 0 && A[i] <= A.length) { if(A[i] != i + 1 && A[A[i] - 1] != A[i]) { //pay attention here tmp = A[A[i] - 1]; A[A[i] - 1] = A[i]; A[i] = tmp; i--;// pay attention here } } } for(int i = 0; i < A.length; i++) { if(A[i] != i + 1) return i + 1; } return A.length + 1; }
一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!
二、互相尊重,对自己的言论和行为负责。