Java Easy o(sqrt(n)) solution


#1
public class Solution {
    public ArrayList<Integer> allFactors(int n) {
        ArrayList<Integer> res= new ArrayList<>();
        if(n==0){
            return new ArrayList<>(Arrays.asList(0));
        }
        int cur=0;
        for(int i=1;i*i<=n ;i++) {
            if(n%i == 0) {
                res.add(cur,i);
                if(i*i != n){
                    res.add(cur+1,n/i);
                }
                cur++;
            }
        }
        return res;
    }
}