fork download
  1. public class Solution {
  2. public int firstMissingPositive(int[] A) {
  3. int n = A.length;
  4. for (int i=0; i<n; i++) {
  5. while (A[i] != i+1 && A[i]-1 >= 0 && A[i]-1 < n && A[i] != A[A[i]-1]) {
  6. swap(A, i, A[i]-1);
  7. }
  8. }
  9.  
  10. for (int i=0; i<n; i++) {
  11. if (A[i] != i+1) return i+1;
  12. }
  13. return n + 1;
  14. }
  15.  
  16. void swap(int[] A, int i, int j) {
  17. int tmp = A[i];
  18. A[i] = A[j];
  19. A[j] = tmp;
  20. }
  21. }
  22.  
  23. // 3,4,-1,1
  24. // -1,4,3,1
  25. // -1,1,3,4
  26. //
  27.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:1: error: class Solution is public, should be declared in a file named Solution.java
public class Solution {
       ^
1 error
stdout
Standard output is empty