This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func majorityElement(_ nums: [Int]) -> Int { | |
| if nums.count == 1 { | |
| return nums.first! | |
| } | |
| var valueOccurCount = [Int: Int]() | |
| for num in nums { | |
| if let value = valueOccurCount[num] { | |
| if value + 1 > (nums.count / 2) { | |
| return num |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func fizzBuzz(_ n: Int) -> [String] { | |
| var results = [String]() | |
| for index in 1 ... n { | |
| if n % 3 == 0 && n % 5 == 0 { | |
| results.append("FizzBuzz") | |
| }else if n % 3 == 0 { | |
| results.append("Fizz") | |
| }else if n % 5 == 0 { | |
| results.append("Buzz") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func isPowerOfTwo(_ n: Int) -> Bool { | |
| if n == 1 { return true } | |
| var sum = 1 | |
| while sum < n { | |
| sum *= 2 | |
| } | |
| return sum == n | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func missingNumber(_ nums: [Int]) -> Int { | |
| if nums.count == 1 { | |
| if nums.first == 1 { | |
| return 0 | |
| } | |
| return nums.first! + 1 | |
| } | |
| let sortedNums = nums.sorted() | |
| if sortedNums.min() != 0 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func isPalindrome(_ s: String) -> Bool { | |
| var letters = [String]() | |
| for char in s { | |
| guard let charAsciiCode = UnicodeScalar(String(char))?.value, | |
| charAsciiCode >= 97 && charAsciiCode <= 122 || charAsciiCode >= 65 && charAsciiCode <= 90 || charAsciiCode >= 48 && charAsciiCode <= 57 | |
| else { continue } | |
| letters.append(String(char)) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func climbStairs(_ n: Int) -> Int { | |
| var fibs = [0, 1, 1] | |
| for index in 1 ... n+1 { | |
| if index <= 2 { | |
| continue | |
| } | |
| fibs.append(fibs[index - 1] + fibs[index - 2]) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func climbStairs(_ n: Int) -> Int { | |
| if n == 0 { | |
| return 1 | |
| }else if n < 0 { | |
| return 0 | |
| } | |
| return climbStairs(n - 1) + climbStairs(n - 2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func climbStairs(_ n: Int) -> Int { | |
| if n == 0 { | |
| return 1 | |
| }else if n < 0 { | |
| return 0 | |
| } | |
| return climbStairs(n - 1) + climbStairs(n - 2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func climbStairs(_ n: Int) -> Int { | |
| var climbForOneStep = 0 | |
| var climbForTwoStep = 0 | |
| if n == 0 { | |
| return 1 | |
| } | |
| if n >= 1 { | |
| climbForOneStep = climbStairs(n - 1) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| func isIsomorphic(_ s: String, _ t: String) -> Bool { | |
| let sChars = Array(s) | |
| let tChars = Array(t) | |
| var mappingCharCodes = [Character: Character]() | |
| var backMappingCharCodes = [Character: Character]() | |
| if s.count == 0 { return true } |