classSolution { public: vector<string> fizzBuzz(int n){ vector<string> answer; for (int i = 1; i <= n; i++) { string curr; if (i % 3 == 0) { curr += "Fizz"; } if (i % 5 == 0) { curr += "Buzz"; } if (curr.size() == 0) { curr += to_string(i); } answer.emplace_back(curr); } return answer; } };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
funcfizzBuzz(n int) (ans []string) { for i := 1; i <= n; i++ { sb := &strings.Builder{} if i%3 == 0 { sb.WriteString("Fizz") } if i%5 == 0 { sb.WriteString("Buzz") } if sb.Len() == 0 { sb.WriteString(strconv.Itoa(i)) } ans = append(ans, sb.String()) } return }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution: deffizzBuzz(self, n: int) -> List[str]: ans = [] for i inrange(1, n + 1): s = "" if i % 3 == 0: s += "Fizz" if i % 5 == 0: s += "Buzz" if s == "": s = str(i) ans.append(s) return ans