Solution without needing to check for 15


  • 0
    G
        public List<String> fizzBuzz(int n) {
            List<String> list = new ArrayList<String>();
            list.add("1");
            
            for(int i=2;i<=n;i++)
            {
                String str="";
                if(i%3==0)
                    str+="Fizz";
                if(i%5==0)
                    str+="Buzz";
                if(str=="")
                    str+=i;
                    
                System.out.println("String is : "+str);
                list.add(str);
                }
            return list;
        }
        
        }
    

    In all the solutions I saw, everyone seemed to be explicitly checking for 15. When you have checked for 3, you just need to check for 5. Because BUZZ will be appended as a part of the code without an explicit check.


Log in to reply
 

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