๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿž Daily Loaf/์•Œ๊ณ ๋ฆฌ์ฆ˜

[iOS] ๋ฐฑ์ค€ ํ’€๊ธฐ์ „ TIP ์•Œ์•„๋‘๊ธฐ

by BreadDev 2024. 10. 2.
728x90
ungchun๋‹˜์˜ ๋ฒจ๋กœ๊ทธ๋ฅผ ์ฐธ์กฐํ•˜๋ฉด์„œ ์ € ๋˜ํ•œ ์ฝ”ํ…Œ์ „ ์Šค์œ„ํ”„ํŠธ๋ฅผ ์ค€๋น„ํ•˜๋ฉด์„œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋“ค์„ ์ •๋ฆฌํ•ด๋ณด๋ คํ•ฉ๋‹ˆ๋‹ค.

 

https://velog.io/@leedool3003/iOS-swift-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-%EC%BD%94%EB%94%A9%ED%85%8C%EC%8A%A4%ED%8A%B8%EC%97%90-%ED%95%84%EC%9A%94%ED%95%9C-Tip-%EC%A0%95%EB%A6%AC

 

[iOS] swift ์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์— ํ•„์š”ํ•œ tip ์ •๋ฆฌ

swift ์•Œ๊ณ ๋ฆฌ์ฆ˜, ์ฝ”๋”ฉํ…Œ์ŠคํŠธ tip ์ •๋ฆฌ

velog.io


๊ธฐ๋ณธ ์ž…๋ ฅ ๋ฐ›๊ธฐ

// return String
var input = readLine()!

// return Int
var input = Int(readLine()!)!

"1 2 3 4 5"์™€ ๊ฐ™์€ ๊ณต๋ฐฑ ์žˆ๋Š” ์ˆซ์ž ์ž…๋ ฅ ๋ฐ›๊ธฐ

// split - return: [SubString]
var nums = readLine()!.split(separator: " ").map {Int($0)!}

// components - return: [String] -> import Foundation ํ•„์ˆ˜
var nums = readLine()!.components(separatedBy: " ").map {Int($0)!}

// FileIO, ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ ๋ฆฌ์ŠคํŠธ์— ๋ฐ”๋กœ ์ถ”๊ฐ€ํ•˜๊ธฐ
array.append((file.readInt(), file.readInt()))

"12345"์™€ ๊ฐ™์€ ๊ณต๋ฐฑ ์—†๋Š” ์ˆซ์ž๋ฅผ ๋ฐฐ์—ด๋กœ ์ž…๋ ฅ ๋ฐ›๊ธฐ

// Int
var nums = Array(readLine()!).map {Int(String($0))!}

// String
var nums = Array(readLine()!).map {String($0)}

// FileIO
let num = [file.readInt(), file.readInt()]

์ž…๋ ฅ๊ฐ’ ์‹œ๊ฐ„์ดˆ๊ณผ -> fread์˜ swift ๋ฒ„์ „(๋ผ์ด๋…ธ๋‹˜ FilelO)

https://gist.github.com/JCSooHwanCho/30be4b669321e7a135b84a1e9b075f88

 

psํ•  ๋•Œ ์ž…๋ ฅ์„ ํ•œ๊บผ๋ฒˆ์— ๋ฐ›๊ธฐ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค. fread์˜ swift ๋ฒ„์ „.

psํ•  ๋•Œ ์ž…๋ ฅ์„ ํ•œ๊บผ๋ฒˆ์— ๋ฐ›๊ธฐ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํด๋ž˜์Šค. fread์˜ swift ๋ฒ„์ „. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

์ž…์ถœ๋ ฅ์ด 10~20๋งŒ์ค„์ด ๋„˜์–ด๊ฐ€๋ฉด ์“ฐ๋ผ๋Š”๋ฐ ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‚˜๋ด์—ฌ ใ„ทใ„ท ๋‚˜์ค‘์— ์ €๋„ ๊ฒฝํ—˜ํ•˜๋ฉด ์จ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
xcode ํ„ฐ๋ฏธ๋„์—์„œ enter๊ฐ€ ์•„๋‹Œ eof๋ฅผ ์ž…๋ ฅํ•ด์•ผ ์ž…๋ ฅ์™„๋ฃŒ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
ํ„ฐ๋ฏธ๋„์—์„œ ๊ฐ’์„ ์ž…๋ ฅํ•œ ๋’ค ์ œ์ผ ๋งˆ์ง€๋ง‰์— ctrl+d๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ถœ๋ ฅ๊ฐ’์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.

prefix / suffix

let strData = "hello์ œ๋กœ!!"

// [prefix: ์‹œ์ž‘ ๊ธฐ์ค€์œผ๋กœ ์ง€์ •๋œ ๋ฌธ์ž ๊ฐœ์ˆ˜ ์ถœ๋ ฅ]
let startData = strData.prefix(5)
print(startData)	// hello

// [suffix: ์ข…๋ฃŒ ๊ธฐ์ค€์œผ๋กœ ์ง€์ •ํ•œ ๋ฌธ์ž ๊ฐœ์ˆ˜ ์ถœ๋ ฅ]
let endData = strData.suffix(4)
print(endData)		// ์ œ๋กœ!!

let numbers = [1, 2, 3, 4, 5]
print(numbers.prefix(2))
// Prints "[1, 2]"
print(numbers.prefix(10))
// Prints "[1, 2, 3, 4, 5]"

let numbers = [1, 2, 3, 4, 5]
print(numbers.suffix(2))
// Print "[4, 5]"
print(numbers.suffix(10))
// Prints "[1, 2, 3, 4, 5]"

split(whereSeparator: {})

// ๊ธฐ์ค€์ด ๋˜๋Š” ๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด ๋ฆฌํ„ด
let dartResult = "1S2D*3T"
var scores = dartResult.split(whereSeparator: {!$0.isNumber}).map {Int($0)}
print(scores)
// Prints "[1, 2, 3]"
let letters = dartResult.split(weherSeparator: {$0.isNumber})
print(letters)
// Prints "["S", "D*", "T"]"

subString

// 3๋ฒˆ์งธ๋ถ€ํ„ฐ ๋๊นŒ์ง€
let startIdx: String.Index = str.index(str.startIndex, offsetBy: 3)
var result = String(str[startIdx...])

// ์ฒ˜์Œ๋ถ€ํ„ฐ 3๋ฒˆ์งธ๊นŒ์ง€
let endIdx: String.Index = str.index(str.startIndex, offsetBy: 3)
var result = String(str[...endIdx])

๋ฌธ์ž์—ด ๋ฐ˜๋ณต

let str = String(repeating: "ใ…‹", count: 10)
print(str)	// "ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹"

๋ฌธ์ž์—ด์— ํŠน์ • ๋ฌธ์ž ๋ฐ”๊พธ๊ธฐ

// import Foundation ํ•„์ˆ˜
let str = "ze!ro!"
let str2 = str.replacingOccurrences(of: "!", with: "?")
print(str2) // "ze?ro?"

๊ฐœํ–‰์—†๋Š” print

print("Hello", terminator: "")

forEach

let nums: [Int] = [1, 2, 3, 4]

nums.forEach {
   print($0)	// 1 2 3 4
}

stride

// 3 6 9
for i in stride(from: 3, to: 12, by: 3) {
   print(i)
}

// 3 6 9 12
for i in stride(from 3, through 12, by: 3) {
   print(i)
}

์ ˆ๋Œ€๊ฐ’

// 11
abs(-11)

์ œ๊ณฑ

let value = 3.0

// 9.0
pow(value, 2)

์ œ๊ณฑ๊ทผ ๊ตฌํ•˜๊ธฐ

let value = 9.0

// 3.0
sqrt(value)

์ž๋ฆฟ์ˆ˜ ๋”ํ•˜๊ธฐ

// n = 123, return = 6
String(n).map{String($0)}.reduce(0){$0 + Int($1)!}

๋ฐฐ์—ด ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ธฐ

var array = [1, 2, 3, 4, 5]

// [2, 3, 4]
array[1...3]

๋ฐฐ์—ด์— ์š”์†Œ ์ถ”๊ฐ€ํ•˜๊ธฐ

var array = [1, 2, 3]

array.append(4)				// [1, 2, 3, 4]
array.append(contentsOf: [5, 6, 7]	// [1, 2, 3, 4, 5, 6, 7]

๋ฐฐ์—ด์— ์›ํ•˜๋Š” ๊ฐ’ index ์ฐพ์•„์„œ ์‚ญ์ œํ•˜๊ธฐ

if let index = array.firstIndex(where: {$0 == value}) {
   array.remove(at: index)
}

1์ฐจ์› ๋ฐฐ์—ด ์„ ์–ธ

let arr = [Int]()

let arr = Array(repeating: 0, count: 5)
let arr = [Int](repeating: 0, count: 5)

2์ฐจ์› ๋ฐฐ์—ด ์„ ์–ธ

let arr = [[Int]]()

let arr = Array(repeating: Array(repeating:0, count: 3), count: 5)
let arr = [[Int]](repeating: [Int](repeating: 0, count: 3), count: 5)

map

var string = ["1", "2", "3", "4", "5"]

// [1, 2, 3, 4] ๊ฐ ์›์†Œ๋ฅผ ์ „๋ถ€ Int๋กœ ๋งตํ•‘
string.map { Int($0)! }

filter

var array = [1, 2, 3, 4]

// [2, 4] ์กฐ๊ฑด์— ๋งž๋Š” ์ˆ˜๋งŒ ๋ฝ‘์•„๋ƒ„
array.filter { $0 % 2 == 0 }

reduce

// ์ดˆ๊ธฐ๊ฐ’์ด 3์ด๊ณ  ์ •์ˆ˜ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ๊ฐ’์„ ๋”ํ•˜๋Š” ์ฝ”๋“œ
let numbers: [Int] = [1, 2, 3]
var sumFromThree: Int = numbers.reduce(3) {
   print("\($0) + \($1)")
   // 3 + 1
   // 4 + 2
   // 6 + 3
   return $0 + $1
}
print(sumFromThree)	// 9

// ๊ฐ„๋‹จํ•˜๊ฒŒ ์ด๋ ‡๊ฒŒ๋„ ๊ฐ€๋Šฅํ•จ
var array = [1, 2, 3, 4]
array.reduce(0, +)
// ๋ฌธ์ž์—ด ํ•ฉ์น˜๊ธฐ๋Š” joined()๋ฅผ ์‚ฌ์šฉ. reduce๊ฐ€ ํ›จ์”ฌ ๋А๋ฆผ

joined()

joined๋Š” ๋ฐฐ์—ด์˜ ๋ฌธ์ž์—ด๋“ค์„ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์ด ๋ฌธ์ž์—ด์ด์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค
var brands = ["Dell", "HP", "Apple"]

// DellHPApple
var result1 = brands.joined()

// Dell HP Apple
var result2 = brands.joined(separator: " ")

// Dell, HP, Apple
var result3 = brands.joined(separator: ", ")

// ๋งŒ์•ฝ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ผ๋ฉด ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ๊ณ  ์ง„ํ–‰ํ•ด์•ผํ•จ
var result: [Int] = [1, 2, 3, 4]
result.map{String($0)}.joined()	// [1, 2, 3, 4] -> 1234
result.map{String($0)).joined(separator: ", ")	// [1, 2, 3, 4] -> 1, 2, 3, 4

flatMap(), compactMap()

let array1 = [1, nil, 3, nil, 5, 6, 7]
let flatMapTest1 = array1.flatMap { $0 }
let compactMapTest1 = array1.compactMap { $0 }

// flatMapTest1 : [1, 3, 5, 6, 7]
// compactMapTest1 : [1, 3, 5, 6, 7]
  • 1์ฐจ์› ๋ฐฐ์—ด์—์„œ๋Š” ๋‘˜ ๋‹ค ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ด์ง€๋งŒ
  • Swift 4.1 ๋ถ€ํ„ฐ๋Š” 1์ฐจ์› ๋ฐฐ์—ด์—์„œ nil์„ ์ œ๊ฑฐํ•˜๊ณ  ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์„ ํ•˜๊ณ ์‹ถ์„๋•Œ๋Š” flatMap๋ง๊ณ  compactMap์„ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ํ•œ๋‹ค.
let array2: [[Int?]] = [[1, 2, 3], [nil, 5], [6, nil], [nil, nil]]
let flatMapTest2 = array2.flatMap { $0 }
let compactMapTest2 = array2.compactMap { $0 }

// flatMapTest2 : [Optional(1), Optional(2), Optional(3), nil, Optional(5), Optional(6), nil, nil, nil]
// compactMapTest2 : [[Optional(1), Optional(2), Optional(3)], [nil, Optional(5)], [Optional(6), nil], [nil, nil]]
  • flatMap๊ณผ compactMap์€ nil์„ ์ œ๊ฑฐํ•˜์ง€ ์•Š๊ณ  1์ฐจ์› ๋ฐฐ์—ด์ผ๋•Œ๋งŒ nil์ œ๊ฑฐ
  • flatMap์€ 2์ฐจ์› ๋ฐฐ์—ด์„ 1์ฐจ์› ๋ฐฐ์—ด๋กœ flattern ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋ฐ˜๋ฉด,
  • compactMap์€ 1์ฐจ์› ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • 2์ฐจ์› ๋ฐฐ์—ด์„ 1์ฐจ์›์œผ๋กœ ๋งŒ๋“ค๋•Œ๋Š” flatMap ์‚ฌ์šฉ
let array2: [[Int?]] = [[1, 2, 3], [nil, 5], [6, nil], [nil, nil]]
let flatMapTest2 = array2.flatMap { $0}.compactMap { $0 }

// flatMapTest2 : [1, 2, 3, 5, 6]
  • flatMap์œผ๋กœ flatternํ•˜๊ฒŒ ๋งŒ๋“ค๊ณ  compactMap์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ํŽธํ•˜๊ฒŒ 2์ฐจ์› ๋ฐฐ์—ด์„ ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉํ•œ 1์ฐจ์› ๋ฐฐ์—ด๋กœ ์ถœ๋ ฅ๊ฐ€๋Šฅ
let array3 = [[[1], [2, 3], [4, 5, 6], [7, 8, 9, 10]]]

let flatMapTest3 = array3.flatMap { $0 }
// [[1], [2, 3], [4, 5 ,6], [7, 8, 9, 10]]

let doubleflatMapTest3 = flatMapTest3.flatMap { $0 }
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  • 3์ฐจ์› ๋ฐฐ์—ด๋„ flatMap 2๋ฒˆ ์‚ฌ์šฉํ•˜๋ฉด flattern ํ•˜๊ฒŒ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅ

Set

  • ์ •๋ ฌ๋˜์ง€ ์•Š์€ ์ปฌ๋ ‰์…˜
  • ์ค‘๋ณต ํ—ˆ์šฉ X
  • ํ•ด์‹œ๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ์ €์žฅํ•ด์„œ ๋ฐฐ์—ด์— ๋น„ํ•ด ๊ฒ€์ƒ‰ ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค.
    • ์ €์žฅ๋˜๋Š” ์ž๋ฃŒํ˜•์€ Hashable ํ”„๋กœํ† ์ฝœ์„ ์ค€์ˆ˜ํ•ด์•ผํ•จ
// ์ƒ์„ฑ
var tempSet: Set<Int> = []
var tempSet: Set<Int>()

var tempSet: Set<Int> = [1, 2, 5, 0]

let count: Int = tempSet.count		// Set ๊ฐœ์ˆ˜ ํ™•์ธ: 4
let isEmpty: Bool = tempSet.isEmpty	// Set ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ: false

tempSet.contains(1)		// true
tempSet.contains(10)		// true

// insert: ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ณ , ์ถ”๊ฐ€๋œ ๊ฒฐ๊ณผ๋ฅผ ํŠœํ”Œ๋กœ ๋ฆฌํ„ด (์ค‘๋ณต์ด๋ฉด false, ์ถ”๊ฐ€๋œ ๊ฐ’)
tempSet.insert(1)		// (false, 1)
tempSet.insert(10)		// (true, 10)

// update: ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ถ”๊ฐ€ ํ›„ nil ๋ฆฌํ„ด, ์กด์žฌํ•  ๊ฒฝ์šฐ ๋ฎ์–ด์“ฐ๊ธฐ ํ›„ ๋ฎ์–ด์“ฐ๊ธฐ ์ „ ๊ฐ’ ๋ฆฌํ„ด
tempSet.update(with: 1)		// Optional(1)
tempSet.update(with: 120)	// nil

// remove: ํ•œ๊ฐ€์ง€ ์š”์†Œ ์‚ญ์ œํ•  ๋•Œ ์‚ฌ์šฉ, ์‚ญ์ œ ํ›„ ์‚ญ์ œํ•œ ๊ฐ’ return (์—†๋Š” ์š”์†Œ ์‚ญ์ œ ์‹œ nil ๋ฆฌํ„ด)
tempSet.remove(1)		// Optional(1)
tempSet.remove(10)		// nil

// removeAll(): ์ „์ฒด ์š”์†Œ ์‚ญ์ œ
tempSet.removeAll()

dictionary

// ์ƒ์„ฑ
var dict: [String: Int] = [:]
var dict = [String: Int]()

// ๊ฐœ์ˆ˜ ํ™•์ธ
let count: Int = dict.count
let isEmpty: Bool = dict.isEmpty

var dict = ["height": 165, "age": 100]

let height = dict["height"]		// Optional(165)
let weight = dict["weight"]		// nil

dict["weight"] = 100			// ํ•ด๋‹น Key๊ฐ€ ์—†๋‹ค๋ฉด, ์ถ”๊ฐ€ (insert)
dict["weight"] = 200			// ํ•ด๋‹น Key๊ฐ€ ์žˆ๋‹ค๋ฉด, Value ๋ฎ์–ด์“ฐ๊ธฐ (update)

dict["weight"] = nil			// ํ•ด๋‹น Key๊ฐ€ ์—†์–ด๋„ ์—๋Ÿฌ ์•ˆ๋‚จ
dict["weight"] = nil			// ํ•ด๋‹น Key๊ฐ€ ์žˆ๋‹ค๋ฉด, ํ•ด๋‹น Key-Value ์‚ญ์ œ
dict.removeAll()			// ์ „์ฒด ์‚ญ์ œ

dict.keys				// "height", "age"
dict.keys.sorted()			// "age", "height"

dict.values		// 165, 100
dict.values.sorted()	// 100, 165

dict.sorted(by: {$0.value > $1.value})

dict.filter(condition)	// ํ•ด๋‹น ํด๋กœ์ €๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๋งŒ ๋ชจ์•„์„œ ์ƒˆ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋ฆฌํ„ด

var v = [[Int]]()
var xDic = [Int: Int]()
xDic[v[i][0]]
let x = xDic.filter { $0.value < 2 }.keys
ans.append(contentsOf: xDic.values.filter { $0 < 2 })

2์ฐจ์› ๋ฐฐ์—ด ์ •๋ ฌํ•˜๊ธฐ

var arr = [[0, 3], [1, 9], [2, 6]]
let sortedArray = arr.sorted(by: { $0[1] < $1[1] })	// ๊ฐ ๋ฐฐ์—ด ๋‘๋ฒˆ์งธ ์ˆ˜ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ
let sortedArray = arr.sorted(by: { $0.1 < $1.1 })	// ๊ฐ™์€ ๊ฒฐ๊ณผ
print(sortedArray)		// [[0, 3], [2, 6], [1, 9]]

init (repeating: count:)

// ์ˆ˜๋ฐ•์ˆ˜๋ฐ•์ˆ˜๋ฐ•
let watermelon4 = String(repeating: "์ˆ˜๋ฐ•", count: 4)
print(watermelon4)

// [false, false, false, false, false]
let liar = Array(repeating: false, count: 5)
print(liar)
  • ๋ฐฐ์—ด์•ˆ์— ๋‹ด๊ธด ์ˆซ์ž๋งŒํผ ์ผ์ผ์ด printํ•˜๋ฉด ๋งค๋ฒˆ printํ•˜๋Š” ๊ฒƒ๋„ ๋А๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ดˆ๊ณผ ๋ฐœ์ƒ
    • String - init (repeating: count: ) ๋กœ ํ•œ๋ฒˆ์— ๋งŒ๋“ค์–ด์„œ String์— ์ €์žฅํ•ด์„œ ์ถœ๋ ฅํ•˜๋ฉด ํ•ด๊ฒฐํ• ์ˆ˜์žˆ๋‹ค

 

var answer = ""
// \n ์ด์šฉํ•ด์„œ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ถœ๋ ฅ
answer += String(repeating: "\(value)\n", count: countValue)
// ๊ณต๋ฐฑ ์ด์šฉํ•ด์„œ ํ•œ ์ค„์— ๋„์–ด์„œ ์ถœ๋ ฅ
answer += String(repeating: "\(value)", count: countValue)
// ํ•œ ์ค„์— ๋ถ™์—ฌ์„œ ์ถœ๋ ฅ
answer += String(repeating: "\(value)", count" countValue)
print(answer)โ€‹

์ตœ๋Œ€๊ณต์•ฝ์ˆ˜, ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜

// ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜
func GCD(_ a: Int, _ b: Int) -> Int {
   let mod: Int = a % b
   return 0 == mod ? min(a, b) : GCD(b, mod)
}

// ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜
func LCM(_ a: Int, _ b: Int) -> Int {
   return a * b / GCD(a, b)
}

heap / ์šฐ์„ ์ˆœ์œ„ ํ

Swift์—” ํž™์ด ์—†๋‹ค๊ณ  ํ•œ๋‹ค.... ์Šคํƒ, ํ, ์กฐํ•ฉ ๋“ฑ๋“ฑ ์—†๋‹ค๊ณ  ํ•œ๋‹ค...

https://developer-p.tistory.com/190

 

์Šค์œ„ํ”„ํŠธ | "(์ตœ์†Œ)ํž™ ๊ตฌํ˜„"ํ•˜๊ธฐ (Swift5 | Heap - MinHeap)

์ฝ”ํ…Œ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€๋ฅผ ํ•˜๋˜ ์ค‘, ๋ฐฑ์ค€ 1927๋ฒˆ์„ ํ’€๊ธฐ ์œ„ํ•ด์„  ์ตœ์†Œํž™(MinHeap) ๊ตฌํ˜„์ด ํ•„์š”ํ–ˆ์Šต๋‹ˆ๋‹ค. Swift์—” ํž™์ด ์—†์Šต๋‹ˆ๋‹ค. ๋ฌผ๋ก  ํ, ๋ฑ, ์ˆœ์—ด, ์กฐํ•ฉ ๋“ฑ๋“ฑ ๋‹ค ์—†์Šต๋‹ˆ๋‹ค... ์ตœ๋Œ€ ํž™์— ๋Œ€ํ•ด์„  ์•„๋ž˜ ๋งํฌ์—

developer-p.tistory.com

 


์ˆœ์—ด๊ณผ ์กฐํ•ฉ

์ด๊ฑฐ๋ฅผ ์“ธ์ผ์ด ๊ณง ์ƒ๊ธฐ๋ฉด ์ˆ˜์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค...

 

https://velog.io/@qwer15417/Swift-%EC%88%9C%EC%97%B4%EA%B3%BC-%EC%A1%B0%ED%95%A9

 

[Swift] ์ˆœ์—ด๊ณผ ์กฐํ•ฉ

swift๋Š” ์ˆœ์—ด๊ณผ ์กฐํ•ฉ์„ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ์ˆœ์—ด๊ณผ ์กฐํ•ฉ์„ Stack๊ณผ ์žฌ๊ท€๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๋„๋ก ํ•˜์ž.

velog.io