Easy Java Solution only one function


  • 0
    public class Solution {
        public List<List<Integer>> getFactors(int n) {
          List<List<Integer>>  ret = new ArrayList<List<Integer>>();
          for(int i=2;i<=(int)Math.sqrt(n);i++)
              {
                  if(n%i!=0) continue;
                  List<List<Integer>> tails=getFactors(n/i);
                  for(List<Integer> tail:tails)
                   {
                        if(tail.get(0)<i) continue;
                        List<Integer> arr = new ArrayList<Integer>();
                        arr.add(i);
                        arr.addAll(tail);
                        ret.add(arr);
                    }
                    List<Integer> arr = new ArrayList<Integer>();
                    arr.add(i); arr.add(n/i);
                    ret.add(arr);
              }
          return ret;
        }
    }
    

Log in to reply
 

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