Skip to content

Instantly share code, notes, and snippets.

@NigelJu
NigelJu / LeetCode169. Majority Element ( swift ).swift
Created June 28, 2018 12:04
LeetCode169. Majority Element ( swift ).swift
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
@NigelJu
NigelJu / LeetCode 412_Fizz Buzz.swift
Created June 26, 2018 14:34
LeetCode 412_Fizz Buzz.swift
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")
@NigelJu
NigelJu / LeetCode231_Power of Two.swift
Created June 25, 2018 12:47
LeetCode231_Power of Two.swift
class Solution {
func isPowerOfTwo(_ n: Int) -> Bool {
if n == 1 { return true }
var sum = 1
while sum < n {
sum *= 2
}
return sum == n
}
@NigelJu
NigelJu / LeetCode268_Missing Number Base.swift
Created June 25, 2018 12:15
LeetCode268_Missing Number Base.swift
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 {
@NigelJu
NigelJu / LeetCode 125_Valid Palindrome Base.swift
Created June 23, 2018 17:24
LeetCode 125_Valid Palindrome Base.swift
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))
}
@NigelJu
NigelJu / LeetCode70_Climbing Stairs Time UsingFibArr.swift
Created June 23, 2018 05:18
LeetCode70_Climbing Stairs Time UsingFibArr.swift
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])
}
@NigelJu
NigelJu / LeetCode70_Climbing Stairs Time Limit Exceeded2.swift
Created June 23, 2018 03:01
LeetCode70_Climbing Stairs Time Limit Exceeded2.swift
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)
@NigelJu
NigelJu / LeetCode70_Climbing Stairs Time Limit Exceeded2.swift
Created June 23, 2018 02:55
LeetCode70_Climbing Stairs Time Limit Exceeded2.swift
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)
@NigelJu
NigelJu / LeetCode70_Climbing StairsTime Limit Exceeded.swift
Last active June 23, 2018 04:34
LeetCode70_Climbing Stairs Time Limit Exceeded.swift
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)
}
@NigelJu
NigelJu / LeetCode205_Isomorphic Strings.swift
Created June 22, 2018 15:57
LeetCode205_Isomorphic Strings.swift
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 }