Skip to content

Commit

Permalink
added #412
Browse files Browse the repository at this point in the history
  • Loading branch information
davidseek committed May 7, 2020
1 parent f633164 commit 69f8fa7
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Runtime: 0 ms, faster than 100.00% of Swift online submissions for Valid Parenth
* [#98 Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/)
* [#200 Number of Islands](https://leetcode.com/problems/number-of-islands/)
* [#347 Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/)
* [#412 Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)
* [#844 BAckspace String Compare](https://leetcode.com/problems/backspace-string-compare/)
* [#937 Reorder Data in Log Files](https://leetcode.com/problems/reorder-data-in-log-files/)
* [#973 K Closest Points to Origin](https://leetcode.com/problems/k-closest-points-to-origin/)
Expand Down
100 changes: 100 additions & 0 deletions leetcode-412-fizz-buzz.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**

Copyright (c) 2020 David Seek, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.





https://leetcode.com/problems/fizz-buzz/


Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:

n = 15,

Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]

*/

/**
Big O Annotation
Time complexity O(n) where n is 1...n.
Space complexity O(n) where n is 1...n.
*/
func fizzBuzz(_ n: Int) -> [String] {

var result: [String] = []

// Iterate from 0 to n
for number in 1...n {

// Check if element is a multiplication of 3
let multiplicationOf3 = (number % 3) == 0

// Check if element is a multiplication of 5
let multiplicationOf5 = (number % 5) == 0

// If it's both, then add "FizzBuzz"
if multiplicationOf3 && multiplicationOf5 {

result.append("FizzBuzz")

// Add "Fizz" for 3
} else if multiplicationOf3 {

result.append("Fizz")

// Add "Fizz" for 5
} else if multiplicationOf5 {

result.append("Buzz")

// Else add just the number
} else {

result.append("\(number)")
}
}

return result
}

0 comments on commit 69f8fa7

Please sign in to comment.