Java 4ms solution , Not using "%" operation


  • 72
    H
    public class Solution {
        public List<String> fizzBuzz(int n) {
            List<String> ret = new ArrayList<String>(n);
            for(int i=1,fizz=0,buzz=0;i<=n ;i++){
                fizz++;
                buzz++;
                if(fizz==3 && buzz==5){
                    ret.add("FizzBuzz");
                    fizz=0;
                    buzz=0;
                }else if(fizz==3){
                    ret.add("Fizz");
                    fizz=0;
                }else if(buzz==5){
                    ret.add("Buzz");
                    buzz=0;
                }else{
                    ret.add(String.valueOf(i));
                }
            } 
            return ret;
        }
    }
    

  • 0
    R

    Nice idea! perfect for follow up question.


  • 10
    R

    Nice extension to the problem. Here is my approach, basic idea is same as your's I think:

        public List<String> fizzBuzz(int n) {
            
            List<String> result = new ArrayList<>();
            
            if(n < 1) return result;
            
            for(int i = 1, fizz = 3, buzz = 5; i <= n; i++) {
            
                String addVal = null;
                
                if(i == fizz && i == buzz) {
                    addVal = "FizzBuzz"; 
                    fizz += 3;
                    buzz += 5;
                } else if(i == fizz) {
                    addVal = "Fizz";
                    fizz += 3;
                } else if(i == buzz) {
                    addVal ="Buzz";
                    buzz += 5;
                } else
                    addVal = String.valueOf(i);
                
                result.add(addVal); 
            }
            
            return result;
        }
    
    

  • 0
    K

    Nice solution!


  • 0

    Nice idea to the problem


  • 1

    Wow!
    But i wonder which is better between
    list.add( "" + i );
    and
    addStr = String.valueOf(i); list.add(addStr)


  • 0

    nice solution! good job!


  • 0
    K

    Another idea without "%".

    class Solution {
    public:
        vector<string> fizzBuzz(int n) {
            vector<string> res(n);
            if (n>=3) res[2]="Fizz";
            if (n>=5) res[4]="Buzz";
            for(int i=0; i<n; ++i) {
                if (i>=3&&res[i-3].find("Fizz")!=string::npos) res[i]="Fizz";
                if (i>=5&&res[i-5].find("Buzz")!=string::npos) res[i]+="Buzz";
                if (res[i].empty()) res[i]=to_string(i+1);
            }
            
            return res;
        }
    };
    

  • 0
    W

    @hand515
    Good idea to consider both readable and performance, it's better than '%' operation.


  • 0
    L

    the idea is very good.
    once i see this question, i think the solution should be % operation.

    after look you solution, i realized that my thinking not diffuse at all.


  • 1
    C

    像这样的解法和用%有什么区别吗,哪个的复杂度更低一点喃?
    小白,望不吝赐教


  • 0
    F

    Good idea, thanks


  • 0
    S

    Java 8 solution

     public List<String> fizzBuzz8(int n) {
                return IntStream.range(1, n + 1).mapToObj(i -> new StringBuilder(12).append(i % 3 == 0 ? "Fizz" : "").append(i % 5 == 0 ? "Buzz" : "").append(
                        (i % 3 != 0 && i % 5 != 0) ? i : "").toString()).collect(Collectors.toList());
            }
    

Log in to reply
 

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