This file contains 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
// edge -> not int, string, nil | |
// 1. get integer | |
// 2. if num is 0, return 0 | |
// 3. check negative integer or not -> if negative, save -1 | |
// 4. convert to string | |
// 5. create new num | |
// 6. multiply -1 if num is negative | |
func reverseInt(number: Int) -> Int? { | |
if (number == 0) { |
This file contains 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
// edge -> including nil, string, should be more than 2 for array size | |
// 1. get array and int | |
// 2. check edge case | |
// 3. prepare start and end index for 0 and 1 | |
// 4. create while statement start <= end, end < n | |
// 5. check diff with array(start) and array(end) | |
// 6. if diff <= int, increase index of end | |
// 7. if diff >= int, increase index of start | |
func checkDiff(A: [Int], k: Int) -> Int { |
This file contains 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
// edge -> nil, string, max or min array size | |
// 1. get array and value | |
// 2. check edge case | |
// 3. check array one by one, remove element which same as value | |
// 4. return array.count | |
func getRemovedValueArray(array: [Int], value: Int) -> Int { | |
var modifyArr = array | |
for a in (0..<array.count).reversed() { | |
if(array[a] == value) { |
This file contains 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
// edge -> nil, string, max, min | |
// 1. create array and init as [1] | |
// 2. create smallArray with adding array[n] and array[n+1] | |
// 2. add smallArray into bigArray | |
// 3. add 1 in the last | |
func pascal(num: Int) -> [[Int]] { | |
var sumArray = [[Int]]() | |
var pascalArray = [Int]() | |
sumArray.append([1]) |
This file contains 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
// edge -> not int, nil, over max, min length | |
// 1. get and sort array to ascending | |
// 2. check diff with numbers in for statement ony by one | |
func diffK(array: [Int], k: Int) -> Int { | |
var arr = array.sorted{ $0 < $1 } | |
for a in 0..<arr.count { | |
for b in a..<arr.count { | |
if arr[b] - arr[a] == k { |
This file contains 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
// edge -> including string, inclu nil | |
// max and min length of array -> return value if including edge case | |
// 1. get array | |
// 2. check array one by one with for statement | |
// 3. if value is same as next value, delete next value | |
// 4. not same, add new array | |
// 5. return number of array and new array | |
func getNonDupArray(array: [Int]) -> [Int]? { |
This file contains 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
func getWrongNumber(num: Int, correct: Int) -> Int? { | |
if (num == 0) { | |
return nil | |
} | |
var array = [Int]() | |
for i in 1...num { | |
array.append(i) | |
} | |
array.remove(at: correct-1) | |
return array.first |
This file contains 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
func findMajority(arr: [Int]) -> Int? { | |
let sortedArray = Array(arr.sorted{ $1 < $0 }) | |
var maxNum = 0 | |
var numOfTimes = 0 | |
for a in sortedArray { | |
if a == maxNum { | |
numOfTimes += 1 | |
} else { | |
maxNum = a | |
numOfTimes = 1 |
This file contains 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
func getPrice(str: String) -> Int? { | |
let arr = str.components(separatedBy:"\n") | |
guard let num = Int(arr[0]) else { | |
return nil | |
} | |
if (num < 1 || num > 10) { | |
return nil | |
} | |
print(arr) | |
var priceArr = [Int]() |
This file contains 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
import Foundation | |
func getNumber(string: String?) -> Int? { | |
// B -> 26*0 + ("B" - "A") | |
// AB -> 26*1 + ("B" - "A") | |
var resultNum = 0 | |
// unwrap optional | |
guard let string = string else { | |
return nil | |
} |
NewerOlder