Skip to content

Commit bdc6cfc

Browse files
committed
Fizz buzz.
1 parent 6b44f9d commit bdc6cfc

File tree

4 files changed

+66
-7
lines changed

4 files changed

+66
-7
lines changed

Easy/409.Longest Palindrome.playground/Contents.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,18 @@ class Solution {
3333
}
3434
}
3535
var hasSingle = false
36-
map = map.filter { $1 > 1 }
3736
var count = 0
3837
for e in map {
39-
if e.value % 2 == 0 {
40-
count += e.value
41-
} else {
42-
if !hasSingle {
43-
hasSingle = true
38+
if e.value > 1 {
39+
if e.value % 2 == 0 {
4440
count += e.value
4541
} else {
46-
count += (e.value - 1)
42+
if !hasSingle {
43+
hasSingle = true
44+
count += e.value
45+
} else {
46+
count += (e.value - 1)
47+
}
4748
}
4849
}
4950
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
Given an integer n, return a string array answer **(1-indexed)** where:
3+
4+
- answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
5+
- answer[i] == "Fizz" if i is divisible by 3.
6+
- answer[i] == "Buzz" if i is divisible by 5.
7+
- answer[i] == i (as a string) if none of the above conditions are true.
8+
 
9+
10+
Example 1:
11+
Input: n = 3
12+
Output: ["1","2","Fizz"]
13+
14+
Example 2:
15+
Input: n = 5
16+
Output: ["1","2","Fizz","4","Buzz"]
17+
18+
Example 3:
19+
Input: n = 15
20+
Output: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
21+
 
22+
23+
Constraints:
24+
- 1 <= n <= 104
25+
*/
26+
class Solution {
27+
func fizzBuzz(_ n: Int) -> [String] {
28+
var resultArr = [String]()
29+
for i in 1...n {
30+
if i % 3 == 0 && i % 5 != 0 {
31+
resultArr.append("Fizz")
32+
} else if i % 3 != 0 && i % 5 == 0 {
33+
resultArr.append("Buzz")
34+
} else if i % 3 == 0 && i % 5 == 0 {
35+
resultArr.append("FizzBuzz")
36+
} else {
37+
resultArr.append("\(i)")
38+
}
39+
}
40+
return resultArr
41+
42+
}
43+
}
44+
45+
let s = Solution()
46+
let r = s.fizzBuzz(3)
47+
print(r)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/412.Fizz Buzz.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)