Usual Brute Force Approach in Java with O(mn) complexity


  • 0
    E
    class Solution {
        public ArrayList<Integer> selfDividingNumbers(int left, int right) {
            ArrayList<Integer>res=new ArrayList<Integer>();
            for(int i=left;i<=right;i++){
                int flag=0;
                int n=i;
                while(n!=0 && n%10!=0){
                    if(i%(n%10)!=0){
                        flag=1;
                        break;
                    }
                    n/=10;
                }
                if(n==0 && flag==0){
                    res.add(i);
                }
            }
            return res;
        }
    }
    

Log in to reply
 

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