Really Simple Easy to understand Java O(n) solution


#1
public class Solution {
        public void sortColors(ArrayList<Integer> a) {
            //nz and nt represents - nextZero and nextTwo index
            int nz=0;int nt=a.size()-1;
            for(int i=0;i<=nt;i++){
                if(a.get(i)==0){
                    //swapping i and nz
                    int temp=a.get(i); a.set(i,a.get(nz));  a.set(nz, temp);
                    nz++;
                }else if(a.get(i)==2){
                    //swapping i and nt
                    int temp=a.get(i);  a.set(i,a.get(nt));  a.set(nt, temp);
                    nt--;i--;
                }
            }
        }
    }